问题描述
我想知道的情况下读取(的char [],INT,INT)未能返回所要求的所有字符,而读出数据块()如预期返回所有字符(比如当的StreamReader可以与FileStream对象的实例)。
I would like to know of a situation Read(char[],int,int) fails to return all chars requested while ReadBlock() returns all chars as expected (say when StreamReader works with an instance of a FileStream object).
推荐答案
在实践中,当使用一个的StreamReader
它只是可能有可能在一段时间延迟流发生 - 例如:因为人们已经在这里所说的网络流。
In practice, when using a StreamReader
it is only likely to happen with a stream which may delay for some time - e.g. a network stream as people have mentioned here.
然而,在一个的TextReader
通常,你可以期望它发生它不以的StreamReader
发生在支持<$ C - 在任何时间(使用.NET的情况下未来的版本它目前不发生可能包括$ C>的FileStream 未记录,因此也不能保证它不会在未来发生)。
However, with a TextReader
generally, you can expect it to happen at any time (including possibly with a future version of .NET in a case where it doesn't currently happen - that it doesn't happen with StreamReader
backed on FileStream
isn't documented, so there's no guarantee it won't happen in the future).
在特别有很多在那里它更容易(对更简单,更可靠,可能更有效的代码效果爆震)为实施者不返回所请求的金额,如果他们可以部分完成呼叫只需清空当前的缓冲区,或者通过使用例请求为量量传递到做一个手术前后备源(流或另一个的TextReader
),只能回到一个较低的数字字符。
In particular there are a lot of cases where it's easier (with a knock on effect of simpler, more reliable and probably more efficient code) for the implementer to not return the requested amount if they can partially fulfil the call with just emptying the current buffer, or by using the amount requested as the amount to pass to a backing source (a stream or another TextReader
) before doing an operation that could only return a lower number of characters.
现在,回答这个问题的实际,以当使用StreamReader.ReadBlock()? (或者更一般地,当使用TextReader.ReadBlock())。下面应该牢记:
Now, to answer the actual question as to "When to use StreamReader.ReadBlock()?" (or more generally, when to use TextReader.ReadBlock()). The following should be borne in mind:
-
两者
阅读()
和读出数据块()
保证,除非整个源已被阅读至少返回一个字符。当有一个未决的内容也不会返回0。
Both
Read()
andReadBlock()
are guaranteed to return at least one character unless the entire source has been read. Neither will return 0 if there is pending content.
调用读出数据块()
在阅读()
将做的是浪费,因为它可以将不必要的。
Calling ReadBlock()
when Read()
will do is wasteful, as it loops needlessly.
但在另一方面,它不是的该的浪费。
But on the other hand it's not that wasteful.
但在第三个方面,在哪些情况下阅读()
将返回比较少请求字符常常哪里另一个线程被接合在得到将填充缓冲器的下一个呼叫的内容的情况下,或在该内容尚不存在(例如用户输入或另一台机器上的待处理操作) - 有一个在处理部分结果,然后调用阅读()
时,再次证明的完成。
But on the third hand, the cases where Read()
will return fewer than requested characters are often cases where another thread is engaged in getting the content that will fill the buffer for the next call, or where that content doesn't exist yet (e.g. user input or a pending operation on another machine) - there's better overall concurrency to be found in processing the partial result and then calling Read()
again when that's finished.
所以。如果你能做些有用的部分结果,然后调用阅读()
和你得到了什么工作。特别是如果你是通过循环和每个结果工作阅读()
然后做到这一点,而不是与读出数据块()
。
So. If you can do something useful with a partial result, then call Read()
and work on what you get. In particular if you are looping through and working on the result of each Read()
then do this rather than with ReadBlock()
.
一个值得注意的情况是,如果您正在构建自己的TextReader由另一个支持。有没有点呼叫读出数据块()
除非该算法确实需要一定数目的字符的工作 - 只返回尽可能多的,你可以从一个电话阅读()
,让调用代码调用读出数据块()
是否需要。
A notable case is if you are building your own TextReader that is backed by another. There's no point calling ReadBlock()
unless the algorithm really needs a certain number of characters to work - just return as much as you can from a call to Read()
and let the calling code call ReadBlock()
if it needs to.
在特别需要注意下面的代码:
In particular, note that the following code:
char buffer = char[4096];
int len = 0;
while((len = tr.ReadBlock(buffer, 0 , 4096)) != 0)
DoSomething(buffer, 0, len);
可以改写为:
Can be rewritten as:
char buffer = char[4096];
for(int len = tr.Read(buffer, 0, 4096); len != 0; len = tr.Read(buffer, 0, 4096))
DoSomething(buffer, 0, len);
它可以调用 DoSomething的()
较小大小有时,但它也可以有更好的并发性,如果有参与提供下一次调用的数据阅读()
另一个线程。
It may call DoSomething()
with smaller sizes sometimes, but it can also have better concurrency if there's another thread involved in providing the data for the next call to Read()
.
然而,增益是在大多数情况下主要。如果你确实需要有一定数目的字符,然后做电读出数据块()
。最重要的是在这些情况下,阅读()
将有相同的结果为读出数据块()
的开销读出数据块()
检查它这样做是的很轻微。不要试图猜测是否阅读()
是在特定情况下的安全与否;如果需要读出数据块()
然后用读出数据块()
。
However, the gain is not major in most cases. If you really need a certain number of characters, then do call ReadBlock()
. Most importantly in those cases where Read()
will have the same result as ReadBlock()
the overhead of ReadBlock()
checking that it has done so is very slight. Don't try to second-guess whether Read()
is safe or not in a given case; if it needs the guarantee of ReadBlock()
then use ReadBlock()
.
这篇关于何时使用StreamReader.ReadBlock()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!