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

问题描述

文档只是说 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 而不是返回,直到读到你要求的次数,或者它已到达流的末尾.

我是否正确理解这是必需的,因为 Read 可能无法提供您所要求的一切?并且仅仅因为它返回 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?)还是使用 ReadBlock?

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

推荐答案

ReadBlock 并不意味着它是线程安全的.如果您使用 Reflector 查看 StreamReader.ReadBlock(继承自 TextReader.ReadBlock)的实现,它所做的就是多次调用Read"方法,直到Read"方法返回 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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 02:30