Java.io.PipedInputStream.available() Method

Advertisements


Description

The java.io.PipedInputStream.available() method returns the number of bytes that can be read from this input stream without blocking.

Declaration

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

public int available()

Parameters

  • NA

Return Value

This method returns the number of bytes that can be read from this input stream without blocking, or 0 if this input stream has been closed by invoking its close() method, or if the pipe is unconnected, or broken.

Exception

  • IOException -- if an I/O error occurs.

Example

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

package com.tutorialspoint;

import java.io.*;

public class PipedInputStreamDemo {

   public static void main(String[] args) {

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStream in = new PipedInputStream();

      try {
         // connect input and output
         in.connect(out);

         // write something 
         out.write(70);
         out.write(71);

         // print how many bytes are available
         System.out.println("" + in.available());

         // read what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) in.read());
         }

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


   }
}

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

2
F
G



Advertisements
Advertisements