Memorystream.Read()始终返回0个字节,其中空字节为[] [英] Memorystream.Read() always returns 0 bytesRead with empty byte[]
本文介绍了Memorystream.Read()始终返回0个字节,其中空字节为[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前有一个 memorystream 长度约为 30000
(在此命名为memStream
)
我希望使用以下代码在块
中读取该内存流(我在网上捡起并进行了一些修改): / p>
I currently have a memorystream with length of about 30000
(Named memStream here
)
I wished to read this memorystream in chunks
using the following code (I picked up on the net and modified somewhat):
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( )
方法似乎不起作用
yet the following read()
method doesn't appear to be working
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后,需要将读取位置设置为开头,如下所示:
After creating and filling the MemoryStream, you need to set the read position to the begining like so:
memStream.Seek(0, SeekOrigin.Begin);
这篇关于Memorystream.Read()始终返回0个字节,其中空字节为[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文