Home »
Java programming language
Java StringBufferInputStream available() Method with Example
StringBufferInputStream Class available() method: Here, we are going to learn about the available() method of StringBufferInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 26, 2020
StringBufferInputStream Class available() method
- available() method is available in java.io package.
- available() method is used to get the number of available bytes left that can be read without blocking from this StringBufferInputStream.
- available() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- available() method does not throw an exception at the time of returning available bytes.
Syntax:
public int available();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is int, it returns the exact number of bytes read.
Example:
// Java program to demonstrate the example
// of int available() method of StringBufferInputStream
import java.io.*;
public class AvailableOfSBIS {
public static void main(String[] args) throws Exception {
StringBufferInputStream is_stm = null;
int count = 0;
try {
// Instantiates StringBufferInputStream
is_stm = new StringBufferInputStream("Java Programming");
// Loop to read until available
// bytes left
while ((count = is_stm.read()) != -1) {
// By using available() method is to
// return the available bytes to be read
int avail_bytes = is_stm.available();
// Display corresponding char value
char ch = (char) count;
// Display value of avail_bytes and ch
System.out.print("is_stm.available(): " + avail_bytes);
System.out.println(" : " + "ch: " + ch);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (is_stm != null) {
is_stm.close();
}
}
}
}
Output
is_stm.available(): 15 : ch: J
is_stm.available(): 14 : ch: a
is_stm.available(): 13 : ch: v
is_stm.available(): 12 : ch: a
is_stm.available(): 11 : ch:
is_stm.available(): 10 : ch: P
is_stm.available(): 9 : ch: r
is_stm.available(): 8 : ch: o
is_stm.available(): 7 : ch: g
is_stm.available(): 6 : ch: r
is_stm.available(): 5 : ch: a
is_stm.available(): 4 : ch: m
is_stm.available(): 3 : ch: m
is_stm.available(): 2 : ch: i
is_stm.available(): 1 : ch: n
is_stm.available(): 0 : ch: g