C# 附加到 MemoryStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12339903/
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
Appending to MemoryStream
提问by Iain Sproat
I'm trying to append some data to a stream. This works well with FileStream, but not for MemoryStreamdue to the fixed buffer size.
我正在尝试将一些数据附加到流中。这适用于FileStream,但不适用于MemoryStream固定缓冲区大小。
The method which writes data to the stream is separated from the method which creates the stream (I've simplified it greatly in the below example). The method which creates the stream is unaware of the length of data to be written to the stream.
将数据写入流的方法与创建流的方法是分开的(我在下面的示例中大大简化了它)。创建流的方法不知道要写入流的数据的长度。
public void Foo(){
byte[] existingData = System.Text.Encoding.UTF8.GetBytes("foo");
Stream s1 = new FileStream("someFile.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
s1.Write(existingData, 0, existingData.Length);
Stream s2 = new MemoryStream(existingData, 0, existingData.Length, true);
s2.Seek(0, SeekOrigin.End); //move to end of the stream for appending
WriteUnknownDataToStream(s1);
WriteUnknownDataToStream(s2); // NotSupportedException is thrown as the MemoryStream is not expandable
}
public static void WriteUnknownDataToStream(Stream s)
{
// this is some example data for this SO query - the real data is generated elsewhere and is of a variable, and often large, size.
byte[] newBytesToWrite = System.Text.Encoding.UTF8.GetBytes("bar"); // the length of this is not known before the stream is created.
s.Write(newBytesToWrite, 0, newBytesToWrite.Length);
}
An idea I had was to send an expandable MemoryStreamto the function, then append the returned data to the existing data.
我的一个想法是向MemoryStream函数发送一个可扩展的,然后将返回的数据附加到现有数据中。
public void ModifiedFoo()
{
byte[] existingData = System.Text.Encoding.UTF8.GetBytes("foo");
Stream s2 = new MemoryStream(); // expandable capacity memory stream
WriteUnknownDataToStream(s2);
// append the data which has been written into s2 to the existingData
byte[] buffer = new byte[existingData.Length + s2.Length];
Buffer.BlockCopy(existingData, 0, buffer, 0, existingData.Length);
Stream merger = new MemoryStream(buffer, true);
merger.Seek(existingData.Length, SeekOrigin.Begin);
s2.CopyTo(merger);
}
Any better (more efficient) solutions?
任何更好(更有效)的解决方案?
采纳答案by Rotem
A possible solution is not to limit the capacity of the MemoryStreamin the first place.
If you do not know in advance the total number of bytes you will need to write, create a MemoryStreamwith unspecified capacity and use it for both writes.
一个可能的解决方案是首先不限制 的容量MemoryStream。如果您事先不知道需要写入的总字节数,请创建一个MemoryStream具有未指定容量的文件并将其用于两次写入。
byte[] existingData = System.Text.Encoding.UTF8.GetBytes("foo");
MemoryStream ms = new MemoryStream();
ms.Write(existingData, 0, existingData.Length);
WriteUnknownData(ms);
This will no doubt be less performant than initializing a MemoryStreamfrom a byte[], but if you need to continue writing to the stream I believe it is your only option.
这无疑比MemoryStream从 a初始化 a 的性能要低byte[],但是如果您需要继续写入流,我相信这是您唯一的选择。

