51 lines
969 B
Java
51 lines
969 B
Java
package com.flaremicro.test.util;
|
|
|
|
import java.io.EOFException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
|
|
public class InOutBuffer {
|
|
|
|
private final ArrayList<Byte> buffer = new ArrayList<Byte>();
|
|
|
|
public final InputStream getInputStream() {
|
|
return new BufferInputStream();
|
|
}
|
|
|
|
public final OutputStream getOutputStream() {
|
|
return new BufferOutputStream();
|
|
}
|
|
|
|
public void empty() {
|
|
buffer.clear();
|
|
}
|
|
|
|
class BufferInputStream extends InputStream {
|
|
@Override
|
|
public int read() throws IOException {
|
|
synchronized (buffer)
|
|
{
|
|
if (buffer.size() <= 0)
|
|
throw new EOFException("End of file");
|
|
return buffer.remove(0) & 0xFF;
|
|
}
|
|
}
|
|
}
|
|
|
|
class BufferOutputStream extends OutputStream {
|
|
@Override
|
|
public void write(int arg0) throws IOException {
|
|
synchronized (buffer)
|
|
{
|
|
buffer.add((byte) arg0);
|
|
}
|
|
}
|
|
}
|
|
|
|
public int size() {
|
|
return buffer.size();
|
|
}
|
|
}
|