Java - BufferedInputStream available() method



Description

The Java BufferedInputStream available() method returns the number of bytes remained to read from an input stream without blocking by the next invocation of a method for this input stream.

Declaration

Following is the declaration for java.io.BufferedInputStream.available() method.

public int available()

Return Value

This method returns number of bytes remained to read from this input stream without blocking.

Exception

IOException − If an I/O error occurs.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDE 

Example - Checking if data is available

The following example shows the usage of Java BufferedInputStream available() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      BufferedInputStream bis = null;
      FileInputStream inStream = null;

      try {
         // open input stream example.txt for reading purpose.
         inStream = new FileInputStream("example.txt");

         // input stream is converted to buffered input stream
         bis = new BufferedInputStream(inStream);      

         // read until a single byte is available
         while( bis.available() > 0 ) {
            
            // get the number of bytes available
            Integer nBytes = bis.available();
            System.out.println("Available bytes = " + nBytes );

            // read next available character
            char ch =  (char)bis.read();

            // print the read character.
            System.out.println("The character read = " + ch );
         }
      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(inStream!=null)
            inStream.close();
         if(bis!=null)
            bis.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Available bytes = 5
The character read = A
Available bytes = 4
The character read = B
Available bytes = 3
The character read = C
Available bytes = 2
The character read = D
Available bytes = 1
The character read = E

Example - Checking Available Bytes in a File

This example demonstrates how to use the available() method to check the number of bytes that can be read from a file without blocking.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      // open input stream example.txt for reading purpose.
      try (BufferedInputStream inputStream = new BufferedInputStream(
         new FileInputStream("example.txt"))) {
         // check the available bytes
         int availableBytes = inputStream.available();
         System.out.println("Available bytes: " + availableBytes);
         // Read the first byte
         int byteRead = inputStream.read(); 
         System.out.println("First byte read: " + byteRead);
         // check the available bytes
         availableBytes = inputStream.available();
         System.out.println("Available bytes after reading: " + availableBytes);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Available bytes: 5
First byte read: 65
Available bytes after reading: 4

Explanation

  • The available() method returns the number of bytes that can be read without blocking.

  • Before reading any bytes, it reports the total number of bytes available in the stream's buffer.

  • After reading one byte, the available count decreases.

Example - Reading Data Based on Available Bytes

This example reads data from an input stream only if bytes are available.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      // open input stream example.txt for reading purpose.
      try (BufferedInputStream inputStream = 
         new BufferedInputStream(new FileInputStream("example.txt"))) {
         // check the available bytes		
         int availableBytes = inputStream.available();
         // if bytes are available to read
         if (availableBytes > 0) {
            byte[] buffer = new byte[availableBytes];
            int bytesRead = inputStream.read(buffer);
            System.out.println("Data read: " + new String(buffer, 0, bytesRead));
         } else {
            System.out.println("No data available to read.");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Data read: ABCDE

Explanation

  • The available() method is used to determine whether there is data to read.

  • If data is available, a buffer of appropriate size is created, and the data is read into the buffer.

  • The data is then converted to a string and displayed.

Key Notes About available() method

  • Purpose− The available() method checks how many bytes can be read from the stream without blocking. It does not guarantee the total size of the file.

  • Buffered Behavior− In BufferedInputStream, it reports the number of bytes available in the internal buffer.

  • Non-Blocking− It's particularly useful for non-blocking reads in cases where immediate data consumption is needed.

  • Usage Limitation− It may not always provide the exact total bytes in a stream, especially for network streams.

java_io_bufferedinputstream.htm