MemoryStream and BinaryReader

  • Thread starter Thread starter Victor
  • Start date Start date
V

Victor

Hi,

Could you tell me can I keep the MemoryStream open and "close" the
BinaryReader?

As the MemoryStream is used for buffering the TCP data and BinaryReader is
only used to read the MemoryStream, so I don't want to close the MemoryStream.

Do I really need to close the BinaryReader if the underlying backing store
needs not to be closed?

Thanks a lot
Victor.
 
Could you tell me can I keep the MemoryStream open and "close" the
BinaryReader?

I've got a NonClosingStream (or something like that - I can never
remember the exact name) in http://pobox.com/~skeet/csharp/miscutil/
which could be useful to you.

Bear in mind, however, that BinaryReader will potentially read beyond
where you think it might (for buffering) and that if you want to write
to the stream again afterwards you'll need to reposition yourself to
the end.

Jon
 
Hi,

Besides what Jon is suggesting, perhaps you could also just leave
alone the stream that you are using to write data too and clone it and
use the cloned copy to mess around with.

MemoryStream liveAlone = new MemoryStream();
MemoryStream messAround = new MemoryStream(liveAlone.ToArray());
 
Victor said:
Could you tell me can I keep the MemoryStream open and "close" the
BinaryReader?

As the MemoryStream is used for buffering the TCP data and BinaryReader is
only used to read the MemoryStream, so I don't want to close the MemoryStream.

No, when the BinaryReader is closed, it also closes the underlying stream.
Do I really need to close the BinaryReader if the underlying backing store
needs not to be closed?

Not really, but the BinaryReader is useless if the underlying stream is
closed. You can not re-connect a different stream to the BinaryReader.

You could make your own middle-stream to use between the MemoryStream
and the BinaryReader, that would support switching of the underlying stream.
 
Back
Top