Class OutputStreamDecorator<O extends OutputStream>

java.lang.Object
java.io.OutputStream
com.globalmentor.io.OutputStreamDecorator<O>
Type Parameters:
O - The type of output stream being decorated.
All Implemented Interfaces:
Disposable, Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
CaptureOutputStream, ProgressOutputStream, TempOutputStream

public class OutputStreamDecorator<O extends OutputStream> extends OutputStream implements Disposable
Wraps an existing output stream.
Author:
Garret Wilson
API Note:
The decorated output stream is released when this stream is closed.
Implementation Note:
This decorator provides convenience methods beforeClose() and afterClose() called before and after the stream is closed, respectively.
  • Constructor Details

    • OutputStreamDecorator

      public OutputStreamDecorator(O outputStream)
      Decorates the given output stream, automatically calling dispose() when closed.
      Parameters:
      outputStream - The output stream to decorate.
      Throws:
      NullPointerException - if the given stream is null.
    • OutputStreamDecorator

      public OutputStreamDecorator(O outputStream, boolean autoDispose)
      Decorates the given output stream.
      Parameters:
      outputStream - The output stream to decorate.
      autoDispose - Whether the stream should be automatically disposed when closed.
      Throws:
      NullPointerException - if the given stream is null.
  • Method Details

    • getOutputStream

      protected O getOutputStream()
      Returns:
      The output stream being decorated, or null if it has been released after this stream was closed.
    • setOutputStream

      protected void setOutputStream(O outputStream)
      Changes the decorated output stream.

      This method can be used by child classes to change the decorated output stream, but cannot be used to remove the output stream---this can be done only by calling close().

      Parameters:
      outputStream - The new output stream to decorate.
      Throws:
      NullPointerException - if the given output stream is null.
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • checkOutputStream

      protected OutputStream checkOutputStream() throws IOException
      Checks to make sure the decorated output stream is available.
      Returns:
      The decorated output stream.
      Throws:
      IOException - if there is no output stream, indicating that the stream is already closed.
    • beforeClose

      protected void beforeClose() throws IOException
      Called before the stream is closed.
      Throws:
      IOException - if an I/O error occurs.
    • afterClose

      protected void afterClose() throws IOException
      Called after the stream is successfully closed.
      Throws:
      IOException - if an I/O error occurs.
    • close

      public void close(boolean closeDecoratedStream) throws IOException
      Closes this output stream and releases any system resources associated with the stream. A closed stream cannot perform output operations and cannot be reopened. If auto-dispose is enabled, dispose() will be called if closing is successful.
      Parameters:
      closeDecoratedStream - Whether the decorated stream should also be closed.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • dispose

      public void dispose()
      Uninitializes the object. After calling this method, the object should not be used further. This method can be called multiple times without danger. All exceptions should be caught and dealt with in this method. Child classes must call the parent class version.
      Specified by:
      dispose in interface Disposable
      Implementation Specification:
      This version closes the output stream and releases it, if still available.
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Implementation Specification:
      This version calls dispose().
      Throws:
      Throwable