Java : FilterOutputStream with Examples

FilterOutputStream (Java SE 18 & JDK 18) API Examples.
You will find code examples on most FilterOutputStream methods.


Summary

This class is the superclass of all classes that filter output streams. These streams sit on top of an already existing output stream (the underlying output stream) which it uses as its basic sink of data, but possibly transforming the data along the way or providing additional functionality.

Class diagram

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

The underlying output stream to be filtered.

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)

Creates an output stream filter built on top of the specified underlying output stream.

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 ()

Closes this output stream and releases any system resources associated with the stream.

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 ()

Flushes this output stream and forces any buffered output bytes to be written out to the stream.

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)

Writes b.length bytes to this output stream.

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)

Writes len bytes from the specified byte array starting at offset off to this output stream.

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)

Writes the specified byte to this output stream.

Please see FilterOutputStream(OutputStream out).

Methods declared in OutputStream

nullOutputStream

Please see the link below.


Related posts

To top of page