Java : FilterOutputStream - API使用例
FilterOutputStream (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
FilterOutputStream は、OutputStream オブジェクトをラップするだけのクラスです。
BufferedOutputStream や DataOutputStream などのスーパークラスとして使われています。
FilterOutputStream は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。
class MyOutputStream extends FilterOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
}
final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
フィールド
protected OutputStream out
class MyOutputStream extends FilterOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
OutputStream getOut() {
return out;
}
}
final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
System.out.println(os.getOut() == out); // true
}
コンストラクタ
FilterOutputStream (OutputStream out)
class MyOutputStream extends FilterOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
}
final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
メソッド
void close ()
final var out = new ByteArrayOutputStream();
try (final FilterOutputStream os = new BufferedOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
// try-with-resources文を使わない例です。
final var out = new ByteArrayOutputStream();
final FilterOutputStream os = new BufferedOutputStream(out);
try {
os.write(10);
os.write(20);
os.write(30);
} finally {
os.close();
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
void flush ()
final var out = new ByteArrayOutputStream();
try (final FilterOutputStream os = new BufferedOutputStream(out)) {
os.write(10);
System.out.println(Arrays.toString(out.toByteArray())); // []
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.write(20);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(30);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
void write (byte[] b)
final var out = new ByteArrayOutputStream();
try (final FilterOutputStream os = new BufferedOutputStream(out)) {
final byte[] b1 = {10, 20};
os.write(b1);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
final byte[] b2 = {30, 40, 50};
os.write(b2);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}
void write (byte[] b, int off, int len)
final var out = new ByteArrayOutputStream();
try (final FilterOutputStream os = new BufferedOutputStream(out)) {
final byte[] b = {10, 20, 30, 40, 50};
os.write(b, 0, 2);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(b, 2, 3);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}
void write (int b)
class MyOutputStream extends FilterOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
}
final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
OutputStreamで宣言されたメソッド
nullOutputStream
「Java API 使用例 : OutputStream」をご参照ください。