PipedOutputStream

PipedOutputStream is a class in the java.io package used to write data to a communication pipe, where another thread can read the data using a connected PipedInputStream. Together, they form a producer-consumer style inter-thread communication channel.

This mechanism is helpful for simulating streaming, pipelines, or in-memory communication between threads without using shared memory or files.

Commonly Used Constructors and Methods

Simple Program – Inter-thread Communication

Mahesh writes data into a PipedOutputStream, and LotusJavaPrince reads it using PipedInputStream. This program demonstrates the basic usage of piped streams for communication between two threads.

import java.io.*;

public class SimplePipedStreamDemo {
    public static void main(String[] args) throws IOException {
        PipedOutputStream pout = new PipedOutputStream();
        PipedInputStream pin = new PipedInputStream(pout);

        Thread writer = new Thread(() -> {
            try {
                pout.write("Hello from Mahesh!".getBytes());
                pout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

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

        writer.start();
        reader.start();
    }
}
/*
Hello from Mahesh!
*/

Problem Statement:

LotusJavaPrince wants to create a simulation where Mahesh sends messages to him via a PipedOutputStream, and LotusJavaPrince receives them using a PipedInputStream. This program emulates a one-way chat between two threads.

import java.io.*;

class MaheshSender extends Thread {
    private final PipedOutputStream out;

    public MaheshSender(PipedOutputStream out) {
        this.out = out;
    }

    public void run() {
        String[] messages = {
            "Mahesh: Hi LotusJavaPrince!",
            "Mahesh: Are you ready for the Java exam?",
            "Mahesh: Let's meet at the library later."
        };

        try {
            for (String msg : messages) {
                out.write((msg + "\n").getBytes());
                Thread.sleep(1000);
            }
            out.close();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class LotusJavaPrinceReceiver extends Thread {
    private final PipedInputStream in;

    public LotusJavaPrinceReceiver(PipedInputStream in) {
        this.in = in;
    }

    public void run() {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("LotusJavaPrince received: " + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class PipedChatSimulator {
    public static void main(String[] args) throws IOException {
        PipedOutputStream output = new PipedOutputStream();
        PipedInputStream input = new PipedInputStream(output);

        new MaheshSender(output).start();
        new LotusJavaPrinceReceiver(input).start();
    }
}

Output

LotusJavaPrince received: Mahesh: Hi LotusJavaPrince!
LotusJavaPrince received: Mahesh: Are you ready for the Java exam?
LotusJavaPrince received: Mahesh: Let's meet at the library later.

PipedOutputStream is not as commonly used as file streams or socket streams, but in scenarios where memory-based, real-time, thread-to-thread communication is required, it becomes an elegant and efficient solution.

Scroll to Top