devstory

Le Tutoriel de Java PipedReader

  1. PipedReader
  2. Example 1
  3. Example 2

1. PipedReader

Pour mieux comprendre PipedReader, 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:
  • Que faut-il faire lorsqu'à chaque fois que des personnages apparaissent sur Thread-A, ils seront transférés automatiquement vers Thread-B?
PipedWriter et PipedReader sont créés pour vous aider à gérer la situation mentionnée ci-dessus. À chaque fois que des données sont écrites dans PipedWritrer, elles apparaîtront automatiquement dans PipedReader.
PipedReader constructors
PipedReader()

PipedReader(int pipeSize)

PipedReader(PipedWriter src)

PipedReader(PipedWriter src, int pipeSize)
Pour que les données écrites dans PipedWriter apparaissent dans PipedReader, vous devez connecter ces deux objets.
PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader = new PipedReader();

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

Exemple: Utiliser PipedReader, PipedWriter avec BufferedReader et BufferedWriter pour améliorer les performances du programme.
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:

Tutoriels Java IO

Show More