Java : PipedReader with Examples
PipedReader (Java SE 22 & JDK 22) with Examples.
You will find code examples on most PipedReader methods.
Summary
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
Constructors
PipedReader ()
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)
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)
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)
Please see :
- PipedReader(PipedWriter src) for the src parameter.
- PipedReader(int pipeSize) for the pipeSize parameter.
Methods
void close ()
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)
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 ()
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)
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 ()
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
Please see the link below.
Related posts
- API Examples