Java - BufferedInputStream Class



Introduction

The Java BufferedInputStream class adds functionality to another input stream, the ability to buffer the input and to support the mark and reset methods. Following are the important points about BufferedInputStream −

  • When the BufferedInputStream is created, an internal buffer array is created.

  • As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.

Class declaration

Following is the declaration for Java.io.BufferedInputStream class −

public class BufferedInputStream
   extends FilterInputStream

Field

Following are the fields for Java.io.BufferedInputStream class −

  • protected byte[] buf − This is the internal buffer array where the data is stored.

  • protected int count − This is the index one greater than the index of the last valid byte in the buffer.

  • protected int marklimit − This is the maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail.

  • protected int markpos − This is the value of the pos field at the time the last mark method was called.

  • protected int pos − This is the current position in the buffer.

  • protected InputStream in − This is the input stream to be filtered.

Class constructors

Sr.No. Constructor & Description
1

BufferedInputStream(InputStream in)

This creates a BufferedInputStream and saves its argument, the input stream in, for later use.

2

BufferedInputStream(InputStream in, int size)

This creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Class methods

Sr.No. Method & Description
1 int available()

This method 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.

2 void close()

This method closes this input stream and releases any system resources associated with the stream.

3 void mark(int readlimit)

This method see the general contract of the mark method of InputStream.

4 boolean markSupported()

This method tests if this input stream supports the mark and reset methods.

5 int read()

This method reads the next byte of data from the input stream.

6 int read(byte[] b, int off, int len)

This method reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

7 void reset()

This method repositions this stream to the position at the time the mark method was last called on this input stream.

8 long skip(long n)

This method skips over and discards n bytes of data from this input stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.FilterInputStream
  • Java.io.Object

Examples

Here are the examples of how to use the BufferedInputStream class in Java, each demonstrating a different aspect of its functionality−

We've used a "example.txt" file with following content−

Welcome to tutorialspoint.com

Example - Reading data from a file using BufferedInputStream

This example demonstrates how to read data from a file using a BufferedInputStream, which provides an efficient way to read bytes in larger chunks.

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamDemo {

   public static void main(String[] args) {
      try {
         // Create a FileInputStream to read the file
         FileInputStream fileInputStream = new FileInputStream("example.txt");
         // Wrap the FileInputStream in a BufferedInputStream
         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
         int byteData;
         // Read bytes from the file and print them as characters
         while ((byteData = bufferedInputStream.read()) != -1) {
            System.out.print((char) byteData); // Convert byte to char and print
         }
         // Close the stream
         bufferedInputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Welcome to tutorialspoint.com

Explanation

  • This program reads a file example.txt using a BufferedInputStream wrapped around a FileInputStream.

  • It reads one byte at a time and prints it as a character.

  • The buffer improves efficiency by reducing the number of I/O operations.

Example - Reading data in chunks (using read(byte[] buffer) method)

This example shows how to read data into a byte array, which is more efficient than reading one byte at a time.

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamDemo {

   public static void main(String[] args) {
      try {
         // Create a FileInputStream to read the file
         FileInputStream fileInputStream = new FileInputStream("example.txt");
         // Wrap the FileInputStream in a BufferedInputStream
         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
         byte[] buffer = new byte[1024]; // 1 KB buffer
         int bytesRead;
         // Read the data in chunks and print each chunk
         while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
            System.out.write(buffer, 0, bytesRead); // Write the chunk to output
         }
         // Close the stream
         bufferedInputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Welcome to tutorialspoint.com

Explanation

  • Instead of reading one byte at a time, this example uses a buffer of 1024 bytes.

  • The read(byte[] buffer) method fills the byte array and returns the number of bytes read.

  • The program efficiently handles large files by reading them in chunks.

Example - Skipping bytes using skip() method

This example demonstrates how to skip a specified number of bytes when reading from a file with a BufferedInputStream.

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Create a FileInputStream to read the file
         FileInputStream fileInputStream = new FileInputStream("example.txt");
         // Wrap the FileInputStream in a BufferedInputStream
         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
         // Skip the first 10 bytes of the file
         bufferedInputStream.skip(10);
         int byteData;
         // Read the rest of the file after skipping
         while ((byteData = bufferedInputStream.read()) != -1) {
            System.out.print((char) byteData); // Print the remaining characters
         }
         // Close the stream
         bufferedInputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

 tutorialspoint.com

Explanation

  • The skip(long n) method allows the program to skip over the first 100 bytes of the file before starting to read the rest of the content.

  • This can be useful when you want to ignore a portion of the data (e.g., headers in a file).

In all examples, BufferedInputStream is used to optimize I/O performance by buffering input data.

Advertisements