devstory

Le Tutoriel de Java PipedInputStream

  1. PipedInputStream
  2. Example 1
  3. Example 2

1. PipedInputStream

Pour mieux comprendre PipedInputStream, je propose une illustration via l'exemple ci-dessous:
On suppose que vous développiez une application Multithreading et que vous disposiez de 2 Threads indépendants: Thread-A et Thread-B. La question est la suivante:
  • Qu'est-ce qu'il faut faire lorsque chaque fois que des bytes apparaissent sur Thread-A, ils seront transférés automatiquement vers Thread-B?
PipedOutputStream et PipedInputStream sont créés pour vous aider à gérer la situation mentionnée ci-dessus. Chaque fois que des données sont écrites dans PipedOutputStream, elles apparaissent automatiquement dans PipedInputStream.
PipedInputStream constructors
PipedInputStream()     

PipedInputStream​(int pipeSize)     

PipedInputStream​(PipedOutputStream src)     

PipedInputStream​(PipedOutputStream src, int pipeSize)
Pour que les données écrites dans PipedOutputStream apparaissent dans PipedInputStream, vous devez mettre ces deux objets ensemble.
PipedOutputStream pipedOS = new PipedOutputStream();
PipedInputStream pipedIS = new PipedInputStream();

pipedOS.connect(pipedIS);
Le code ci-dessus est équivalent à la manière suivante:
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);
  • Java PipedOutputStream
  • Le Tutoriel de Java PipedWriter

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

Exemple: Utiliser PipedInputStream, PipedOutputStream avec BufferedInputStream et BufferedOutputStream pour améliorer les performances du programme.
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) {
            }
        }
    }
}

Tutoriels Java IO

Show More