Java : PipedWriter (パイプ) - API使用例
PipedWriter (Java SE 22 & JDK 22) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
PipedWriter は、PipedReader とセットで使う Writer です。
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 --
Writerで宣言されたフィールド
lock
「Java API 使用例 : Writer」をご参照ください。
コンストラクタ
PipedWriter ()
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
PipedWriter (PipedReader snk)
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
メソッド
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 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)
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
void flush ()
// 開始時刻からの差分を秒として取得
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());
}
});
}
}
// 結果
// ↓
//-- 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)
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)
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());
}
});
}
}
// 結果
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
Writerで宣言されたメソッド
append, append, append, nullWriter, write, write, write
「Java API 使用例 : Writer」をご参照ください。