Java memory stream

I am looking for an implementation of a memory stream in Java. The implementation should be roughly modeled after the implementation of the .NET memory stream .

Basically, I would like to have a MemoryStream class that has 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); } 

Therefore, when I have an instance from the MemoryStream class, I can simultaneously create input and output streams, which should also allow positioning in any direction. The memory stream does not have to be circular, it should work well with small sizes and automatically increase. Memory flow should be limited to only one process.

Any of the box codes available?

+62
java stream memory seek
Dec 08 '11 at 19:43
source share
4 answers

ByteArrayInputStream and ByteArrayOutputStream are what you are looking for.

This is an implementation of the InputStream and OutputStream , which are read and written to an array of bytes in memory. For ByteArrayOutputStream array will grow automatically when writing data to the stream.

+93
Dec 08 2018-11-12T00:
source share

You can use PipedInputStream and PipedOutputStream

like this:

 PipedOutputStream outstr = new PipedOutputStream(); PipedInputStream instr = new PipedInputStream(outstr); 

which will not directly allow you to search, but it allows you to skip so many bytes from the input stream.

Keep in mind that whenever you write to outstr, it blocks until everything is read in instr (that is: if I remember correctly, Streams is not Buffer, but you can decorate them with BufferedInputStream, then you do not have to worry.

+9
Dec 08 '11 at 19:53
source share

Do I need to support input and output streams? If not, I would just use ByteBuffer, which allows you to read / write primitive types in random places. (Up to 2 GB)

You can share ByteBuffer between reader and writer.

eg.

 // 1 GB of virtual memory outside the heap. ByteBuffer writer = ByteBuffer.allocateDirect(1024*1024*1024); ByteBuffer reader = writer.slice(); 

You can exchange memory between threads (e.g. Exchanger) and processes (using memory mapped files)

+8
Dec 08 '11 at 19:47
source share

NIO allows you to directly transfer data to kernel memory - I'm not sure if it exactly overlaps with the .NET memory stream. Here is a simple example of matching the entire file in memory for reading.

+4
Dec 08 '11 at 19:46
source share



All Articles