Skip to content
Related Articles

Related Articles

Improve Article
ByteArrayInputStream available() method in Java with Examples
  • Last Updated : 01 Apr, 2019

The available() method is a built-in method of the Java.io.ByteArrayInputStream returns the number of remaining bytes that can be read (or skipped over) from this input stream. It tells the total no. of bytes from the Input Stream to be read.

Syntax:

public int available()

Parameters: The function does not accepts any parameter.

Return Value: The function returns the total no of bytes from the Input Strem to be read.

Below is the implementation of the above function:



Program 1:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        // Array
        byte[] buffer = { 71, 69, 69, 75, 83 };
  
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
  
        // Use the function to get the number
        // of available
        int number = geek.available();
  
        // Print
        System.out.println("Use of available() method : "
                           + number);
    }
}
Output:
Use of available() method : 5

Program 2:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        // Array
        byte[] buffer = { 1, 2, 3, 4 };
  
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
  
        // Use the function to get the number
        // of available
        int number = geek.available();
  
        // Print
        System.out.println("Use of available() method : "
                           + number);
    }
}
Output:
Use of available() method : 4

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#available()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :