Recommended Posts

Is it possible to be able to write to the byte[] got at by GetBuffer from a MemoryStream ? I managed to write data into its area but the problem obviously comes with dealing with MemoryStream length property. I tried calling a SetLength but this seems to zero out the byte[] data. What I'm doing is dealing with sockets and pointing BeginReceive to the MemoryStreams byte[], I was just wondering if this is at all possible without having to write additional glue code, ie, having a networking buffer then doing a MemoryStream.Read. Any tips would be greatly appreciated. -Nik

Share on other sites
Ah nevermind, seems I can copy the MemoryStreams contents to itself and afterwards set Position to 0 which just has the overheads of doing a needless copy but will save me reinventing the wheel :)

Cheers.

Share on other sites
I believe that if you use the memorystream constructor that takes a byte[] that it will use that byte array as its backing buffer. If you keep a reference to that array you can do whatever you want with it. You could for example make multiple memorystreams on the same buffer (EX: one for reading and one for writing).

Hope this helps.

Share on other sites
Quote:
 Original post by turnpastI believe that if you use the memorystream constructor that takes a byte[] that it will use that byte array as its backing buffer. If you keep a reference to that array you can do whatever you want with it. You could for example make multiple memorystreams on the same buffer (EX: one for reading and one for writing).Hope this helps.

Confirmed.
byte[] buffer = new byte[]{0,0,0,0};MemoryStream ms = new MemoryStream(buffer);buffer[0] = 255;int test = ms.ReadByte();Console.WriteLine(test.ToString());

Prints '255'.

Share on other sites
I don't have reflector handy atm, but wouldn't passing a byte[] into the constructor set the length and capacity to the same thing, so would in effect give me the same problem, well not the same problem as the length would be zero using the constructor with an initial size (capacity), but would still need the same workarounds for how much real data is in a fixed sized buffer.

Obviously there are benefits in sharing the buffer, but isn't an issue in my case as I only use one anyway.

Share on other sites
I guess it is just a matter of preference. Either you make and manage your own buffer or you let MemoryStream manage the buffer. The big problem with letting MemoryStream do the management is that you may be relying on it to do things that it has made no guarantees it is going to do consistently. For example GetBuffer can return different references depending on when and how it is called.

Quote:
 From MSDN:To create a MemoryStream instance with a publicly visible buffer, use MemoryStream, MemoryStream(array[]()[], Int32, Int32, Boolean, Boolean), or MemoryStream(Int32). If the current stream is resizable, two calls to this method do not return the same array if the underlying byte array is resized between calls. For additional information, see Capacity.

Share on other sites
Yeah, more getting something for nothing, as the only reason I used MemoryStream in the first place was to use the BinaryReader/Writer classes, still, I seem to have it all working.

Thanks for the replies though.

Share on other sites
Glad you got it all working :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628367
• Total Posts
2982278

• 10
• 9
• 13
• 24
• 11