StreamReader.Read 和 StreamReader.ReadBlock 之间的区别

发布于 2024-07-14 09:57:27 字数 511 浏览 9 评论 0原文

文档只是说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-07-21 09:57:28

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文