本文介绍了如果是的GetBuffer()上的MemoryStream曾经有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的GetBuffer()在C#/一个MemoryStream。NET,必须小心使用,因为,作为文档描述的,有可能在年底未使用的字节,所以你就一定要看看仅在缓冲区中的第一MemoryStream.Length字节

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.

但后来我遇到了一个情况下,昨天,其中字节的缓冲区的开头都是垃圾!事实上,如果你使用一个工具如反光,看的ToArray(),你可以看到这一点:

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._origin];
    Buffer.InternalBlockCopy(this._buffer, this._origin, dst, 0,
        this._length - this._origin);
    return dst;
}

所以,与通过的GetBuffer 返回缓冲区做任何事情(),你真的需要知道_origin。唯一的问题是,_origin是私人的,没有办法得到它...

So 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's no way to get at it...

所以我的问题是 - 什么用的GetBuffer()的MemoryStream()没有一些先验知识如何将MemoryStream构建(这是什么套_origin)?

So my question is - what use is GetBuffer() on a MemoryStream() without some apriori knowledge of how the MemoryStream was constructed (which is what sets _origin)?

(正是这种构造,也只有这样的构造,即设置原点 - 当你想要一个字节数组围绕一个MemoryStream开始字节数组中的特定索引处为:

(It is this constructor, and only this constructor, that sets origin - for when you want a MemoryStream around a byte array starting at a particular index in the byte array:

public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)

推荐答案

答案是在的,你可能已经错过了。

The answer is in the GetBuffer() MSDN doc, you might have missed it.

当你创建一个的MemoryStream 不提供字节数组(字节[] ):

When you create a MemoryStream without providing a byte array (byte[]) :

它创建初始化为零的可扩展能力。

在换句话说,将MemoryStream将参照字节[] 用适当的大小,当通话将在流上进行。

In other words, the MemoryStream will reference to a byte[] with the proper size when a Write call will be made on the Stream.

因此​​,用的GetBuffer()就可以直接访问底层阵列并读取它。

Thus, with GetBuffer() you can directly access the underlying array and read to it.

可能是有用当你在,你会收到一个流不知道它的大小的情况。如果接收到的数据流通常是非常大的,这将是更快叫的GetBuffer()不是调用的ToArray()它复制引擎盖下的数据,请参见下文。

This could be usefull when you're in the situation that you will receive a stream without knowing its size. If the stream received is usually very big, it will be much faster to call GetBuffer() than calling ToArray() which copy the data under the hood, see below.

要获取只在缓冲区中的数据,使用的ToArray方法;  然而,在的ToArray存储器创建数据的副本。

我不知道此时你可能已经调用的GetBuffer()来获取之初的垃圾数据,也可能是在两个从调用其中数据第一个会被垃圾回收,但我不知道这会发生。

I wonder at which point you might have called GetBuffer() to get junk data at the beginning, it could be between two Write calls where the data from the first one would have been garbage collected, but I'm not sure if that could happen.

这篇关于如果是的GetBuffer()上的MemoryStream曾经有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 16:31