Buffered IO Stream


Buffered IO Stream

Buffered IO Streams:
Buffered IO Streams are a type of Filter IO Stream, which provides buffering functionality over a primary Java IO Stream. Buffered IO Streams caches chunk of data in memory and services API calls by reading or writing into the Memory. Hence Buffered IO Streams reduces the number of System Calls there by increasing the performance.




Creating a BufferedInputStream
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("fileinputstream.txt"), 2);
bufferedInputStream.read();

Buffered IO Streams provide us API to mark a position in the buffer with a read limit, so that one could return to that marked position as long as the number of READ performed after the mark is less than the read limit.


BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("fileinputstream.txt"), 2);
// Marking with a read-limit of 4
bufferedInputStream.mark(4);
bufferedInputStream.read();
bufferedInputStream.read();
bufferedInputStream.read();
bufferedInputStream.read();
bufferedInputStream.read();
// Five READ has been performed since Marked with a read-limit of '4'
// Hence calling a BufferedInputStream.reset() will have no effect.
bufferedInputStream.reset();

Please note: The Contract says that the READ-LIMIT will only be honored if there is crunch in buffer space such that new data is READ into the end of Buffer. Else the READ-LIMIT will not be honored and one will be allowed to return to the Marked position even after crossing the READ-LIMIT.  

<< Filter IO Stream Array IO Stream >>

No comments:

Post a Comment