StreamReader.Read 和 StreamReader.ReadBlock 之间的区别
文档只是说 ReadBlock 是
“Read 的阻塞版本”,
但这是什么意思呢?
之前已经有人问过这个问题了,嗯?
http://www.pcreview.co.uk/forums/thread-1385785.php
回答的人说
基本上,这意味着您可以依赖 StreamReader.ReadBlock,而不是 返回,直到它已按照您的要求阅读完毕,或者 它已到达流的末尾。
我是否正确理解这是必需的,因为 Read 可能无法为您提供您所要求的一切? 仅仅因为它返回 0 并不意味着您到达了文件末尾?
那么这意味着检查返回的字节数(EndOfStream?)以外的其他内容或使用 ReadBlock 代替?
The documentation simply says ReadBlock is
"a blocking version of Read"
but what does that mean?
Someone else has asked the question before but, huh?
http://www.pcreview.co.uk/forums/thread-1385785.php
The guy answering said
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.
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?
So this means check something other than the number of bytes returned (EndOfStream?) or use ReadBlock instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ReadBlock并不意味着它是线程安全的。 如果您使用 Reflector 查看 StreamReader.ReadBlock(继承自 TextReader.ReadBlock)的实现,它所做的就是多次调用“Read”方法,直到“Read”方法返回 0 或我们读取为根据要求的许多字节。 这是必需的,因为“Read”方法不一定返回您要求的字节数。
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.