Java.io.PushbackInputStream.available() Method

Advertisements


Description

The java.io.PushbackInputStream.available() 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. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Declaration

Following is the declaration for java.io.PushbackInputStream.available() method

public int available()

Parameters

  • NA

Return Value

This method returns the number of bytes that can be read (or skipped over) from the input stream without blocking.

Exception

  • IOException -- if this input stream has been closed by invoking its close() method, or an I/O error occurs.

Example

The following example shows the usage of java.io.PushbackInputStream.available() method.

package com.tutorialspoint;

import java.io.*;

public class PushbackInputStreamDemo {

   public static void main(String[] args) {

      // declare a buffer and initialize its size:
      byte[] arrByte = new byte[1024];

      // create an array for our message
      byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o'};

      try {
         // create object of PushbackInputStream class for specified stream
         InputStream is = new ByteArrayInputStream(byteArray);
         PushbackInputStream pis = new PushbackInputStream(is);

         // check how many bytes are available
         System.out.println("" + pis.available());

         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length; i++) {

            // read a char into our array
            arrByte[i] = (byte) pis.read();

            // display the read byte
            System.out.print((char) arrByte[i]);
         }

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

5
Hello



Advertisements
Advertisements