Java : FilterOutputStream with Examples
FilterOutputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most FilterOutputStream methods.
Summary
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]
Fields
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
}
Constructors
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]
Methods
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]
// An example without a try-with-resources statement.
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]
Methods declared in OutputStream
Related posts
- API Examples