12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package de.tudarmstadt.informatik.hostage.io;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.List;
- import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
- public class ByteArrayReaderWriter implements ReaderWriter<ByteArray> {
- private BufferedInputStream in;
- private BufferedOutputStream out;
- public ByteArrayReaderWriter(InputStream in, OutputStream out) {
- this.in = new BufferedInputStream(in);
- this.out = new BufferedOutputStream(out);
- }
- @Override
- public ByteArray read() throws IOException {
- while (in.available() == 0) {
- Thread.yield();
- }
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- int oneByte;
- while ((oneByte = in.read()) != -1) {
- os.write(oneByte);
- }
- return new ByteArray(os.toByteArray());
- }
- @Override
- public void write(List<ByteArray> message) throws IOException {
- for (ByteArray m : message) {
- out.write(m.get());
- out.flush();
- }
- }
- }
|