I knew that GetBuffer() on a MemoryStream in C # /. NET should be used with care, because, as the documents describe here , there may be unused bytes at the end, so you should definitely look at only the first bytes of MemoryStream.Length in the buffer.
But then I came across a case yesterday when the bytes at the beginning of the buffer were junk! Indeed, if you use a tool such as a reflector and look at ToArray() , you can see this:
public virtual byte[] ToArray() { byte[] dst = new byte[this._length - this._origin]; Buffer.InternalBlockCopy(this._buffer, this._origin, dst, 0, this._length - this._origin); return dst; }
To do anything with the buffer returned by GetBuffer() , you really need to know _origin. The only problem is that _origin is private and there is no way to get to it ...
So my question is: what is the use of GetBuffer() on MemoryStream() without any GetBuffer() knowledge of how MemoryStream was created (which is what _origin sets)?
(It is this constructor and this constructor alone that sets the origin - if you want the MemoryStream around the byte array to start with a specific index in the byte array:
public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
)
aggieNick02 Oct 24 2018-12-12T00: 00Z
source share