【发布时间】:2014-05-05 13:19:03
【问题描述】:
我将 MemoryStream 传递给函数 - 这可能最终会变得非常大。然后需要将其拆分为一个小字节 [] 进行处理,然后再添加回流中。在我之前的实现中,我大致有以下内容:
byte[] output = new byte[stream.Length];
output = stream.ToArray();
byte[] processingArray = new byte[16];
int index = 0;
int chunks = output / 16;
for(int i = 0; i < chunks; i++){
for(int x = 0; x < 16; x++){
processingArray[x] = output[x + index];
}
//do the processing on the array here
index += 16;
}
我想避免使用 .ToArray() 因为这对我来说似乎是一种资源浪费。 我将如何从内存流中一次提取 16 个字节,对其进行处理,然后抓取接下来的 16 个字节?
谢谢
【问题讨论】:
-
对于
MemoryStream实例,流的内容已经占用RAM -
顺便说一句:将输出创建为新字节[strem.Length],然后直接通过分配 stream.ToArray() 的结果来删除它没有多大意义。你可以只写 byte[] output = stream.ToArray();
标签: c# memorystream