getbuffer

When is GetBuffer() on MemoryStream ever useful?

荒凉一梦 提交于 2019-12-28 05:56:29
问题 I've known that GetBuffer() on a MemoryStream in C#/.NET has to be used with care, because, as the docs describe here, there can be unused bytes at the end, so you have to be sure to look only at the first MemoryStream.Length bytes in the buffer. But then I ran into a case yesterday where bytes at the beginning of the buffer were junk! Indeed, if you use a tool like reflector and look at ToArray() , you can see this: public virtual byte[] ToArray() { byte[] dst = new byte[this._length - this.

Why does MemoryStream.GetBuffer() always throw?

萝らか妹 提交于 2019-12-03 23:35:52
问题 The following code will always throw UnuthorizedAccessException (MemoryStream's internal buffer cannot be accessed.) byte[] buf1 = { 2, 3, 5, 7, 11 }; var ms = new MemoryStream(buf1); byte[] buf2 = ms.GetBuffer(); // exception will be thrown here This is in a plain old console app and I'm running as an admin. I can't imagine a more privileged setting I could give this code. So why can't I get at this buffer? (And if nobody can, what's the point of the GetBuffer method?) The MSDN docs say To

Why does MemoryStream.GetBuffer() always throw?

北战南征 提交于 2019-12-01 03:00:06
The following code will always throw UnuthorizedAccessException (MemoryStream's internal buffer cannot be accessed.) byte[] buf1 = { 2, 3, 5, 7, 11 }; var ms = new MemoryStream(buf1); byte[] buf2 = ms.GetBuffer(); // exception will be thrown here This is in a plain old console app and I'm running as an admin. I can't imagine a more privileged setting I could give this code. So why can't I get at this buffer? (And if nobody can, what's the point of the GetBuffer method?) The MSDN docs say To create a MemoryStream instance with a publicly visible buffer, use MemoryStream, MemoryStream(array[],