Related Articles

Related Articles

PushbackInputStream available() method in Java with Examples
  • Last Updated : 05 Jun, 2020

The available() method of PushbackInputStream class in Java is used to find the number of bytes that can be read from the input stream without blocking. It returns the estimated value of this number of bytes. It may be blocked by the next invocation of a method for the same input stream.

Syntax:

public int available()
              throws IOException

Overrides: This method overrides the available() method of FilterInputStream class.

Parameters: This method does not accept any parameter.

Return value: This method returns the number of bytes that can be read from the input stream without blocking.



Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or an I/O error occurs.

Below programs illustrate available() method of PushbackInputStream class in IO package:

Program 1:




// Java program to illustrate
// PushbackInputStream available() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create an array
        byte[] byteArray
            = new byte[] { 'G', 'E', 'E',
                           'K', 'S' };
  
        // Create inputStream
        InputStream inputStr
            = new ByteArrayInputStream(byteArray);
  
        // Create object of
        // PushbackInputStream
        PushbackInputStream pushbackInputStr
            = new PushbackInputStream(inputStr);
  
        // Find number of bytes
        int total_bytes
            = pushbackInputStr.available();
  
        System.out.println(
            "Total Bytes : " + total_bytes);
    }
}


Output:

Total Bytes : 5

Program 2:




// Java program to illustrate
// PushbackInputStream available() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create an array
        byte[] byteArray
            = new byte[] { 'G', 'E', 'E', 'K', 'S',
                           'F', 'O', 'R', 'G', 'E',
                           'E', 'K', 'S' };
  
        // Create inputStream
        InputStream inputStr
            = new ByteArrayInputStream(byteArray);
  
        // Create object of
        // PushbackInputStream
        PushbackInputStream pushbackInputStr
            = new PushbackInputStream(inputStr);
  
        // Print total bytes
        System.out.println(
            "Total Bytes : "
            + pushbackInputStr.available());
    }
}


Output:

Total Bytes : 13

References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :