Class ImprovedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    RydePgpSigningOutputStream

    @NotThreadSafe
    public class ImprovedOutputStream
    extends java.io.FilterOutputStream
    OutputStream wrapper that offers some additional magic.
    • Byte counting
    • Always FilterOutputStream.flush() on close()
    • Check expected byte count when closed (Optional)
    • Close original OutputStream when closed (Optional)
    • Overridable onClose() method
    • Throws NullPointerException if written after close()
    See Also:
    ImprovedInputStream, CountingOutputStream
    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      ImprovedOutputStream​(java.lang.String name, java.io.OutputStream out)  
      ImprovedOutputStream​(java.lang.String name, java.io.OutputStream out, boolean shouldClose)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Flushes, logs byte count, checks byte count (optional), closes (optional), and self-sabotages.
      long getBytesWritten()
      Returns the number of bytes that have been written to this stream thus far.
      protected void onClose()
      Overridable method that's called by close().
      void write​(byte[] b)  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.FilterOutputStream

        flush
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

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

      • ImprovedOutputStream

        public ImprovedOutputStream​(java.lang.String name,
                                    @WillCloseWhenClosed
                                    java.io.OutputStream out)
      • ImprovedOutputStream

        public ImprovedOutputStream​(java.lang.String name,
                                    java.io.OutputStream out,
                                    boolean shouldClose)
    • Method Detail

      • getBytesWritten

        public long getBytesWritten()
        Returns the number of bytes that have been written to this stream thus far.
      • write

        @OverridingMethodsMustInvokeSuper
        public void write​(int b)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        FilterOutputStream.write(int)
      • write

        public final void write​(byte[] b)
                         throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        write(byte[], int, int)
      • write

        @OverridingMethodsMustInvokeSuper
        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        FilterOutputStream.write(byte[], int, int)
      • close

        @NonFinalForTesting
        public void close()
                   throws java.io.IOException
        Flushes, logs byte count, checks byte count (optional), closes (optional), and self-sabotages.

        This method may not be overridden, use onClose() instead.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.close()
      • onClose

        protected void onClose()
                        throws java.io.IOException
        Overridable method that's called by close().

        This method does nothing by default.

        Throws:
        java.io.IOException