available(): This method is available in java.io.BufferedInputStream class of Java.
Syntax:
int java.io.BufferedInputStream.available() throws IOException
This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
Parameters: NA
Returns: an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking.
Throws:
IOException - if this input stream has been closed by invoking its close() method,or an I/O error occurs.
Approach 1: When no exception
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamavailable {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);System.out.println(bufferedInputStream.available());}}
Output:
0
Approach 2: IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamavailable {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);bufferedInputStream.close();System.out.println(bufferedInputStream.available());}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:157) at java.base/java.io.BufferedInputStream.available(BufferedInputStream.java:408) at com.example.BufferedInputStream.BufferedInputStreamavailable.main(BufferedInputStreamavailable.java:14)
No comments:
Post a Comment