MemoryStream as buffer for incoming data

M

Markus Ewald

I guess this counts as a rather adventurous use case, but can the
MemoryStream class somehow be used for buffers (eg. in socket
communications) that fill the MemoryStream's internal byte array
directly instead of using the stream's Write() method?

I could of course just use a plain byte array, bit this would be more
convenient to manage and I wouldn't need to copy around received data
before it could be parsed using a BinaryReader.

The following code snippet doesn't seem to work:

System.IO.MemoryStream ms = new System.IO.MemoryStream(100);

// Bypass the Write() method
// (assume this was a Socket.Receive() call)
ms.GetBuffer()[0] = 1;
ms.GetBuffer()[1] = 2;
ms.GetBuffer()[2] = 3;
ms.GetBuffer()[3] = 4;
ms.SetLength(4);

// ms now has a Length of 4 but its contents have been cleared

All I need would be a way to tell the MemoryStream that its actual size
is not what it thinks it is ;)

-Markus-
 
V

Vadym Stetsyak

Hello, Markus!
You wrote on Thu, 03 Aug 2006 12:09:14 +0200:

ME> The following code snippet doesn't seem to work:

ME> System.IO.MemoryStream ms = new System.IO.MemoryStream(100);

ME> // Bypass the Write() method
ME> // (assume this was a Socket.Receive() call)
ME> ms.GetBuffer()[0] = 1;
ME> ms.GetBuffer()[1] = 2;
ME> ms.GetBuffer()[2] = 3;
ME> ms.GetBuffer()[3] = 4;
ME> ms.SetLength(4);

ME> // ms now has a Length of 4 but its contents have been cleared

ME> All I need would be a way to tell the MemoryStream that its actual size
ME> is not what it thinks it is ;)

Just a hint, SetLength will clear inner buffer only if:
- new size value is larger then current capacity
- new size is larger then current length.

Your code didn't work because you were not actually writing to stream.
Change code to
ms.Position = 0;
ms.WriteByte(1);
ms.WriteByte(2);
ms.WriteByte(3);
ms.WriteByte(4);
ms.SetLength(4);
 
M

Markus Ewald

Vadym said:
Hello, Markus!
You wrote on Thu, 03 Aug 2006 12:09:14 +0200:

ME> [...]

Just a hint, SetLength will clear inner buffer only if:
- new size value is larger then current capacity
- new size is larger then current length.

Your code didn't work because you were not actually writing to stream.
Change code to
ms.Position = 0;
ms.WriteByte(1);
ms.WriteByte(2);
ms.WriteByte(3);
ms.WriteByte(4);
ms.SetLength(4);

Hi!

Sorry, but that doesn't help me. Looks like you've only looked at the
source code, not the problem I was describing ;)

I am thinking about using the MemoryStream as a receive buffer for
Socket.BeginReceive(). BeginReceive() writes to a byte array, so it's
impossible to make it call MemoryStream.WriteByte().

Nevertheless, I think I've found a workaround:

ms.Position = 0;
int byteCount = socket.Receive(ms.GetBuffer(), 0, ms.Capacity);

// This funny piece of code will cause the MemoryStream to enlarge
// itself without clearing the additional bytes. The array is
// copied on the place, which is hopefully detected by the runtime
// to result in a no-op.
ms.WriteBytes(ms.GetBuffer(), 0, byteCount);

-Markus-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top