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:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] byteArray
= new byte [] { 'G' , 'E' , 'E' ,
'K' , 'S' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
int total_bytes
= pushbackInputStr.available();
System.out.println(
"Total Bytes : " + total_bytes);
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] byteArray
= new byte [] { 'G' , 'E' , 'E' , 'K' , 'S' ,
'F' , 'O' , 'R' , 'G' , 'E' ,
'E' , 'K' , 'S' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
System.out.println(
"Total Bytes : "
+ pushbackInputStr.available());
}
}
|
References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#available()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...