我目前有memorystream,其长度约为30000
(Named memStream here
)
我希望使用以下代码chunks
read {(3}}内存流(我在网上选择并稍微修改):
byte[] chunk = new byte[4096];
bool hasNext = true;
while(hasNext)
{
int index = 0;
while (index < chunk.Length)
{
int bytesRead = memStream.Read(chunk, index, chunk.Length - index);
if (bytesRead == 0)
{
break;
}
index += bytesRead;
//Do something with this chunk
}
if (index != 0) // Our previous chunk may have been the last one
{
//Do something with the last chunk
}
if (index != chunk.Length) // We didn't read a full chunk: we're done
{
hasNext = false;
}
}
但以下read()
方法似乎无法正常工作
int bytesRead = memStream.Read(chunk, index, chunk.Length - index);
WHERE
chunk: new byte[4096]
index: 0
memstream: capacitiy & length : 34272
memstream: position 0 (according to VS watch)
Always returns
0 bytesRead
Chunk with all values containing '0'
知道为什么吗?这可能是权利许可吗?
感谢您的时间。
答案 0 :(得分:6)
创建并填充MemoryStream之后,需要将读取位置设置为开头,如下所示:
memStream.Seek(0, SeekOrigin.Begin);