FilterOutputStream applies a composition template where it delegates all calls to its out instance variable:
protected OutputStream out;
FilterOutputStream also has standard implementations for the abstract class OutputStream :
public void write(int b) throws IOException { out.write(b); } public void write(byte b[]) throws IOException { write(b, 0, b.length); } public void write(byte b[], int off, int len) throws IOException { if ((off | len | (b.length - (len + off)) | (off + len)) < 0) throw new IndexOutOfBoundsException(); for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } public void flush() throws IOException { out.flush(); } public void close() throws IOException { try { flush(); } catch (IOException ignored) { } out.close(); }
Now, any class that includes FilterOutputStream can extend FilterOutputStream and override the appropriate methods. Note that they still need to delegate their calls to out . For example PrintStream#flush() :
public void flush() { synchronized (this) { try { ensureOpen(); out.flush(); } catch (IOException x) { trouble = true; } } }
source share