StreamReader.Read和StreamReader.ReadBlock的区别 [英] Difference between StreamReader.Read and StreamReader.ReadBlock

查看:2139
本文介绍了StreamReader.Read和StreamReader.ReadBlock的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该文件只是说读出数据块是

The documentation simply says ReadBlock is

读阻塞版

不过这是什么?意味着

别人一直问这个问题之前,但,呵呵

Someone else has asked the question before but, huh?

http://www.pcreview.co.uk/forums/thread-1385785.php

这家伙回答说。

基本上,这意味着你可以依靠StreamReader.ReadBlock没有
返回,直到它的多读你问它,或
它到达流的末尾。

Basically, it means that you can rely on StreamReader.ReadBlock not returning until either it's read as much as you've asked it to, or it's reached the end of the stream.

我是否理解正确,由于读取可能不会给你你要的一切这是必需的?而这只是因为它返回0并不意味着你到达了文件的末尾?

Am I understanding correctly that this is required because Read may not give you everything you asked for? And that just because it returns 0 does NOT mean you reached the end of the file?

因此​​,这意味着检查不是字节数等东西回来(EndOfStream?)或用读出数据块呢?

So this means check something other than the number of bytes returned (EndOfStream?) or use ReadBlock instead?

推荐答案

读出数据块并不意味着它是线程安全的。如果您使用反射来看看StreamReader.ReadBlock(这是从TextReader.ReadBlock继承)的实施,它是所有多次调用读取的方法直到读取方法返回0或我们读作多少个字节的要求。这是必要的,因为读的方法不一定返回的字节数,你提出的要求。

ReadBlock does not mean it is thread safe. If you use Reflector to look at the implementation of StreamReader.ReadBlock (which is inherited from TextReader.ReadBlock), all it does is make multiple calls to the "Read" method until either the "Read" method returns 0 or we have read as many bytes as requested. This is needed because the "Read" method will not necessarily return as many bytes as you asked for.

这篇关于StreamReader.Read和StreamReader.ReadBlock的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆