重置或清除 .NET MemoryStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2462391/
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-03 14:05:30  来源:igfitidea点击:

Reset or Clear .NET MemoryStream

.netmemorystream

提问by Andrew Bezzub

The .NET MemoryStream does not appear to have a .Reset or .Clear method.

.NET MemoryStream 似乎没有 .Reset 或 .Clear 方法。

I was thinking of using the following code to accomplish this:

我正在考虑使用以下代码来完成此操作:

ms.Seek(0, IO.SeekOrigin.Begin)
ms.SetLength(0)

What is the proper way to clear or reset an existing .NET MemoryStream?

清除或重置现有 .NET MemoryStream 的正确方法是什么?

采纳答案by omglolbah

The memorystream does not have a reset/clear method because it would be redundant. By setting it to zero length you clear it.

内存流没有重置/清除方法,因为它是多余的。通过将其设置为零长度,您可以清除它。

Of course you could always do:

当然,你总是可以这样做:

memoryStream = new MemoryStream(memoryStream.Capacity());

This would yield you a memorystream of the same size that is initialized.

这将为您生成一个与初始化大小相同的内存流。

If you really want to manually clear the stream I suspect you would have to resort to looping through the elements.

如果您真的想手动清除流,我怀疑您将不得不求助于遍历元素。

回答by Andrew Bezzub

Why do you need resetting memory stream? You always can create a new one. Or you can use:

为什么需要重置内存流?你总是可以创建一个新的。或者你可以使用:

memoryStream.SetLength(0);

回答by Rana Ian

Since a MemoryStream is essentially a byte array with an index (and some other supporting members) clearing the byte array and resetting the index can be considered resetting and clearing the MemoryStream. If the initial state of a MemoryStream is a zeroed array with a position of zero then an example of a MemoryStream reset may be:

由于 MemoryStream 本质上是一个带有索引(和一些其他支持成员)的字节数组,因此清除字节数组并重置索引可以被视为重置和清除 MemoryStream。如果 MemoryStream 的初始状态是位置为零的归零数组,则 MemoryStream 重置的示例可能是:

public static void Clear(this MemoryStream source)
{
  byte[] buffer = source.GetBuffer();
  Array.Clear(buffer, 0, buffer.Length);
  source.Position = 0;
  source.SetLength(0);
}

It is incorrect to say that MemoryStream.SetLength alone resets or clears the MemoryStream since SetLength only clears the internal buffer array if the length exceeds the current buffer's length.

说 MemoryStream.SetLength 单独重置或清除 MemoryStream 是不正确的,因为如果长度超过当前缓冲区的长度,SetLength 只会清除内部缓冲区数组。

Reinitializing a MemoryStream is a valid approach but less efficient. One benefit of reinitializing the MemoryStream would be to guarantee that the stream was never closed. Once the MemoryStream is closed it can no longer be changed. If you can ensure that the MemoryStream instance isn't closed then clearing the buffer may be the way to go.

重新初始化 MemoryStream 是一种有效的方法,但效率较低。重新初始化 MemoryStream 的好处之一是保证流永远不会关闭。一旦 MemoryStream 关​​闭,就不能再更改它。如果您可以确保 MemoryStream 实例未关闭,则清除缓冲区可能是可行的方法。