Re: Reading into a buffer and writing from it at the same time
Thank you, but there is unfortunately
no java.util.concurrent.* in my embedded device.
And I can't backport parts of new JVM to finish my task.
I'll try to finish my custom solution. I've switched to using
several buffers of a fixed size as suggested by Patricia,
but I still have some bug there (final buffer is not being
written out and also sometimes it deadlocks).
It's only 50 lines, but tricky to fix it up...
Regards
Alex
while (true) {
if (readBuffer.read(in) <= 0)
break;
}
......
final class CyclicBuffer
{
private static int NBUFFERS = 4;
private byte[][] _buffers;
private int[] _bytesRead;
private int _readIndex;
private int _writeIndex;
public CyclicBuffer(int size)
{
_buffers = new byte[NBUFFERS][size];
_bytesRead = new int[NBUFFERS];
_readIndex = 0;
_writeIndex = 0;
}
public int read(InputStream in) throws IOException
{
_bytesRead[_readIndex] = in.read(_buffers[_readIndex]);
if (_bytesRead[_readIndex] <= 0) {
return -1;
}
synchronized(this) {
_readIndex = (_readIndex + 1) % NBUFFERS;
// something has been read, so allow writing again
notify();
if (_readIndex == _writeIndex) {
try {
// all buffers are full, so stop reading for now
wait();
} catch (InterruptedException e) {
}
}
}
return 1;
}
public int write(OutputStream out) throws IOException
{
if (_bytesRead[_writeIndex] <= 0) {
return -1;
}
out.write(_buffers[_writeIndex], 0, _bytesRead[_writeIndex]);
synchronized(this) {
_writeIndex = (_writeIndex + 1) % NBUFFERS;
// something has been written out, so allow reading again
notify();
if (_readIndex == _writeIndex) {
try {
// all buffers are empty, so stop writing for now
wait();
} catch (InterruptedException e) {
}
}
}
return 1;
}
}