【Engineering】Buffer

Posted by 西维蜀黍 on 2021-11-09, Last Modified on 2022-12-10

Data Buffer

In computer science, a data buffer (or just buffer) is a region of a physical memory storage used to temporarily store data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a microphone) or just before it is sent to an output device (such as speakers). However, a buffer may be used when moving data between processes within a computer.

Write buffer

A write buffer is a type of data buffer used in certain CPU cache architectures like Intel’s x86 and AMD64.

In a CPU cache, a write buffer can be used to hold data being written from the cache to main memory or to the next cache in the memory hierarchy.

This is a variation of write-through caching called buffered write-through.

Java BufferedWriter Example

To add buffering to a Writer simply wrap it in a Java BufferedWriter. Here is how that looks:

BufferedWriter bufferedWriter = 
    new BufferedWriter(new FileWriter("c:\\data\\output-file.txt"));

This example creates a BufferedWriter which writes characters to a FileWriter. Simple, isn’t it?

BufferedWriter Buffer Size

You can set the buffer size to use internally by the Java BufferedWriter. You provide the size as a constructor parameter, like this:

int bufferSize = 8 * 1024;
    
BufferedWriter bufferedWriter = 
    new BufferedWriter(
        new FileWriter("c:\\data\\output-file.txt"),
            bufferSize);

This example sets the internal buffer of the BufferedWriter to 8 KB. It is best to use buffer sizes that are multiples of 1024 bytes. That works best with most built-in buffering in hard disks etc.

Except for adding buffering to your input streams, BufferedWriter behaves pretty much like a Writer. The BufferedWriter adds one extra method though: The newLine() method which can write a new-line character to the underlying Writer. In addition, you may need to call flush() if you need to be absolutely sure that the characters written until now is flushed out of the buffer and onto the network or disk.

write(int)

The Java BufferedWriter write(int) method writes the lower 16 bit of the int to its internal buffer, as a single character. Here is an example of writing a single character to a Java BufferedWriter:

bufferedWriter.write('A');

write(char[])

The Java BufferedWriter also has a write(char[]) method which can write an array of characters to its internal buffer. The write(char[]) method returns the number of characters actually written to the Writer. Here is an example of writing an array of chars to a Java Writer:

char[] chars = new char[]{'A','B','C','D','E'};
bufferedWriter.write(chars);

Write Performance

It is faster to write an array of characters to a Java BufferedWriter than writing one character at a time. Since the BufferedWriter collects the characters written internally in a buffer before writing them to the underlying Writer, the speedup is not as noticeable as with other Writer classes (that do not use buffering). However, there is still a small speedup.

flush()

The Java BufferedWriter’s flush() method flushes all data written to the BufferedWriter to the underlying data destination. By calling flush() you can assure that any buffered data will be flushed (written) to disk (or network, or whatever else the destination of your BufferedWriter has). Here is an example of flushing data written to a Java BufferedWriter by calling its flush() method:

bufferedWriter.flush();

Reference