Java BufferedInputStream class is a subclass of InputStream that adds buffering functionality to improve I/O performance. It reads data from an input stream and stores it in an internal buffer, making it available for efficient reading by applications. The BufferedInputStream class is used when we need to read a large amount of data from an input stream in a more efficient manner.
To use the BufferedInputStream class, you need to create an instance of it by passing an existing InputStream object to its constructor. You can then use the read() method of the BufferedInputStream class to read data from the input stream. The read() method returns an int value representing the next byte of data, or -1 if the end of the stream has been reached.
Here is an example code snippet that demonstrates how to use the BufferedInputStream class to read data from an input stream:
import java.io.*; public class BufferedInputStreamExample { public static void main(String[] args) { try { InputStream input = new FileInputStream("input.txt"); BufferedInputStream buffer = new BufferedInputStream(input); int data = buffer.read(); while(data != -1) { System.out.print((char) data); data = buffer.read(); } buffer.close(); } catch (IOException e) { e.printStackTrace(); } } }
In this example, we create an InputStream object to read data from a file named “input.txt”. We then create a BufferedInputStream object and pass the InputStream object to its constructor. We use the read() method to read data from the BufferedInputStream object and print it to the console until the end of the stream is reached. Finally, we close the BufferedInputStream object to release any system resources associated with it.
Java BufferedInputStream class declaration:
The Java BufferedInputStream class is declared in the java.io package and has the following class signature:
public class BufferedInputStream extends FilterInputStream
The BufferedInputStream class extends the FilterInputStream class, which is itself a subclass of the InputStream class. This means that the BufferedInputStream class inherits all the methods of its parent classes, as well as adding its own unique functionality.
The constructors of the BufferedInputStream class are:
BufferedInputStream(InputStream in)
: Creates a new BufferedInputStream and reads data from the specified input stream.BufferedInputStream(InputStream in, int size)
: Creates a new BufferedInputStream with the specified buffer size and reads data from the specified input stream.
The methods of the BufferedInputStream class include:
int available()
: Returns the number of bytes that can be read from the input stream without blocking.void close()
: Closes the input stream and releases any system resources associated with it.void mark(int readlimit)
: Sets a mark position in the input stream up to a certain read limit.boolean markSupported()
: Returns true if this input stream supports the mark and reset methods.int read()
: Reads the next byte of data from the input stream and returns it as an integer value.int read(byte[] b, int off, int len)
: Reads up to len bytes of data from the input stream into an array of bytes starting at the specified offset off.void reset()
: Resets the input stream to the last marked position.long skip(long n)
: Skips over and discards n bytes of data from the input stream.
The BufferedInputStream class is a useful tool for improving the performance of I/O operations in Java programs that involve reading from input streams.
Java BufferedInputStream class constructors:
The Java BufferedInputStream class provides two constructors for creating a BufferedInputStream object:
BufferedInputStream(InputStream in)
This constructor creates a BufferedInputStream object and reads data from the specified input stream. It sets the default buffer size (8192 bytes) for the buffer.BufferedInputStream(InputStream in, int size)
This constructor creates a BufferedInputStream object and reads data from the specified input stream. It allows you to specify the size of the buffer in bytes.
Here’s an explanation of the two constructors in more detail:
BufferedInputStream(InputStream in)
This constructor creates a BufferedInputStream object and sets the default buffer size of 8192 bytes. It takes an InputStream object as a parameter. The InputStream object can be any subclass of the InputStream class, such as FileInputStream or ByteArrayInputStream.
For example, the following code creates a BufferedInputStream object that reads data from a FileInputStream object:
InputStream input = new FileInputStream("file.txt"); BufferedInputStream buffer = new BufferedInputStream(input);
BufferedInputStream(InputStream in, int size)
This constructor creates a BufferedInputStream object and allows you to specify the size of the buffer in bytes. It takes two parameters: an InputStream object and an integer value that represents the buffer size in bytes.
For example, the following code creates a BufferedInputStream object that reads data from a FileInputStream object with a buffer size of 4096 bytes:
InputStream input = new FileInputStream("file.txt"); BufferedInputStream buffer = new BufferedInputStream(input, 4096);
In general, it is recommended to use a buffer size that is a power of 2, as this can improve the performance of the BufferedInputStream object.
Java BufferedInputStream class methods:
The Java BufferedInputStream class provides several methods for reading and manipulating the data in the buffer. Here are some of the most commonly used methods:
int read()
: Reads the next byte of data from the input stream and returns it as an integer value. If the end of the stream has been reached, it returns -1.int read(byte[] b, int off, int len)
: Reads up to len bytes of data from the input stream into an array of bytes starting at the specified offset off. Returns the total number of bytes read, or -1 if the end of the stream has been reached.void mark(int readlimit)
: Sets a mark position in the input stream up to a certain read limit. If the read limit is exceeded, the mark becomes invalid.boolean markSupported()
: Returns true if this input stream supports the mark and reset methods.void reset()
: Resets the input stream to the last marked position.long skip(long n)
: Skips over and discards n bytes of data from the input stream. Returns the actual number of bytes skipped.int available()
: Returns the number of bytes that can be read from the input stream without blocking.void close()
: Closes the input stream and releases any system resources associated with it.
The methods of the BufferedInputStream class allow you to read data from the input stream in a more efficient manner by buffering the data in memory. The mark() and reset() methods allow you to set a position in the input stream and return to it later, which can be useful when you need to re-read the same data multiple times. The skip() method allows you to skip over unwanted data in the input stream, which can improve performance in some cases.
Overall, the BufferedInputStream class is a powerful tool for improving the performance of I/O operations in Java programs that involve reading from input streams.
Example of Java BufferedInputStream:
Here’s an example of how to use the Java BufferedInputStream class to read data from a file:
import java.io.*; public class BufferedInputStreamExample { public static void main(String[] args) { try { InputStream input = new FileInputStream("file.txt"); BufferedInputStream buffer = new BufferedInputStream(input); byte[] data = new byte[1024]; int bytesRead; while ((bytesRead = buffer.read(data, 0, 1024)) != -1) { // Process the data here } buffer.close(); } catch (IOException e) { e.printStackTrace(); } } }
In this example, we create a FileInputStream object to read data from a file called “file.txt”. We then create a BufferedInputStream object and pass the FileInputStream object to it.
We also create a byte array called “data” with a size of 1024 bytes. We use the read() method of the BufferedInputStream object to read up to 1024 bytes of data into the “data” array. We repeat this process until the end of the input stream is reached (i.e., the read() method returns -1).
Finally, we close the BufferedInputStream object to release any system resources associated with it.
Note that in a real-world application, you would typically process the data read from the input stream inside the while loop, rather than just commenting it out as shown in the example.