Java BufferedInputStream available() method with Example
Last Updated :
03 Jan, 2020
The BufferedInputStream class adds new attributes to other input streams, an ability to buffer the input. When BufferedInputStream is created, an internal buffer array is created.
The available() method of BufferedInputStream class is used to know the number of bytes available to read from the internal buffer array, until a situation arises where there is no data available to read. The invocation of the method read() will block the execution flow of the program and wait for the data to be made available.
Syntax:
public int available()
Parameters: This method does not take any parameters.
Return value: The method is used to return the sum of bytes remained to be read from this input stream without any blockage.
Exception: The method throws IOException if an error related to input-output occurs or when the close method has been used to close the input stream.
Example 1: Below program illustrates the use of available() method, assuming the existence of file “d:/demo.txt”.
import java.io.*;
class Testing {
public static void main(String[] args)
throws IOException
{
FileInputStream inputStream =
new FileInputStream( "d:/demo.txt" );
BufferedInputStream buffInputStr =
new BufferedInputStream(inputStream);
Integer remBytes =
buffInputStr.available();
System.out.println(
"Remaining bytes =" + remBytes);
}
}
|
Output:
5
Example 2: Below program illustrates the use of available() method, assuming the existence of file “d:/demo.txt”.
import java.io.*;
class Testing {
public static void main(String[] args)
throws IOException
{
FileInputStream inputStream =
new FileInputStream( "d:/demo.txt" );
BufferedInputStream buffInputStr =
new BufferedInputStream(inputStream);
Integer remBytes =
buffInputStr.available();
System.out.println(
"Remaining bytes =" + remBytes);
}
}
|
Output:
13
Share your thoughts in the comments
Please Login to comment...