Java : PipedWriter with Examples

PipedWriter (Java SE 22 & JDK 22) with Examples.
You will find code examples on most PipedWriter methods.


Summary

Piped character-output streams.

Class diagram

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                writer.write("abc");
                TimeUnit.SECONDS.sleep(1);

                writer.write('X');
                TimeUnit.SECONDS.sleep(1);

                writer.write('Y');
                TimeUnit.SECONDS.sleep(1);

                writer.write('Z');
                TimeUnit.SECONDS.sleep(1);

            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c%n", c);
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a
//b
//c
//X
//Y
//Z
//-- end --

Fields declared in Writer

lock

Please see the link below.

Constructors

PipedWriter ()

Creates a piped writer that is not yet connected to a piped reader.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter()) {
    writer.connect(reader);

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                final var list = List.of('a', 'b', 'c', 'd');
                for (final var c : list) {
                    writer.write(c);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c%n", c);
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --

PipedWriter (PipedReader snk)

Creates a piped writer connected to the specified piped reader.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                final var list = List.of('a', 'b', 'c', 'd');
                for (final var c : list) {
                    writer.write(c);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c%n", c);
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --

Methods

void close ()

Closes this piped output stream and releases any system resources associated with this stream.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    writer.write('a');
    System.out.printf("%c%n", reader.read()); // a
}
// An example without a try-with-resources statement.
final var writer = new PipedWriter();
try (final var reader = new PipedReader(writer)) {

    writer.write('a');
    System.out.printf("%c%n", reader.read()); // a
} finally {
    writer.close();
}

void connect (PipedReader snk)

Connects this piped writer to a receiver.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter()) {
    writer.connect(reader);

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                final var list = List.of('a', 'b', 'c', 'd');
                for (final var c : list) {
                    writer.write(c);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c%n", c);
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --

void flush ()

Flushes this output stream and forces any buffered output characters to be written out.

// Gets the elapsed seconds from the start time.
final long startTime = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - startTime) / 1000000000.0;

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                final var list = List.of('a', 'b', 'c', 'd');
                for (final var c : list) {
                    writer.write(c);
                    writer.flush();
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c (%f sec.)%n", c, elapsedTime.getAsDouble());
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a (0.002405 sec.)
//b (1.007973 sec.)
//c (2.018421 sec.)
//d (3.024290 sec.)
//-- end --

void write (char[] cbuf, int off, int len)

Writes len characters from the specified character array starting at offset off to this piped output stream.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    final char[] cbuf = {'a', 'b', 'c'};

    {
        writer.write(cbuf, 0, 1);

        final var a = new char[3];
        final var ret = reader.read(a);

        // ret = 1 : [a,  ,  ]
        System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
    }
    {
        writer.write(cbuf, 0, 2);

        final var a = new char[3];
        final var ret = reader.read(a);

        // ret = 2 : [a, b,  ]
        System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
    }
    {
        writer.write(cbuf, 0, 3);

        final var a = new char[3];
        final var ret = reader.read(a);

        // ret = 3 : [a, b, c]
        System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
    }
    {
        writer.write(cbuf, 1, 2);

        final var a = new char[3];
        final var ret = reader.read(a);

        // ret = 2 : [b, c,  ]
        System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
    }
    {
        writer.write(cbuf, 2, 1);

        final var a = new char[3];
        final var ret = reader.read(a);

        // ret = 1 : [c,  ,  ]
        System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
    }
}

void write (int c)

Writes the specified char to the piped output stream.

try (final var reader = new PipedReader();
     final var writer = new PipedWriter(reader)) {

    try (final var executor = Executors.newFixedThreadPool(2)) {

        executor.submit(() -> {
            try (writer) {
                final var list = List.of('a', 'b', 'c', 'd');
                for (final var c : list) {
                    writer.write(c);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException!");
            }
        });

        executor.submit(() -> {
            try (reader) {
                System.out.println("-- start --");
                while (true) {
                    final var c = reader.read();
                    if (c == -1) {
                        System.out.println("-- end --");
                        break;
                    }

                    System.out.printf("%c%n", c);
                }
            } catch (IOException e) {
                System.out.println("Reader IOException! : " + e.getMessage());
            }
        });
    }
}

// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --

Methods declared in Writer

append, append, append, nullWriter, write, write, write

Please see the link below.


Related posts

To top of page