BufferedInputStream BufferedOutputStream

BufferedInputStream BufferedOutputStream

Using Buffers to Enhance the Performance

Buffering of streams increases the performance to higher extents of few thousand times. For buffering, two streams exist in java.io package in byte streams category – BufferedInputStream and BufferedOutputStream. These high-level classes are subclasses of FilterInputStream and FilterOutputStream and their functionality is to increase the performance with buffer. These streams give an implict system-defined buffer (generally, 2048 bytes) into which data is read and written. The buffer decreases the number of transfers between source file context area and destination file context area and thereby performance increases.

File Copying Using Buffer Streams: BufferedInputStream BufferedOutputStream

The following program uses high-level streams BufferedInputStream BufferedOutputStream to increase the performance.

BufferedInputStream BufferedOutputStream
Output screen on BufferedInputStream BufferedOutputStream

FileInputStream fistream = new FileInputStream("pqr.txt");
BufferedInputStream bistream = new BufferedInputStream(fistream);

For greater performance, the BufferedInputStream constructor is passed (chained) with an object of FileInputStream, fistream.

FileOutputStream fostream = new FileOutputStream("xyz.txt");
BufferedOutputStream bostream = new BufferedOutputStream(fostream);

Similarly, the BufferedOutputStream constructor is passed (chained) with an object of FileOutputStream, fostream.

How buffers increase performance?

A buffer works as a reservoir to store data. A buffer stands in between an input stream and output stream. The data is read and put in the buffer instead of sending immediately. When the buffer is full, the buffer is transferred. This decreases the number of execution control shiftings between input and output streams and thereby performance increases.

The size of buffer allocated depends on the underlying operating system. The size of buffer can be requested explicitly using overloaded constructor of BufferedInputStream as follows.

BufferedInputStream bistream = new BufferedInputStream(fistream, 6000);

In the above statement, a buffer is 6000 bytes is allocated by the OS.

One thought on “BufferedInputStream BufferedOutputStream

Leave a Reply

Your email address will not be published. Required fields are marked *