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.

4 Answers
4

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.

ByteArrayInputStream supports mark() and reset() to mark a position in the stream so that you can jump back there later. ByteArrayOutputStream doesn't have this. Peter Lawrey's suggestion, using NIO ByteBuffer, is probably more useful.
– JesperDec 8 '11 at 22:12

Well these ByteBuffers, I am not yet sure. Problem is a common understanding of the non-functional requirements: Frequency, amount of data and type of access. Depending they could be a good or bad idea.
– Transfinite NumbersDec 8 '11 at 22:16

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.

How do I use for small sizes? Will it automatically grow?
– Transfinite NumbersDec 8 '11 at 20:01

It doesn't grow automatically as such. However if you make direct buffers much larger than you need but you don't use it, the OS doesn't allocate the memory to your process.
– Peter LawreyDec 8 '11 at 20:05

Aha, ok, I didn't know. Is this guaranteed across OS and JVM?
– Transfinite NumbersDec 8 '11 at 20:07

Yeah, Input / Output is mandatory. So that I can plug into an existing application.
– Transfinite NumbersDec 8 '11 at 20:08

NIO allows you to directly transfer data within kernel memory - I'm not sure if it exactly overlaps with .NET's memory stream. Here's a simple example of mapping an entire file into memory for reading.