Java.io.PipedInputStream.read() Method



Description

The java.io.PipedInputStream.read() method reads the next byte of data from this piped input stream. The value byte is returned as an int in the range 0 to 255. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Declaration

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

public int read()

Parameters

NA

Return Value

This method returns the next byte of data, or -1 if the end of the stream is reached.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.PipedInputStream.read() 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);

         // 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 −

F
G
java_io_pipedinputstream.htm
Advertisements