By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,961 Members | 2,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,961 IT Pros & Developers. It's quick & easy.

Truncating a Stream...

P: n/a
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
Sep 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
rlrcstr <rl*****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #2

P: n/a
Thanks Jon. THat's basically what I ended up doing.

"Jon Skeet [C# MVP]" wrote:
rlrcstr <rl*****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #3

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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"rlrcstr" wrote:
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
Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.