package de.tudarmstadt.informatik.hostage.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import de.tudarmstadt.informatik.hostage.wrapper.Packet; /** * Handles the reading and writing of the socket in- and outputstream for byte arrays * @author Mihai Plasoianu * @author Wulf Pfeiffer */ public class ByteArrayReaderWriter implements ReaderWriter { private BufferedInputStream in; private BufferedOutputStream out; private int SLEEPTIME; /** * Constructor * @param in inputstream of socket * @param out outputstream of socket * @param SLEEPTIME time until timeout */ public ByteArrayReaderWriter(InputStream in, OutputStream out, int SLEEPTIME) { this.in = new BufferedInputStream(in); this.out = new BufferedOutputStream(out); this.SLEEPTIME = SLEEPTIME; } public Packet read() throws IOException { int availableBytes; while((availableBytes = in.available()) <= 0) { try { Thread.sleep(SLEEPTIME); } catch (InterruptedException e) { e.printStackTrace(); } } byte[] buffer = new byte[availableBytes]; in.read(buffer); return new Packet(buffer); } public void write(List outputLine) throws IOException { for (Packet o : outputLine) { out.write(o.getMessage()); out.flush(); } } }