PipedInputStream and PipedOutputStream in Java

Pipes in Java IO provides the ability for two threads running in the same JVM to communicate. Therefore pipes can also be sources or destinations of data.

You cannot use a pipe to communicate with a thread in a different JVM (different process).

PipedInputStream and PipedOutputStream are byte based. The PipedOutputStream must always be connected to a PipedInputStream. When connected like that, they form a pipe.

The data written to the PipedOutputStream by one thread can be read from the connected PipedInputStream by another thread.

A pipe is said to be broken if a thread that was providing data to the connected PipedOutputStream is no longer alive.

Note : Use PipedWriter and PipedReader for character based pipe.

package com.java.io;

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

public class PipedStreamExample {

    public static void main(String[] args) throws IOException {

        // create a new PipedOutputStream and PipedInputStream
        PipedOutputStream pipedOutputStream = new PipedOutputStream();
        PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
        String data = "Piped data contents";

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("Writing data using pipedOutputStream:" + data);
                    pipedOutputStream.write(data.getBytes());
                } catch (IOException e) {}
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int data = pipedInputStream.read();
                    System.out.print("Reading data using pipedInputStream:");
                    while (data != -1) {
                        System.out.print((char) data);
                        data = pipedInputStream.read();
                    }
                } catch (IOException e) {

                } finally {
                    // Closing the streams
                    if (pipedOutputStream != null)
                        try {
                            pipedOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    if (pipedInputStream != null) {
                        try {
                            pipedInputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        thread1.start();
        thread2.start();

    }

}
Writing data using pipedOutputStream:Piped data contents
Reading data using pipedInputStream:Piped data contents

Leave a Reply

Your email address will not be published. Required fields are marked *