Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Buffered Streams works?

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

They gets some stream in constructor and implements the same interface.

The question is - how it works?

What happens, when I'm trying to read one byte? It reads a lot of bytes into inner buffer and then returns it to me byte after byte? On writing one byte? Writes into inner buffer and on flush() writes it to inner stream?

And about reading/writing an array of bytes - is it inefficient to do it on buffered streams cause of double copying bytes into and from inner array?

like image 448
VorobeY1326 Avatar asked May 17 '13 15:05

VorobeY1326


People also ask

What happens when we use a buffered stream instead of a normal stream?

Internally a buffer array is used and instead of reading bytes individually from the underlying input stream enough bytes are read to fill the buffer. This generally results in faster performance as less reads are required on the underlying input stream.

What is a buffered output stream?

BufferedOutputStream(OutputStream out) Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

What is the best buffer size for streaming?

A buffer size less than 0.5 s results in a severe loss of QoE for all users. A buffer size of 2-4 s offers a good QoE for the average user and any sensitive user. Increasing the buffer size further decreases the QoE.

What is a buffered stream in C?

12.20 Stream Buffering Similarly, streams often retrieve input from the host environment in blocks rather than on a character-by-character basis. This is called buffering.


1 Answers

It reads a lot of bytes into inner buffer and then returns it to me byte after byte?

Basically, yes. It takes time to request data from disk platters or from a TCP stream, so it can be more efficient to get a whole chunk of bytes at once, rather than trying to retrieve them individually from the source.

like image 162
Robert Harvey Avatar answered Sep 18 '22 17:09

Robert Harvey