Java : PipedReader con ejemplos

PipedReader (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de PipedReader.

Nota :


Summary

Flujos de entrada de caracteres canalizados. (Traducción automática)

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 Reader

lock

Consulte el siguiente enlace.

Constructors

PipedReader ()

Crea un PipedReader para que aún no esté conectado. (Traducción automática)

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

    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 --

PipedReader (int pipeSize)

Crea un PipedReader para que aún no esté conectado y utiliza el tamaño de tubería especificado para el búfer de la tubería. (Traducción automática)

Please see also : PipedReader()

// 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(3);
     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', 'e');
                for (final var c : list) {
                    writer.write(c);
                    System.out.printf("write = %c (%f sec.)%n", c, elapsedTime.getAsDouble());
                }
            } catch (IOException e) {
                System.out.println("Writer IOException! : " + e.getMessage());
            }
        });

        executor.submit(() -> {
            try (reader) {
                while (true) {
                    TimeUnit.SECONDS.sleep(2);

                    final var c = reader.read();
                    if (c == -1) {
                        break;
                    }

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

}

// Result
// ↓
//write = a (0.002390 sec.)
//write = b (0.003750 sec.)
//write = c (0.003882 sec.)
//read  = a (2.014535 sec.)
//write = d (2.029911 sec.)
//read  = b (4.030443 sec.)
//write = e (4.046095 sec.)
//read  = c (6.036116 sec.)
//read  = d (8.042297 sec.)
//read  = e (10.055684 sec.)

PipedReader (PipedWriter src)

Crea un PipedReader para que esté conectado a la fuente del escritor canalizado. (Traducción automática)

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

    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 --

PipedReader (PipedWriter src, int pipeSize)

Crea un PipedReader para que se conecte al origen del escritor canalizado y utilice el tamaño de canalización especificado para el búfer de la canalización. (Traducción automática)

Please see :

Methods

void close ()

Cierra esta transmisión canalizada y libera todos los recursos del sistema asociados con la transmisión. (Traducción automática)

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 reader = new PipedReader();
try (final var writer = new PipedWriter(reader)) {

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

void connect (PipedWriter src)

Hace que este lector canalizado se conecte al origen del escritor canalizado. (Traducción automática)

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

    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 --

int read ()

Lee el siguiente carácter de datos de esta secuencia canalizada. (Traducción automática)

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

    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 --

int read (char[] cbuf, int off, int len)

Lee caracteres en una parte de una matriz. (Traducción automática)

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

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

    writer.write(cbuf);

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

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

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

    writer.write(cbuf);

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

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

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

    writer.write(cbuf);

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

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

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

    writer.write(cbuf);

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

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

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

    writer.write(cbuf);

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

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

boolean ready ()

Indique si este flujo está listo para ser leído. (Traducción automática)

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

    System.out.println(reader.ready()); // false

    writer.write('a');

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.ready()); // false

    writer.write('b');
    writer.write('c');

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // b

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.ready()); // false
}

Methods declared in Reader

mark, markSupported, nullReader, read, read, reset, skip, transferTo

Consulte el siguiente enlace.


Related posts

To top of page