Memorystream.Read()总是返回0字节Read,字节[]为空
本文关键字:Read 字节 为空 0字节 返回 Memorystream | 更新日期: 2023-09-27 18:30:08
我当前有一个长度约为30000
(Named memStream here
)的内存流我希望使用以下代码在chunks
中读取此内存流(我在网上找到并进行了一些修改):
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'
知道为什么吗?这可能是权限许可吗?
谢谢你抽出时间。
创建并填充MemoryStream后,您需要将读取位置设置为开头,如下所示:
memStream.Seek(0, SeekOrigin.Begin);