Class OutputStreamDecorator<O extends java.io.OutputStream>

  • Type Parameters:
    O - The type of output stream being decorated.
    All Implemented Interfaces:
    Disposable, java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    CaptureOutputStream, ProgressOutputStream, TempOutputStream

    public class OutputStreamDecorator<O extends java.io.OutputStream>
    extends java.io.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 Summary

      Constructors 
      Constructor Description
      OutputStreamDecorator​(O outputStream)
      Decorates the given output stream, automatically calling dispose() when closed.
      OutputStreamDecorator​(O outputStream, boolean autoDispose)
      Decorates the given output stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void afterClose()
      Called after the stream is successfully closed.
      protected void beforeClose()
      Called before the stream is closed.
      protected java.io.OutputStream checkOutputStream()
      Checks to make sure the decorated output stream is available.
      void close()  
      void close​(boolean closeDecoratedStream)
      Closes this output stream and releases any system resources associated with the stream.
      void dispose()
      Uninitializes the object.
      protected void finalize()
      void flush()  
      protected O getOutputStream()  
      protected void setOutputStream​(O outputStream)
      Changes the decorated output stream.
      void write​(byte[] b)  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OutputStreamDecorator

        public OutputStreamDecorator​(O outputStream)
        Decorates the given output stream, automatically calling dispose() when closed.
        Parameters:
        outputStream - The output stream to decorate.
        Throws:
        java.lang.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:
        java.lang.NullPointerException - if the given stream is null.
    • Method Detail

      • 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:
        java.lang.NullPointerException - if the given output stream is null.
      • write

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

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

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

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

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

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

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

        public void close​(boolean closeDecoratedStream)
                   throws java.io.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:
        java.io.IOException - if an I/O error occurs.
        See Also:
        beforeClose(), afterClose(), dispose()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.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 java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Implementation Specification:
        This version calls dispose().
        Throws:
        java.lang.Throwable