Memory Stream in Java
I am looking for a memory stream implementation in Java. The implementation should be roughly modeled after the .NET memory stream implementation.
Basically I would like to have a class MemoryStream
which has to factory
methods:
class MemoryStream {
MemoryInput createInput();
MemoryOutput createOutput();
}
class MemoryInput extends InputStream {
long position();
void seek(long pos);
}
class MemoryOutput extends OutputStream {
long position();
void seek(long pos);
}
So once I have an instance from the class MemoryStream
I
should be able to concurrently simultaneously create
input and output streams, which should also allow
positioning in any direction. The memory stream need
not be circular, it should work for small sizes well
and automatically grow. The memory stream need only
be confined into one process.
Any out of the box code available?
Solution 1:
ByteArrayInputStream
and ByteArrayOutputStream
is what you are looking for.
These are implementations of the interfaces InputStream
and OutputStream
that read from and write to a byte array in memory. For ByteArrayOutputStream
, the array will grow automatically as you write data to the stream.
Solution 2:
Does it need to support the Input and Output Streams? If not I would just use a ByteBuffer which allows you to read/write primitive types at random locations. (Up to 2 GB)
You can share a ByteBuffer between a reader and a writer.
e.g.
// 1 GB of virtual memory outside the heap.
ByteBuffer writer = ByteBuffer.allocateDirect(1024*1024*1024);
ByteBuffer reader = writer.slice();
You can share memory between threads (e.g. Exchanger) and processes (using memory mapped files)
Solution 3:
You can use PipedInputStream and PipedOutputStream
like this:
PipedOutputStream outstr = new PipedOutputStream();
PipedInputStream instr = new PipedInputStream(outstr);
that won't directly allow you to seek, but it does allow you to skip as many bytes you want from the input stream.
Be aware that whenever you write into the outstr it is blocked until everything is read from in instr (that is: if I remember correctly the Streams don't Buffer, but you can decorate them with a BufferedInputStream then you don't have to bother.