Introduction
A Stream is a logical connection to exchange a sequence of bytes between a program and a file. Java uses this concept to make input and output operations faster. InputStream can read data from a source, while OutputStream can write data into a destination file. Sometimes, buffers can store the data to be transferred, thereby making the process more efficient. This buffer mechanism in I/O streams can be implemented using the BufferedInput/OutputStream classes in Java. Let’s learn more about these classes in detail.
Must Read, Multithreading in java, Duck Number in Java
BufferedInputStream
The BufferedInputStream class reads data from a stream and uses an internal buffer to store the bytes of data to be read or skipped. This internal buffer array is created along with the BufferedInputStream. The size of the internal buffer is approximately 8192 bytes.
During a read operation, chunks of bytes are stored from the disc to the internal buffer array, from where they are read individually. This significantly saves the amount of time used to contact to disc.
The BufferedInputStream class is a part of the java.io package and extends the InputStream abstract class.
Declaration:
public class BufferedInputStream extends FilterInputStream
Constructors
-
BufferedInputStream (InputStream in)
-
It creates a BufferedInputStream and saves the given InputStream argument for later use.
-
It creates a BufferedInputStream and saves the given InputStream argument for later use.
BufferedInputStream b_in = new BufferedInputStream(InputStream in);
-
BufferedInputStream (InputStream in, int size)
-
It creates a BufferedInputStream with specific buffer size and saves the InputStream argument for later use.
-
It creates a BufferedInputStream with specific buffer size and saves the InputStream argument for later use.
BufferedInputStream b_in = new BufferedInputStream(InputStream in, int size);
Methods
Method |
Description |
int available() | It returns the estimated number of bytes that can be read or skipped over from this input stream without blocking calls from another method that requests this InputStream. |
void close() | It closes the InputStream and releases all the resources associated with it. |
void mark (int readLimit) | It marks a point in the Input stream. |
Boolean markSupported() | It checks if the input stream supports the mark and reset functions. |
int read() | It reads the next byte of data from the Input stream. |
int read (byte b[ ], int off, int len) | It reads a given number of bytes from the Input stream and stores it in the given byte array. |
void reset() | It resets the position of the stream to the initial one mentioned by the most recent mark function. |
long skip(long n) | It skips over and discards n bytes of data from the Input stream. |
Example
Assume ‘hello world!!!’ to be the contents of a file named read.txt present in the current working directory.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("read.txt");
// Creates a BufferedInputStream
BufferedInputStream b_in = new BufferedInputStream(file);
System.out.println(b_in.markSupported());
System.out.println(b_in.available());
// Read first byte
int i = b_in.read();
while (i != -1) {
System.out.print((char) i);
// Reads next byte from the file
i = b_in.read();
}
b_in.close();
}
catch (Exception e) {
System.out.println("Error");
}
}
}
Output:
true
13
hello world!!!
Practice by yourself on java online compiler.