MemoryStream and BinaryReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?VmljdG9y?=

    #1

    MemoryStream and BinaryReader

    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.
  • Jon Skeet [C# MVP]

    #2
    Re: MemoryStream and BinaryReader

    On Jun 11, 11:32 am, Victor <Vic...@discuss ions.microsoft. comwrote:
    Could you tell me can I keep the MemoryStream open and "close" the
    BinaryReader?
    I've got a NonClosingStrea m (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

    Comment

    • qglyirnyfgfo@mailinator.com

      #3
      Re: MemoryStream and BinaryReader

      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(li veAlone.ToArray ());


      On Jun 11, 5:32 am, Victor <Vic...@discuss ions.microsoft. comwrote:
      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.

      Comment

      • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

        #4
        Re: MemoryStream and BinaryReader

        Victor wrote:
        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.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        Working...