Java - InputStream available() method



Description

The Java InputStream available() method returns an the number of bytes that can be read from this input stream without blocking by the next invocation of a method from this input stream.

Declaration

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

public int available()

Parameters

NA

Return Value

The method returns the number of bytes that can be read.

Exception

IOException − If an I/O error occurs.

Example - Usage of InputStream available() method

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

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      int i = 0;
      char c;
      
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         // read till the end of the stream
         while((i = is.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // print
            System.out.println("Character Read: "+c);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDEF")

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

Character Read: A
Character Read: B
Character Read: C
Character Read: D
Character Read: E
Character Read: F

Example - Checking Available Bytes in FileInputStream

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

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new FileInputStream("example.txt")) {
         System.out.println("Bytes available: " + inputStream.available());

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

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

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Bytes available: 5
First byte read: H
Bytes available after reading one byte: 4

Explanation

  • Uses FileInputStream to read from "example.txt".

  • Calls available() before reading, showing the total available bytes.

  • Reads one byte, then calls available() again to show the remaining bytes.

Example - Checking Available Bytes in BufferedInputStream

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

InputStreamDemo.java

package com.tutorialspoint;

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

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Buffered Stream - Bytes available: " + inputStream.available());

         byte[] buffer = new byte[3];
         inputStream.read(buffer); // Read 3 bytes

         System.out.println("After reading 3 bytes, bytes available: " + inputStream.available());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Microservices")

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

Buffered Stream - Bytes available: 13
After reading 3 bytes, bytes available: 10

Explanation

  • Uses BufferedInputStream (a subclass of InputStream).

  • Calls available() before reading.

  • Reads 3 bytes from the file.

  • Calls available() again to check the remaining unread bytes.

java_io_inputstream.htm
Advertisements