[.net] MemoryStream and direct access to its byte[]

Started by
6 comments, last by turnpast 14 years, 2 months ago
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
Advertisement
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.
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.
Quote:Original post by turnpast
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.


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'.
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.
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<Byte>[]()[], 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.


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.
Glad you got it all working :)

This topic is closed to new replies.

Advertisement