1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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.ByteArray;
- /**
- * Handles the reading and writing of the socket in- and outputstream for byte arrays
- * @author Mihai Plasoianu
- */
- public class ByteArrayReaderWriter implements ReaderWriter<ByteArray> {
- 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;
- }
- @Override
- public ByteArray 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 ByteArray(buffer);
- }
- @Override
- public void write(List<ByteArray> message) throws IOException {
- for (ByteArray m : message) {
- out.write(m.get());
- out.flush();
- }
- }
- }
|