System.IO.Stream.Read(byte[], int, int) 的阻塞版本

3
我正在使用System.IO.Stream.Read(byte[] buffer, int offset, int count)方法。是否有替代该方法的方式(或设置属性),使该方法不会返回,直到读取完所有的count(或到达流的末尾)?或者我应该像这样做:
int n = 0, readCount = 0;
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
    readCount += n;
1个回答

9
BinaryReader.ReadBytes 方法以所需的方式读取字节块。但这并不等同于读取流的末尾。(您不想调用 BinarReader.ReadBytes(int.MaxValue) - 它会尝试创建一个2GB的缓冲区!) 通常我会使用MemoryStream来读取未知大小的流中的所有数据。请参见相关问题以获取示例代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接