Java BufferedInputStream Class

input stream

Java BufferedInputStream class is a powerful tool for improving IO performance in Java.When working with input streams, it is crucial to ensure optimal performance, especially when dealing with large amounts of data.

In this article, we will explore what the Java BufferedInputStream class is, how it works, and how you can use it to improve your IO performance in Java.

What is Java BufferedInputStream Class?

Java BufferedInputStream class is a subclass of the InputStream class that provides a buffered input stream. It reads bytes from an input stream and stores them in a buffer.

By doing so, it reduces the number of system calls required to read data from the underlying input stream.

The BufferedInputStream class reads data from the underlying input stream in chunks and stores it in a buffer, which can be accessed later.

Reading Large Files

When reading large files, it is crucial to ensure optimal performance. The BufferedInputStream class can be used to read data from a file in chunks, which can significantly reduce the amount of time required to read the file.

try (InputStream inputStream = new FileInputStream(“file.txt”);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
            // process the data
        }
} catch (IOException ex) {
    // handle exception
}

Reading Data from Network Streams

When reading data from network streams, it is essential to ensure that your program can handle slow or intermittent connections. The BufferedInputStream class can be used to read data from network streams in chunks, which can help ensure that your program remains responsive even when the network connection is slow. Here is an example:

try (Socket socket = new Socket("localhost", 8080);
    InputStream inputStream = socket.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
            // process the data
        }
} catch (IOException ex) {
    // handle exception
}

Advantages of using the BufferedInputStream class in Java

Java Input Stream

Example 1: Java program to show java BufferedInputStream class

Run
import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            // Create a BufferedInputStream to read from a file
            InputStream in = new BufferedInputStream(new FileInputStream("example.txt"));

            // Read data from the file and print it to the console
            int data;
            while ((data = in.read()) != -1) {
                System.out.print((char)data);
            }

            // Close the BufferedInputStream
            in.close();
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Output

An error occurred: example.txt (No such file or directory)

Example 2: Java program to show java BufferedInputStream class

 
Run
import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            // Create a BufferedInputStream to read from System.in
            InputStream in = new BufferedInputStream(System.in);

            // Read input from the user and print it to the console
            byte[] buffer = new byte[1024];
            int length;
            System.out.print("Enter some text: ");
            while ((length = in.read(buffer)) != -1) {
                System.out.print(new String(buffer, 0, length));
                if (length < buffer.length) {
                    break;
                }
            }

            // Close the BufferedInputStream
            in.close();
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Output

Enter some text: 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription