I have a binarywriter that is backed by a memorystream as such:
BinaryWriter m_binaryWriter = new BinaryWriter(new MemoryStream(BUFFER_SIZE));
I want to get the base reader and read it into a byte array:
long DataLength = m_binaryWriter.BaseStream.Length;
byte[] MemoryStrData = new byte[DataLength];
int BytesRead = m_binaryWriter.BaseStream.Read(MemoryStrData, 0, (int)DataLength);
However, BytesRead is always 0 (and obviously MemoryStrData contains all 0's) . I have tried to use a BinaryReader to read the stream out of the base stream, but that resulted in the same thing. Am I missing something here?
Do you always know that the BinaryWriter will be backed by a MemoryStream?
If so, either keep a reference to the MemoryStream around, or cast the BaseStream as appropriate, and then use
MemoryStream.ToArray()
to get a byte array of the contents of the MemoryStream.