It is a block of memory used to hold data temporarily. It is always located between a peripheral device and a faster computer. Buffers are used to pass data between computer and devices. The buffer increases the performance of the computer by allowing read/write operation in larger chunks. For example, if the size of the buffer is N, the buffer can hold N number of bytes of data.
Writing data to the disk is very costly. The read/write operation with disk takes a long time. When these operations are carried out, the execution of the program will be slow for few seconds. If the program involves many read and write operations, the program execution speed will be slower. To avoid this problem, the stream provides buffer. Buffer holds the data temporarily. The input data is passed to the stream buffer. The data is not written to the disk immediately till the buffer fills. When the buffer is completely filled, the data is written to the disk. This is explained with an example in Figure.
Working of buffer
In Figure (a), the level of the steam in the pressure cooker is increasing. The steam is not full, hence the whistle will not blow, and the steam will not be released from the pressure cooker.
In Figure (b), the pressure cooker is completely filled. The level of the steam reaches to the top. Due to high-pressure the whistle blows automatically and the steam comes out of the pressure cooker.
After the steam comes out the whistle falls down. Even when the cooker is not completely filled with steam, the steam can be released manually by pulling up the whistle. It is just like flushing the buffer even when the buffer is not completely filled.