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,MessageDigestInputStream
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:
MessageDigestInputStream
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObservableInputStream.AbstractBuilder<T extends ObservableInputStream.AbstractBuilder<T>>For subclassing builders fromBoundedInputStreamsubclassses.static classObservableInputStream.BuilderBuilds instances ofObservableInputStream.static classObservableInputStream.ObserverAbstracts observer callback forObservableInputStreams.
-
Constructor Summary
Constructors Constructor Description ObservableInputStream(java.io.InputStream inputStream)Constructs a new ObservableInputStream for the given InputStream.ObservableInputStream(java.io.InputStream inputStream, ObservableInputStream.Observer... observers)Constructs 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'sInputStream.close()method.voidconsume()Reads all data from the underlyingInputStream, while notifying the observers.java.util.List<ObservableInputStream.Observer>getObservers()Gets a copy of currently registered observers.protected voidnoteClosed()Notifies the observers by invokingObservableInputStream.Observer.finished().protected voidnoteDataByte(int value)Notifies the observers by invokingObservableInputStream.Observer.data(int)with the given arguments.protected voidnoteDataBytes(byte[] buffer, int offset, int length)Notifies the observers by invokingObservableInputStream.Observer.data(byte[],int,int)with the given arguments.protected voidnoteError(java.io.IOException exception)Notifies the observers by invokingObservableInputStream.Observer.error(IOException)with the given argument.protected voidnoteFinished()Notifies the observers by invokingObservableInputStream.Observer.finished().intread()Invokes the delegate'sInputStream.read()method unless the stream is closed.intread(byte[] buffer)Invokes the delegate'sInputStream.read(byte[])method.intread(byte[] buffer, int offset, int length)Invokes the delegate'sInputStream.read(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
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, setReference, skip, unwrap
-
-
-
-
Constructor Detail
-
ObservableInputStream
public ObservableInputStream(java.io.InputStream inputStream)
Constructs a new ObservableInputStream for the given InputStream.- Parameters:
inputStream- the input stream to observe.
-
ObservableInputStream
public ObservableInputStream(java.io.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 Detail
-
add
public void add(ObservableInputStream.Observer observer)
Adds an Observer.- Parameters:
observer- the observer to add.
-
close
public void close() throws java.io.IOException
Description copied from class:ProxyInputStreamInvokes the delegate'sInputStream.close()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.IOException
Reads 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 a copy of currently registered observers.- Returns:
- a copy of the list of currently registered observers.
- Since:
- 2.9.0
-
noteClosed
protected void noteClosed() throws java.io.IOException
Notifies the observers by invokingObservableInputStream.Observer.finished().- Throws:
java.io.IOException- Some observer has thrown an exception, which is being passed down.
-
noteDataByte
protected void noteDataByte(int value) throws java.io.IOException
Notifies the observers by invokingObservableInputStream.Observer.data(int)with the given arguments.- Parameters:
value- Passed to the observers.- Throws:
java.io.IOException- Some observer has thrown an exception, which is being passed down.
-
noteDataBytes
protected void noteDataBytes(byte[] buffer, int offset, int length) throws java.io.IOException
Notifies the observers by invokingObservableInputStream.Observer.data(byte[],int,int)with the given arguments.- Parameters:
buffer- Passed to the observers.offset- Passed to the observers.length- Passed to the observers.- Throws:
java.io.IOException- Some observer has thrown an exception, which is being passed down.
-
noteError
protected void noteError(java.io.IOException exception) throws java.io.IOException
Notifies the observers by invokingObservableInputStream.Observer.error(IOException)with the given argument.- Parameters:
exception- Passed to the observers.- Throws:
java.io.IOException- Some observer has thrown an exception, which is being passed down. This may be the same exception, which has been passed as an argument.
-
noteFinished
protected void noteFinished() throws java.io.IOException
Notifies the observers by invokingObservableInputStream.Observer.finished().- Throws:
java.io.IOException- Some observer has thrown an exception, which is being passed down.
-
read
public int read() throws java.io.IOException
Description copied from class:ProxyInputStreamInvokes the delegate'sInputStream.read()method unless the stream is closed.- Overrides:
readin classProxyInputStream- Returns:
- the byte read or
EOFif we reached the end of stream. - Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] buffer) throws java.io.IOException
Description copied from class:ProxyInputStreamInvokes the delegate'sInputStream.read(byte[])method.- Overrides:
readin classProxyInputStream- Parameters:
buffer- the buffer to read the bytes into.- Returns:
- the number of bytes read or
EOFif we reached the end of stream. - Throws:
java.io.IOException-- If the first byte cannot be read for any reason other than the end of the file,
- if the input stream has been closed, or
- if some other I/O error occurs.
-
read
public int read(byte[] buffer, int offset, int length) throws java.io.IOException
Description copied from class:ProxyInputStreamInvokes the delegate'sInputStream.read(byte[], int, int)method.- Overrides:
readin classProxyInputStream- 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
EOFif we reached the end of stream. - Throws:
java.io.IOException-- If the first byte cannot be read for any reason other than the end of the file,
- if the input stream has been closed, or
- if some other 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.
-
-