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:
Closeable
,AutoCloseable
- Direct Known Subclasses:
MessageDigestCalculatingInputStream
,MessageDigestInputStream
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
For subclassing builders fromBoundedInputStream
subclassses.static class
Builds instances ofObservableInputStream
.static class
Abstracts observer callback forObservableInputStream
s. -
Field Summary
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorDescriptionObservableInputStream
(InputStream inputStream) Constructs a new ObservableInputStream for the given InputStream.ObservableInputStream
(InputStream inputStream, ObservableInputStream.Observer... observers) Constructs a new ObservableInputStream for the given InputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(ObservableInputStream.Observer observer) Adds an Observer.void
close()
Invokes the delegate'sInputStream.close()
method.void
consume()
Reads all data from the underlyingInputStream
, while notifying the observers.Gets a copy of currently registered observers.protected void
Notifies the observers by invokingObservableInputStream.Observer.finished()
.protected void
noteDataByte
(int value) Notifies the observers by invokingObservableInputStream.Observer.data(int)
with the given arguments.protected void
noteDataBytes
(byte[] buffer, int offset, int length) Notifies the observers by invokingObservableInputStream.Observer.data(byte[],int,int)
with the given arguments.protected void
noteError
(IOException exception) Notifies the observers by invokingObservableInputStream.Observer.error(IOException)
with the given argument.protected void
Notifies the observers by invokingObservableInputStream.Observer.finished()
.int
read()
Invokes the delegate'sInputStream.read()
method unless the stream is closed.int
read
(byte[] buffer) Invokes the delegate'sInputStream.read(byte[])
method.int
read
(byte[] buffer, int offset, int length) Invokes the delegate'sInputStream.read(byte[], int, int)
method.void
remove
(ObservableInputStream.Observer observer) Removes an Observer.void
Removes all Observers.Methods inherited from class org.apache.commons.io.input.ProxyInputStream
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip, unwrap
-
Constructor Details
-
ObservableInputStream
Constructs a new ObservableInputStream for the given InputStream.- Parameters:
inputStream
- the input stream to observe.
-
ObservableInputStream
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
Adds an Observer.- Parameters:
observer
- the observer to add.
-
close
Description copied from class:ProxyInputStream
Invokes the delegate'sInputStream.close()
method.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classProxyInputStream
- Throws:
IOException
- if an I/O error occurs.
-
consume
Reads all data from the underlyingInputStream
, while notifying the observers.- Throws:
IOException
- The underlyingInputStream
, or either of the observers has thrown an exception.
-
getObservers
Gets a copy of currently registered observers.- Returns:
- a copy of the list of currently registered observers.
- Since:
- 2.9.0
-
noteClosed
Notifies the observers by invokingObservableInputStream.Observer.finished()
.- Throws:
IOException
- Some observer has thrown an exception, which is being passed down.
-
noteDataByte
Notifies the observers by invokingObservableInputStream.Observer.data(int)
with the given arguments.- Parameters:
value
- Passed to the observers.- Throws:
IOException
- Some observer has thrown an exception, which is being passed down.
-
noteDataBytes
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:
IOException
- Some observer has thrown an exception, which is being passed down.
-
noteError
Notifies the observers by invokingObservableInputStream.Observer.error(IOException)
with the given argument.- Parameters:
exception
- Passed to the observers.- Throws:
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
Notifies the observers by invokingObservableInputStream.Observer.finished()
.- Throws:
IOException
- Some observer has thrown an exception, which is being passed down.
-
read
Description copied from class:ProxyInputStream
Invokes the delegate'sInputStream.read()
method unless the stream is closed.- Overrides:
read
in classProxyInputStream
- Returns:
- the byte read or
EOF
if we reached the end of stream. - Throws:
IOException
- if an I/O error occurs.
-
read
Description copied from class:ProxyInputStream
Invokes the delegate'sInputStream.read(byte[])
method.- Overrides:
read
in classProxyInputStream
- Parameters:
buffer
- the buffer to read the bytes into.- Returns:
- the number of bytes read or
EOF
if we reached the end of stream. - Throws:
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
Description copied from class:ProxyInputStream
Invokes the delegate'sInputStream.read(byte[], int, int)
method.- Overrides:
read
in 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
EOF
if we reached the end of stream. - Throws:
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
Removes an Observer.- Parameters:
observer
- the observer to remove
-
removeAllObservers
Removes all Observers.
-