IEnumerable<Stream> 到一个大流或 MemoryStream
IEnumerable<Stream> to One big Stream or MemoryStream
我有 IEnumerable 流。我如何将其转换为一个大的 MemoryStream 或 byte[]。
我想要将所有 Stream inside 流附加到一个大 Stream/MemoryStream 中。通过遍历所有流,我们遇到了某些性能问题。我正在寻找最佳解决方案。
using (var ms = new MemoryStream())
{
foreach (var stream in streams)
{
stream.CopyTo(ms);
}
byteData = ms.ToArray();
}
谢谢
只需调用stream.CopyTo
Copying begins at the current position in the current stream, and does
not reset the position of the destination stream after the copy
operation is complete.
var list = new List<Stream>()
{
new MemoryStream(new byte[] {1, 2, 3, 4}),
new MemoryStream(new byte[] {5, 6, 7, 8}),
new MemoryStream(new byte[] {9, 10, 11, 12})
};
var result = new MemoryStream();
foreach (var stream in list)
stream.CopyTo(result);
var array =result.ToArray();
Console.WriteLine(string.Join(", ", array));
输出
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
您可以使用 VirtualStream 的 IEnumerable 代替 MemoryStream 或 FileStream
https://searchcode.com/codesearch/view/14192551/
基本上,当大小超过配置的阈值时,它会自动创建 FileStream。这些文件将在处理后被删除
我有 IEnumerable 流。我如何将其转换为一个大的 MemoryStream 或 byte[]。 我想要将所有 Stream inside 流附加到一个大 Stream/MemoryStream 中。通过遍历所有流,我们遇到了某些性能问题。我正在寻找最佳解决方案。
using (var ms = new MemoryStream())
{
foreach (var stream in streams)
{
stream.CopyTo(ms);
}
byteData = ms.ToArray();
}
谢谢
只需调用stream.CopyTo
Copying begins at the current position in the current stream, and does not reset the position of the destination stream after the copy operation is complete.
var list = new List<Stream>()
{
new MemoryStream(new byte[] {1, 2, 3, 4}),
new MemoryStream(new byte[] {5, 6, 7, 8}),
new MemoryStream(new byte[] {9, 10, 11, 12})
};
var result = new MemoryStream();
foreach (var stream in list)
stream.CopyTo(result);
var array =result.ToArray();
Console.WriteLine(string.Join(", ", array));
输出
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
您可以使用 VirtualStream 的 IEnumerable 代替 MemoryStream 或 FileStream https://searchcode.com/codesearch/view/14192551/
基本上,当大小超过配置的阈值时,它会自动创建 FileStream。这些文件将在处理后被删除