Java BufferedInputStream Class
BufferedInputStream Class in Java
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. The internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
Buffering I/O is a very common performance optimization. Java’s BufferedInputStream class allows you to "wrap" any InputStream into a buffered stream to improve performance.
In this tutorial you can learn about Java BufferedInputStream class. And how to use Java BufferedInputStream class examples.
What is Java BufferedInputStream Class ?
Java BufferedInputStream class is used to read data bytes from input stream. Class BufferedInputStream is since JDK 1.0 it is also work in Java 8 and above.
What are the Constructors of Java BufferedInputStream Class ?
Following are the constructors of BufferedInputStream class.
Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size)
Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.
Methods of class BufferedInputStream
- available() : Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
- close() : Closes this input stream and releases any system resources associated with the stream.
- mark(int readlimit) : Marks the current position in this input stream.
- markSupported() : Tests if this input stream supports the mark and reset methods.
- read() : Reads the next byte of data from the input stream.
- read(byte b, int off, int len) : Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
- reset() : Repositions this stream to the position at the time the mark method was last called on this input stream.
- skip(long n) : Skips over and discards n bytes of data from this input stream.
java.io.BufferedInputStream class Example
The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.
The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".
java.io.BufferedInputStream class Example 2
How to read total bytes from file into array using java BufferedInputStream