Push back Input Stream available() method in Java with Examples
The available() method of PushbackInputStream class in Java determines the available number of bytes which can be read from input stream without being blocked. When it is called, it returns an estimated value of the byte count, indicating the number of bytes that are available for use. However, subsequent method invocations on the same input stream may affect the availability of bytes, which results in blocking scenarios.
Syntax:
public int available() throws IOException
overrides: It overrides the available() method of the FilterInputStream class.
Parameters: the method does not accept any parameter.
Return value: The method returns the number of bytes which can be read from the input stream without blocking.
Exceptions: It throws an IOException if an I/O error occurs or if the input stream is closed by invoking the close() method of the same class.
Example 1:
The following example explains how to use the PushbackInputStream in Java.
We are creating a byte array, initialising an InputStream with this byte array, and then using the InputStream to create a PushbackInputStream object. The available() method is then used to determine the number of bytes available for reading from the PushbackInputStream, and the result is displayed to the output.
FileName: PushBackIpStreamExample.java
// importing io packages import java.io.*; public class PushBackIpStreamExample { // main method public static void main(String[] args) throws IOException { // creating a byte array byte[] btArray = new byte[] { 'H', 'E', 'L', 'L', 'O', 'w', 'O', 'R', 'L', 'D' }; // creating an input Stream InputStream ipStrm = new ByteArrayInputStream(btArray); // creating object of PushbackInputStream class PushbackInputStream pshBkIpStrm = new PushbackInputStream(ipStrm); // Using an available method to find the available number of bytes int ttlBytesCnt = pshBkIpStrm.available(); System.out.println("Total number of Bytes Available are: " + ttlBytesCnt); } }
Output:
Total number of Bytes Available are: 10
Example 2:
The following example demonstrates how to use the Java PushbackInputStream class with a custom message. We are creating a string, converting it to a byte array, and using that byte array to initialize an InputStream. The PushbackInputStream object is then constructed using the InputStream. The available() method returns the number of bytes available for reading from the PushbackInputStream, which is printed in the output.
FileName: PushBackIpStreamExample.java
// importing io packages import java.io.*; public class PushBackIpStreamExample { // main method public static void main(String[] args) throws IOException { // creating a string String msg = "Custom Message"; // converting the string into bytes byte[] btArray = msg.getBytes(); // creating an input Stream InputStream ipStrm = new ByteArrayInputStream(btArray); // creating object of PushbackInputStream class PushbackInputStream pshBkIpStrm = new PushbackInputStream(ipStrm); // using the available method to find the available number of bytes int ttlBytesCnt = pshBkIpStrm.available(); System.out.println("Total number of Bytes Available are: " + ttlBytesCnt); } }
Output:
Total number of Bytes Available are: 14