Java : PipedReader (パイプ) - API使用例
PipedReader (Java SE 22 & JDK 22) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
PipedReader は、PipedWriter とセットで使う Reader です。
PipedWriter で書き込んだ文字を、PipedReader で読み込むことができます。
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//X
//Y
//Z
//-- end --
Readerで宣言されたフィールド
lock
「Java API 使用例 : Reader」をご参照ください。
コンストラクタ
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
PipedReader (int pipeSize)
pipeSize パラメータ以外については PipedReader() の使用例をご参照ください。
// 開始時刻からの差分を秒として取得
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!");
}
});
}
}
// 結果
// ↓
//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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
PipedReader (PipedWriter src, int pipeSize)
各パラメータについては、下記の使用例をご参照ください。
- src パラメータ : PipedReader(PipedWriter src)
- pipeSize パラメータ : PipedReader(int pipeSize)
メソッド
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
}
// try-with-resources文を使わない例です。
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());
}
});
}
}
// 結果
// ↓
//-- 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());
}
});
}
}
// 結果
// ↓
//-- 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
}
Readerで宣言されたメソッド
mark, markSupported, nullReader, read, read, reset, skip, transferTo
「Java API 使用例 : Reader」をご参照ください。