Class InputStreamDecorator<I extends InputStream>

java.lang.Object
java.io.InputStream
com.globalmentor.io.InputStreamDecorator<I>
Type Parameters:
I - The type of input stream being decorated.
All Implemented Interfaces:
Disposable, Closeable, AutoCloseable
Direct Known Subclasses:
CaptureInputStream, FixedLengthInputStream, TempFileInputStream

public class InputStreamDecorator<I extends InputStream> extends InputStream implements Disposable
Wraps an existing input stream. The decorated input stream is released when this stream is closed. This decorator provides convenience methods beforeClose() and afterClose() called before and after the stream is closed, respectively.
Author:
Garret Wilson
  • Constructor Summary

    Constructors
    Constructor
    Description
    InputStreamDecorator(I inputStream)
    Decorates the given input stream, automatically calling dispose() when closed.
    InputStreamDecorator(I inputStream, boolean autoDispose)
    Decorates the given input stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Called after the stream is successfully closed.
    int
     
    protected void
    Called before the stream is closed.
    void
    Closes this input stream and releases any system resources associated with the stream.
    void
    close(boolean closeDecoratedStream)
    Closes this input stream and releases any system resources associated with the stream.
    void
    Uninitializes the object.
    protected void
    protected I
     
    void
    mark(int readlimit)
     
    boolean
     
    int
     
    int
    read(byte[] b)
     
    int
    read(byte[] b, int off, int len)
     
    void
     
    long
    skip(long n)
     

    Methods inherited from class java.lang.Object

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

    • InputStreamDecorator

      public InputStreamDecorator(I inputStream)
      Decorates the given input stream, automatically calling dispose() when closed.
      Parameters:
      inputStream - The input stream to decorate.
      Throws:
      NullPointerException - if the given stream is null.
    • InputStreamDecorator

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

    • getInputStream

      protected I getInputStream()
      Returns:
      The input stream being decorated, or null if it has been released after this stream was closed.
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • 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 input 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
      Closes this input stream and releases any system resources associated with the stream. A closed stream cannot perform output operations and cannot be reopened.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • 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 input 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