o7planning

Java PipedReader Tutorial with Examples

  1. PipedReader
  2. Example 1
  3. Example 2

1. PipedReader

To easily understand PipedReader, I illustrate by an example below:
Suppose you are developing a Multithreading apps, and you have 2 independent Threads: Thread-A and Thread-B. The question is:
  • What need to do when every time characters appear on Thread-A, they will be transfered to Thread-B automatically?
PipedWriter and PipedReader are created to help you handle situation mentioned above. Each time data is written to PipedWritrer, they will appear automatically on PipedReader.
PipedReader constructors
PipedReader()

PipedReader(int pipeSize)

PipedReader(PipedWriter src)

PipedReader(PipedWriter src, int pipeSize)
For data written to PipedWriter to appear on PipedReader, you must connect these two objects together.
PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader = new PipedReader();

pipedWriter.connect(pipedReader);
The above code is equivalent to the following ways:
PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader = new PipedReader();

pipedReader.connect(pipedWriter);
PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader = new PipedReader(pipedWriter);
PipedReader pipedReader = new PipedReader();
PipedWriter pipedWriter = new PipedWriter(pipedReader);
  • Java PipedWriter

2. Example 1

PipedReaderEx1.java
package org.o7planning.pipedreader.ex;

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

public class PipedReaderEx1 {

    private PipedReader pipedReader;
    private PipedWriter pipedWriter;

    public static void main(String[] args) throws IOException, InterruptedException {
        new PipedReaderEx1().test();
    }

    private void test() throws IOException, InterruptedException {

        // Create a PipedReader
        pipedReader = new PipedReader();

        // Data written to 'pipedWriter'
        // will appear automatically at 'pipedReader'.
        pipedWriter = new PipedWriter(pipedReader);

        new ThreadB().start();
        new ThreadA().start();
    }

    //
    class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                char[] chs = new char[] { 'a', 'a', 'b', 'c', 'e' };

                for (char ch : chs) {
                    pipedWriter.write(ch);
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(pipedWriter);
            }
        }
    }

    //
    class ThreadB extends Thread {

        @Override
        public void run() {
            try {
                int ch = 0;
                while ((ch = pipedReader.read()) != -1) {
                    System.out.println((char) ch);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(pipedReader);
            }
        }
    }

    private void closeQuietly(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    private void closeQuietly(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
}
Output:

3. Example 2

Example: Using PipedReader, PipedWriter with BufferedReader and BufferedWriter to improve the program's performance.
PipedReaderEx2.java
package org.o7planning.pipedreader.ex;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedReaderEx2 {

    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;

    public static void main(String[] args) throws IOException, InterruptedException {
        new PipedReaderEx2().test();
    }

    private void test() throws IOException, InterruptedException {

        
        PipedReader pipedReader = new PipedReader();  
        PipedWriter pipedWriter = new PipedWriter();
        
        pipedReader.connect(pipedWriter);
        
        this.bufferedReader = new BufferedReader(pipedReader);
        this.bufferedWriter = new BufferedWriter(pipedWriter);

        new ThreadB().start();
        new ThreadA().start();
    }

    //
    class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                String[] texts = new String[] { "One\nTwo\n", "Three\nFour\n", "Five" };

                for (String text: texts) {
                    bufferedWriter.write(text);
                    bufferedWriter.flush();
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(bufferedWriter);
            }
        }
    }

    //
    class ThreadB extends Thread {

        @Override
        public void run() {
            try {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(bufferedReader);
            }
        }
    }

    private void closeQuietly(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    private void closeQuietly(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
}
Output:

Java IO Tutorials

Show More