2008-12-01 25 views
3

我正在使用System.IO.Stream.Read(byte[] buffer, int offset, int count)。是否有替代方法(或设置属性),以便该方法不会返回,直到读取所有计数(或达到流结束)?或者我应该这样做:阻止版本的System.IO.Stream.Read(byte [],int,int)

int n = 0, readCount = 0; 
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0) 
    readCount += n; 

回答

9

BinaryReader.ReadBytes以所需的方式块。但是,这不等同于阅读流的末尾。 (您不想拨打BinarReader.ReadBytes(int.MaxValue) - 它会尝试创建2GB缓冲区!)

我倾向于使用MemoryStream从未知大小的流中读取所有数据。有关示例代码,请参见this related question

相关问题