PipedWriter and PipedReader 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).

PipedWriter and PipedReader are character based. The PipedWriter must always be connected to a PipedReader. When connected like that, they form a pipe.

The data written to the PipedWriter by one thread can be read from the connected PipedReader by another thread.

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

package com.java.io;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedExample {
    public static void main(String[] args) {

        // create a new Piped writer and reader
        PipedWriter pipedWriter = new PipedWriter();
        PipedReader pipedReader = new PipedReader();
        String data1 = "Piped data contents1";
        int totalChars1 = data1.length();
        String data2 = "Piped data contents2";
        int totalChars2 = data2.length();

        try {
            // connect the PipedReader and the PipedWriter
            pipedWriter.connect(pipedReader);

            // write data1
            pipedWriter.write(data1);
            System.out.print("Reading characters one by one:");
            if (pipedReader.ready()) {
                for (int i = 0; i < totalChars1; i++) {
                    // Reading one character at a time
                    System.out.print((char) pipedReader.read());
                }

            }
            // write data2
            pipedWriter.write(data2);
            System.out.println("");
            System.out.print("Reading characters into char Array:");
            char c[] = new char[totalChars2];
            if (pipedReader.ready()) {
                pipedReader.read(c);
                for (int i = 0; i < totalChars2; i++) {
                    System.out.print(c[i]);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Closing the streams
            if (pipedWriter != null)
                try {
                    pipedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (pipedReader != null) {
                try {
                    pipedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
Reading characters one by one:Piped data contents1
Reading characters into char Array:Piped data contents2
package com.java.io;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedThreadExample {

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

        // create a new Piped writer and reader
        PipedWriter pipedWriter = new PipedWriter();
        PipedReader pipedReader = new PipedReader(pipedWriter);
        String data = "Piped data contents";

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

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

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

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

    }

}
Writing data using pipedWriter:Piped data contents
Reading data using pipedReader:Piped data contents

Leave a Reply

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