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