o7planning

Java PipedInputStream Tutorial

View more Tutorials:

1- PipedInputStream

  • TODO Link!
  • TODO Link!
To easily understand PipedInputStream, 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 bytes appear on Thread-A, they will be transfered to Thread-B automatically?
PipedOutputStream and PipedInputStream are created to help you handle situation mentioned above. Each time data is written to PipedOutputStream, they will appear automatically on PipedInputStream.
PipedInputStream constructors
PipedInputStream()     

PipedInputStream​(int pipeSize)     

PipedInputStream​(PipedOutputStream src)     

PipedInputStream​(PipedOutputStream src, int pipeSize)
For data written to PipedOutputStream to appear on PipedInputStream, you must connect these two objects together.
PipedOutputStream pipedOS = new PipedOutputStream();
PipedInputStream pipedIS = new PipedInputStream();

pipedOS.connect(pipedIS);
The above code is equivalent to the following ways:
PipedOutputStream pipedOS = new PipedOutputStream();
PipedInputStream pipedIS = new PipedInputStream();

pipedIS.connect(pipedOS);
PipedOutputStream pipedOS = new PipedOutputStream();
PipedInputStream pipedIS = new PipedInputStream(pipedOS);
PipedInputStream pipedIS = new PipedInputStream();
PipedOutputStream pipedOS = new PipedOutputStream(pipedIS);
  • TODO Link!
  • TODO Link!

2- Example 1

PipedInputStreamEx1.java
package org.o7planning.pipedinputstream.ex;

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

public class PipedInputStreamEx1 {

    private PipedInputStream pipedIS;
    private PipedOutputStream pipedOS;

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

    private void test() throws IOException, InterruptedException {

        // Create a PipedInputStream
        pipedIS = new PipedInputStream();

        // Data written to 'pipedOS'
        // will appear automatically at 'pipedIS'.
        pipedOS = new PipedOutputStream(pipedIS);

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

    //
    class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                byte[] bytes = new byte[] { 'a', 97, 'b', 'c', 101 };

                for (byte b : bytes) {
                    pipedOS.write(b);
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(pipedOS);
            }
        }
    }

    //
    class ThreadB extends Thread {

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

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

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

3- Example 2

Example: Using PipedInputStream, PipedOutputStream with BufferedInputStream and BufferedOutputStream to improve the program's performance.
PipedInputStreamEx2.java
package org.o7planning.pipedinputstream.ex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputStreamEx2 {

    private BufferedInputStream bufferedIS;
    private BufferedOutputStream bufferedOS;

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

    private void test() throws IOException, InterruptedException {

        
        PipedInputStream pipedIS = new PipedInputStream();  
        PipedOutputStream pipedOS = new PipedOutputStream();
        
        pipedIS.connect(pipedOS);
        
        this.bufferedIS = new BufferedInputStream(pipedIS);
        this.bufferedOS = new BufferedOutputStream(pipedOS);

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

    //
    class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                byte[] bytes = new byte[] { 'a', 97, 'b', 'c', 101 };

                for (byte b : bytes) {
                    bufferedOS.write(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeQuietly(bufferedOS);
            }
        }
    }

    //
    class ThreadB extends Thread {

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

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

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

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.