ByteArrayReaderWriter.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. public ByteArrayReaderWriter(InputStream in, OutputStream out) {
  14. this.in = new BufferedInputStream(in);
  15. this.out = new BufferedOutputStream(out);
  16. }
  17. @Override
  18. public ByteArray read() throws IOException {
  19. int availableBytes;
  20. while((availableBytes = in.available()) <= 0) {
  21. try {
  22. Thread.sleep(500);
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. byte[] buffer = new byte[availableBytes];
  28. in.read(buffer);
  29. return new ByteArray(buffer);
  30. }
  31. @Override
  32. public void write(List<ByteArray> message) throws IOException {
  33. for (ByteArray m : message) {
  34. out.write(m.get());
  35. out.flush();
  36. }
  37. }
  38. }