123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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<Packet> outputLine) throws IOException {
- for (Packet o : outputLine) {
- out.write(o.getMessage());
- out.flush();
- }
- }
- }
|