开发者

What does the FillBuffer method of BinaryReader do?

According to the documentation:

Fills the internal buffer with the specified number of bytes rea开发者_如何学JAVAd from the stream.

What does this mean (what's the internal buffer?)?


The BinaryReader has an internal buffer so that it doesn't need to perform as many small reads on the underlying stream, especially also when reading character data which may need some lookahead. You shouldn't need to call this manually.


Notice that the method is declared as protected.

As such, it is only of interest if you want to create a class that inherits from BinaryReader, which you seldom need to do.


It looks like the main aim here is to allow you to have a convenient method to ensure that you have a block of data locally; for example, when reading a "double" you would (typically) want 8 bytes. This method wraps up:

  • checking if the internal buffer already has enough
  • looping over 'Read' as necessary
  • checking for EOF (and erroring)
  • guarding for overrun
  • handling buffer-management, such as either block-copying the data backwards periodically, or handling the various indices for a cyclic buffer

However, it seems unlikely you would need to call it externally, unless you were reading a small 'byte[]'

As for the internal buffer; simply, when deserializing you:

  • want to minimise calls to "Read"
  • often need to peek-ahead more than 1 byte (tricky without a buffer)
  • regularly want operations on small 'byte[]' (via BitConverter, for example)

So just work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜