Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c# - Difference between StreamReader.Read and StreamReader.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...