Java - ByteArrayInputStream available() method



Description

The Java ByteArrayInputStream available() method returns the number of bytes left to be read from this input stream. It returns a value as count - pos, which is the number of bytes remaining to be read from the input buffer.

This method is used to determine the number of remaining bytes that can be read from the stream without blocking.

Declaration

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

public int available()

Parameters

NA

Return Value

The value returns the count of bytes to be read from the input stream.

Exception

NA

Example - Getting available bytes

The following example shows the usage of Java ByteArrayInputStream available() method. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. In while loop, we're checking if stream contains any byte using available() method and then its bytes are read using read() method.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamDemo {

   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         int count =0;
         
         // read till the end of the stream
         while((count = bais.available())>0) {
            
            // convert byte to character
            char c = (char)bais.read();
            
            // print number of bytes available
            System.out.print("available byte(s) : "+ count);
            
            // print characters read form the byte array
            System.out.println(" & byte read : "+c);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

Output

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

available byte(s) : 5 & byte read : A
available byte(s) : 4 & byte read : B
available byte(s) : 3 & byte read : C
available byte(s) : 2 & byte read : D
available byte(s) : 1 & byte read : E

Example - Getting available bytes for empty array

The following example shows the usage of Java ByteArrayInputStream available() method. We've created a variable buf as byte[] and initialized with empty array. We've created a ByteArrayInputStream reference and then initialized it with buf variable. In if loop, we're checking if stream contains any byte using available() method and then its result is printed.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamDemo {

   public static void main(String[] args) throws IOException {
      byte[] buf = {};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         if(bais.available()>0) {
            System.out.print("bytes are available");
         }else{
            System.out.print("byte stream is empty");
         } 
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

Output

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

byte stream is empty

Example - Using ByteArrayInputStream available() method

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

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {65, 66, 67, 68, 69}; // Corresponds to 'A', 'B', 'C', 'D', 'E'

      // Create a ByteArrayInputStream using the byte array
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      // Check the number of bytes available initially
      System.out.println("Bytes available at the start: " + inputStream.available());

      // Read the first byte
      int firstByte = inputStream.read();
      System.out.println("Read byte: " + (char) firstByte);

      // Check the number of bytes available after reading one byte
      System.out.println("Bytes available after reading one byte: " + inputStream.available());

      // Read all remaining bytes
      while (inputStream.available() > 0) {
         int byteData = inputStream.read();
         System.out.println("Read byte: " + (char) byteData);
      }

      // Check the number of bytes available at the end
      System.out.println("Bytes available at the end: " + inputStream.available());
   }
}

Output

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

Bytes available at the start: 5
Read byte: A
Bytes available after reading one byte: 4
Read byte: B
Read byte: C
Read byte: D
Read byte: E
Bytes available at the end: 0

Explanation

  • Initialization− A ByteArrayInputStream is created using a byte array that contains values representing ASCII characters ('A', 'B', 'C', 'D', 'E').

  • Initial Available Bytes− The available() method is called at the start, returning the total number of bytes in the stream (5 in this case).

  • After Reading− When a byte is read using the read() method, the available bytes decrease by 1.

  • While Loop− The program uses a loop to read and print all remaining bytes while available() indicates more bytes are left in the stream.

  • Final Check− At the end, when all bytes have been read, available() returns 0.

Key Points

  • available() is a non-blocking method, meaning it does not pause or wait for more data.

  • It's useful for checking how much data is left in the stream without actually consuming the bytes.

java_bytearrayinputstream.htm
Advertisements