Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - PipedInputStream read() method



Description

The Java 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 - Usage of PipedInputStream read() method

The following example shows the usage of PipedInputStream read() method.

PipedInputStreamDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

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();
      }
   }
}

Output

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

F
G 

Example - Using read() to read one byte at a time from a piped stream

The following example shows the usage of PipedInputStream read() method.

PipedInputStreamDemo.java

package com.tutorialspoint;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;

public class PipedInputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedOutputStream pos = new PipedOutputStream();
         PipedInputStream pis = new PipedInputStream();
         pis.connect(pos);

         // Write a string into the pipe
         pos.write("ABC".getBytes());

         // Read one byte at a time
         int data;
         while ((data = pis.read()) != -1) {
            System.out.print((char) data); // Converts byte to char
         }

         pos.close();
         pis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

ABC

Explanation

  • read() reads one byte at a time from the piped input stream.

  • It returns the byte value (0−255) or -1 if the end of the stream is reached.

  • In this example, it reads "A", "B", and "C" byte by byte from the pipe.

Example - Reading bytes sent by another thread using read()

The following example shows the usage of PipedInputStream read() method.

PipedInputStreamDemo.java

package com.tutorialspoint;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;

public class PipedInputStreamDemo {
   public static void main(String[] args) throws IOException {
      PipedInputStream pis = new PipedInputStream();
      PipedOutputStream pos = new PipedOutputStream(pis); // connect via constructor

      Thread producer = new Thread(() -> {
         try {
            pos.write("Hello from thread!".getBytes());
            pos.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = pis.read()) != -1) {
               System.out.print((char) ch);
            }
            pis.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      producer.start();
      consumer.start();
   }
}

Output

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

Hello from thread!

Explanation

  • This example uses threads to simulate real-time communication.

  • The producer thread writes data to the pipe.

  • The consumer thread reads it one byte at a time using read().

  • read() blocks until data is available or the stream is closed.

java_io_pipedinputstream.htm
Advertisements