windows 为什么 MemoryStream.GetBuffer() 总是抛出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:21:24  来源:igfitidea点击:

Why does MemoryStream.GetBuffer() always throw?

windowsexceptionmemorystreamgetbuffer

提问by I. J. Kennedy

The following code will always throw UnuthorizedAccessException (MemoryStream's internal buffer cannot be accessed.)

以下代码将始终抛出 UnuthorizedAccessException(无法访问 MemoryStream 的内部缓冲区。)

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?)

这是在一个普通的旧控制台应用程序中,我以管理员身份运行。我无法想象我可以提供此代码的更特权设置。那么为什么我不能进入这个缓冲区呢?(如果没有人可以,GetBuffer 方法的意义何在?)

The MSDN docs say

MSDN 文档说

To create a MemoryStream instance with a publicly visible buffer, use MemoryStream, MemoryStream(array[], Int32, Int32, Boolean, Boolean), or MemoryStream(Int32).

要创建具有公开可见缓冲区的 MemoryStream 实例,请使用 MemoryStream、MemoryStream(array[]、Int32、Int32、Boolean、Boolean) 或 MemoryStream(Int32)。

Am I not doing that?

我不这样做吗?

P.S. I don't want to use ToArray() because that makes a copy.

PS 我不想使用 ToArray() 因为它会复制。

回答by Pavel Minaev

Hereis the documentation for MemoryStream(byte[])constructor that you're using. It specifically says:

MemoryStream(byte[])您正在使用的构造函数的文档。它具体说:

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

此构造函数不公开底层流。GetBuffer 抛出 UnauthorizedAccessException。

You should use thisconstructor instead, with publiclyVisible = true.

您应该改用构造函数,使用publiclyVisible = true.

回答by Dolphin

Check the docs for MemoryStream.GetBuffer()

检查MemoryStream.GetBuffer()的文档

To create a MemoryStream instance with a publicly visible buffer, use MemoryStream, MemoryStream(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.

要创建具有公开可见缓冲区的 MemoryStream 实例,请使用 MemoryStream、MemoryStream(Byte[]、Int32、Int32、Boolean、Boolean) 或 MemoryStream(Int32)。如果当前流可调整大小,并且在两次调用之间调整了基础字节数组的大小,则对此方法的两次调用不会返回相同的数组。有关其他信息,请参阅容量。

You need to use a different constructor.

您需要使用不同的构造函数。

回答by Bomlin

To add to what others have already put in here...

要添加到其他人已经在这里放入的内容...

Another way to get your code to work is change your code to the following line.

使代码正常工作的另一种方法是将代码更改为以下行。

byte[] buf2 = ms.ToArray();

回答by Hyman Rosen

You appear to be using MemoryStream(array[])which does not match any of the three versions mentioned in the docs.

您使用的MemoryStream(array[])似乎与文档中提到的三个版本都不匹配。