views:

392

answers:

6

As discussed before, when a BinaryReader or BinaryWriter gets closed, its underlying Stream get closed as well (aargh). Consider this situation: a routine R is passed a MemoryStream, say M; I would like to write some stuff to M and then pass it to another routine for more processing (not necessarily writing). For convenience, I'd like to wrap M in a BinaryWriter to do my writing. After writing, I'm done with the BinaryWriter but not with M.

void R(MemoryStream M)
{
    using (B = new BinaryWriter(M))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}

But, I can't dispose of the BinaryStream without closing M.

Q: Is there a way to do any of the following?

  • extract the underlying byte[] from a MemoryStream,
  • clone a Stream
  • reopen a Stream after it's been closed
A: 

A somewhat naive approach is to use

byte buf[] = MemoryStream.ToArray();

To copy the stream contents to a byte array. You can turn it back into a stream with

MemoryStream ms = new MemoryStream(buf);
David Lively
Worth emphasising : this method works on a closed stream as well as a live one.
Steve Gilham
+2  A: 

You can:

  • Call M.ToArray() to get the stream as an array of bytes.
  • Subclass BinaryWriter and override the Dispose method to prevent closing of the child stream
Chris
+1 specifically for the Dispose override.
Orion Adrian
Can't you just avoid calling close/Dispose?
Adrian Pronk
A: 

Accoring to this M.Clone(); should work. But i may be wrong...

Trickster
-1 for posting to a website which doesn't allow viewing it's contents for free, which is pretty much useless
Ion Todirel
+6  A: 

You should better get the underlying byte[] buffer using

byte[] buffer = ms.GetBuffer();

And then copy the byte data using the Array.Copy() method. You are free to create a new stream with it.

Ricky AH
I don't understand why you're saying it's better to use GetBuffer and then make a copy using Array.Copy. If I wanted a copy I'd just use ToArray.
I. J. Kennedy
From MSDN doc This method omits unused bytes in MemoryStream from the array. To get the entire buffer, use the GetBuffer method.http://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray.aspx
Ricky AH
The above "This Method" refers to the MemoryStream.ToArray()
Ricky AH
+2  A: 

Thanks to several who suggested ToArray, I was led to right answer, which is `M.GetBuffer'. ToArray is not too bad, but it

  • makes a copy
  • gets only part of the buffer

GetBuffer just grabs a reference to the underlying byte[], which is what I'm after.

I. J. Kennedy
Which was pointed out in my answer ;)
Ricky AH
+3  A: 

You can use things like the MiscUtil.IO.NonClosingStreamWrapper in MiscUtil, which wraps a Stream and simply ignores Close/Dispose requests. For just this purpose.

void R(MemoryStream M)
{
    using (B = new BinaryWriter(new NonClosingStreamWrapper(M)))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}
Marc Gravell