ByteArrayReaderWriter.java 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. ;
  22. byte[] buffer = new byte[availableBytes];
  23. in.read(buffer);
  24. return new ByteArray(buffer);
  25. }
  26. @Override
  27. public void write(List<ByteArray> message) throws IOException {
  28. for (ByteArray m : message) {
  29. out.write(m.get());
  30. out.flush();
  31. }
  32. }
  33. }