Truncating a Stream...

G

Guest

Is it possible to truncate a MemroyStream to free up the space up to the
current position?

I wantes to be able to just keep dumping bytes into a stream and then read
the stream as needed. After the read, free up the portion of the stream I
read, esentially making the first unread byte the first byte in the stream.

Thanks.

Jerry
 
J

Jon Skeet [C# MVP]

rlrcstr said:
Is it possible to truncate a MemroyStream to free up the space up to the
current position?

I wantes to be able to just keep dumping bytes into a stream and then read
the stream as needed. After the read, free up the portion of the stream I
read, esentially making the first unread byte the first byte in the stream.

MemoryStreams are basically backed by a byte array. You'd need to make
a copy of the part of the byte array you still need, and either create
a new MemoryStream or reset the current one. Your Position property is
likely to be wrong afterwards though...

It would possibly be easier to write such a stream class yourself than
to use MemoryStream. For one thing, you could make it use several
chunks of memory rather than a single byte array, which would make it
cheaper to throw the start away.
 
G

Guest

Sounds to me like you want / need some implementation of a CircularBuffer
class.
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top