Question

According to the documentation:

Fills the internal buffer with the specified number of bytes read from the stream.

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top