How does a buffer stream work?

In Java and C #, there are several classes for buffering streams: BufferedStream in C #, Buffered(Input|Output)Stream and Buffered(Reader|Writer) .

They get some thread in the constructor and implement the same interface.

The question is, how does it work?

What happens when I try to read one byte? Does it read many bytes into the internal buffer and then return it in bytes after the byte? Writing a single byte? Writes to the internal buffer and writes to flush() to the internal stream?

And about reading / writing an array of bytes - is it inefficient to do this on buffered streams, which causes double copying of bytes to and from the internal array?

+8
java c # stream encapsulation buffer
source share
1 answer

Does it read many bytes into the internal buffer and then return it in bytes after the byte?

I guess, yes. It takes time to request data from disk tablets or from a TCP stream, so it may be more efficient to get the entire fragment of bytes at once, rather than trying to extract them separately from the source.

+2
source share

All Articles