Package org.apache.commons.io.input
Class ObservableInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- org.apache.commons.io.input.ObservableInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
MessageDigestCalculatingInputStream
public class ObservableInputStream extends ProxyInputStream
TheObservableInputStreamallows, that an InputStream may be consumed by other receivers, apart from the thread, which is reading it. The other consumers are implemented as instances ofObservableInputStream.Observer.A typical application may be the generation of a
MessageDigeston the fly.Note: The
ObservableInputStreamis 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:
MessageDigestCalculatingInputStream
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObservableInputStream.ObserverAbstracts observer callback forObservableInputStreams.
-
Constructor Summary
Constructors Constructor Description ObservableInputStream(java.io.InputStream inputStream)Creates a new ObservableInputStream for the given InputStream.ObservableInputStream(java.io.InputStream inputStream, ObservableInputStream.Observer... observers)Creates a new ObservableInputStream for the given InputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(ObservableInputStream.Observer observer)Adds an Observer.voidclose()Invokes the delegate'sclose()method.voidconsume()Reads all data from the underlyingInputStream, while notifying the observers.java.util.List<ObservableInputStream.Observer>getObservers()Gets all currently registered observers.intread()Invokes the delegate'sread()method.intread(byte[] buffer)Invokes the delegate'sread(byte[])method.intread(byte[] buffer, int offset, int length)Invokes the delegate'sread(byte[], int, int)method.voidremove(ObservableInputStream.Observer observer)Removes an Observer.voidremoveAllObservers()Removes all Observers.-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
available, mark, markSupported, reset, skip
-
-
-
-
Constructor Detail
-
ObservableInputStream
public ObservableInputStream(java.io.InputStream inputStream)
Creates a new ObservableInputStream for the given InputStream.- Parameters:
inputStream- the input stream to observe.
-
ObservableInputStream
public ObservableInputStream(java.io.InputStream inputStream, ObservableInputStream.Observer... observers)Creates a new ObservableInputStream for the given InputStream.- Parameters:
inputStream- the input stream to observe.observers- List of observer callbacks.- Since:
- 2.9.0
-
-
Method Detail
-
add
public void add(ObservableInputStream.Observer observer)
Adds an Observer.- Parameters:
observer- the observer to add.
-
close
public void close() throws java.io.IOExceptionDescription copied from class:ProxyInputStreamInvokes the delegate'sclose()method.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classProxyInputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
consume
public void consume() throws java.io.IOExceptionReads all data from the underlyingInputStream, while notifying the observers.- Throws:
java.io.IOException- The underlyingInputStream, or either of the observers has thrown an exception.
-
getObservers
public java.util.List<ObservableInputStream.Observer> getObservers()
Gets all currently registered observers.- Returns:
- a list of the currently registered observers.
- Since:
- 2.9.0
-
read
public int read() throws java.io.IOExceptionDescription copied from class:ProxyInputStreamInvokes the delegate'sread()method.- Overrides:
readin classProxyInputStream- Returns:
- the byte read or -1 if the end of stream
- Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] buffer) throws java.io.IOExceptionDescription copied from class:ProxyInputStreamInvokes the delegate'sread(byte[])method.- Overrides:
readin classProxyInputStream- Parameters:
buffer- the buffer to read the bytes into- Returns:
- the number of bytes read or EOF if the end of stream
- Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] buffer, int offset, int length) throws java.io.IOExceptionDescription copied from class:ProxyInputStreamInvokes the delegate'sread(byte[], int, int)method.- Overrides:
readin classProxyInputStream- Parameters:
buffer- the buffer to read the bytes intooffset- The start offsetlength- The number of bytes to read- Returns:
- the number of bytes read or -1 if the end of stream
- Throws:
java.io.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.
-
-