ByteArrayReaderWriter.java 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package de.tudarmstadt.informatik.hostage.io;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.List;
  9. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  10. public class ByteArrayReaderWriter implements ReaderWriter<ByteArray> {
  11. private BufferedInputStream in;
  12. private BufferedOutputStream out;
  13. private int SLEEPTIME;
  14. public ByteArrayReaderWriter(InputStream in, OutputStream out, int SLEEPTIME) {
  15. this.in = new BufferedInputStream(in);
  16. this.out = new BufferedOutputStream(out);
  17. this.SLEEPTIME = SLEEPTIME;
  18. }
  19. @Override
  20. public ByteArray read() throws IOException {
  21. int availableBytes;
  22. while((availableBytes = in.available()) <= 0) {
  23. try {
  24. Thread.sleep(SLEEPTIME);
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. byte[] buffer = new byte[availableBytes];
  30. in.read(buffer);
  31. return new ByteArray(buffer);
  32. }
  33. @Override
  34. public void write(List<ByteArray> message) throws IOException {
  35. for (ByteArray m : message) {
  36. out.write(m.get());
  37. out.flush();
  38. }
  39. }
  40. }