Class ObservableInputStream

All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
MessageDigestCalculatingInputStream, MessageDigestInputStream

public class ObservableInputStream extends ProxyInputStream
The ObservableInputStream allows, that an InputStream may be consumed by other receivers, apart from the thread, which is reading it. The other consumers are implemented as instances of ObservableInputStream.Observer.

A typical application may be the generation of a MessageDigest on the fly.

Note: The ObservableInputStream is not thread safe, as instances of InputStream usually aren't. If you must access the stream from multiple threads, then synchronization, locking, or a similar means must be used.

See Also:
  • Constructor Details

    • ObservableInputStream

      public ObservableInputStream(InputStream inputStream)
      Constructs a new ObservableInputStream for the given InputStream.
      Parameters:
      inputStream - the input stream to observe.
    • ObservableInputStream

      public ObservableInputStream(InputStream inputStream, ObservableInputStream.Observer... observers)
      Constructs a new ObservableInputStream for the given InputStream.
      Parameters:
      inputStream - the input stream to observe.
      observers - List of observer callbacks.
      Since:
      2.9.0
  • Method Details

    • add

      public void add(ObservableInputStream.Observer observer)
      Adds an Observer.
      Parameters:
      observer - the observer to add.
    • close

      public void close() throws IOException
      Description copied from class: ProxyInputStream
      Invokes the delegate's close() method.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ProxyInputStream
      Throws:
      IOException - if an I/O error occurs.
    • consume

      public void consume() throws IOException
      Reads all data from the underlying InputStream, while notifying the observers.
      Throws:
      IOException - The underlying InputStream, or either of the observers has thrown an exception.
    • getObservers

      public List<ObservableInputStream.Observer> getObservers()
      Gets a copy of currently registered observers.
      Returns:
      a copy of the list of currently registered observers.
      Since:
      2.9.0
    • read

      public int read() throws IOException
      Description copied from class: ProxyInputStream
      Invokes the delegate's read() method.
      Overrides:
      read in class ProxyInputStream
      Returns:
      the byte read or -1 if the end of stream
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(byte[] buffer) throws IOException
      Description copied from class: ProxyInputStream
      Invokes the delegate's read(byte[]) method.
      Overrides:
      read in class ProxyInputStream
      Parameters:
      buffer - the buffer to read the bytes into
      Returns:
      the number of bytes read or EOF if the end of stream
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(byte[] buffer, int offset, int length) throws IOException
      Description copied from class: ProxyInputStream
      Invokes the delegate's read(byte[], int, int) method.
      Overrides:
      read in class ProxyInputStream
      Parameters:
      buffer - the buffer to read the bytes into
      offset - The start offset
      length - The number of bytes to read
      Returns:
      the number of bytes read or -1 if the end of stream
      Throws:
      IOException - if an I/O error occurs.
    • remove

      public void remove(ObservableInputStream.Observer observer)
      Removes an Observer.
      Parameters:
      observer - the observer to remove
    • removeAllObservers

      public void removeAllObservers()
      Removes all Observers.