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
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
Nested ClassesModifier and TypeClassDescriptionstatic class
Abstracts observer callback forObservableInputStream
s. -
Field Summary
FieldsFields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsConstructorDescriptionObservableInputStream
(InputStream pProxy) Creates a new ObservableInputStream for the given InputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(ObservableInputStream.Observer pObserver) Adds an Observer.void
close()
Invokes the delegate'sclose()
method.void
consume()
Reads all data from the underlyingInputStream
, while notifying the observers.protected List<ObservableInputStream.Observer>
Gets all currently registered observers.protected void
Notifies the observers by invokingObservableInputStream.Observer.finished()
.protected void
noteDataByte
(int pDataByte) Notifies the observers by invokingObservableInputStream.Observer.data(int)
with the given arguments.protected void
noteDataBytes
(byte[] pBuffer, int pOffset, int pLength) Notifies the observers by invokingObservableInputStream.Observer.data(byte[],int,int)
with the given arguments.protected void
noteError
(IOException pException) 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'sread()
method.int
read
(byte[] pBuffer) Invokes the delegate'sread(byte[])
method.int
read
(byte[] pBuffer, int pOffset, int pLength) Invokes the delegate'sread(byte[], int, int)
method.void
remove
(ObservableInputStream.Observer pObserver) 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
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
observers
-
-
Constructor Details
-
ObservableInputStream
Creates a new ObservableInputStream for the given InputStream.- Parameters:
pProxy
- the input stream to proxy
-
-
Method Details
-
add
Adds an Observer.- Parameters:
pObserver
- the observer to add
-
remove
Removes an Observer.- Parameters:
pObserver
- the observer to remove
-
removeAllObservers
public void removeAllObservers()Removes all Observers. -
read
Description copied from class:ProxyInputStream
Invokes the delegate'sread()
method.- Overrides:
read
in classProxyInputStream
- Returns:
- the byte read or -1 if the end of stream
- Throws:
IOException
- if an I/O error occurs
-
read
Description copied from class:ProxyInputStream
Invokes the delegate'sread(byte[])
method.- Overrides:
read
in classProxyInputStream
- Parameters:
pBuffer
- 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
Description copied from class:ProxyInputStream
Invokes the delegate'sread(byte[], int, int)
method.- Overrides:
read
in classProxyInputStream
- Parameters:
pBuffer
- the buffer to read the bytes intopOffset
- The start offsetpLength
- 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
-
noteDataBytes
Notifies the observers by invokingObservableInputStream.Observer.data(byte[],int,int)
with the given arguments.- Parameters:
pBuffer
- Passed to the observers.pOffset
- Passed to the observers.pLength
- Passed to the observers.- Throws:
IOException
- Some observer has thrown an exception, which is being passed down.
-
noteFinished
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:
pDataByte
- 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:
pException
- 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.
-
noteClosed
Notifies the observers by invokingObservableInputStream.Observer.finished()
.- Throws:
IOException
- Some observer has thrown an exception, which is being passed down.
-
getObservers
Gets all currently registered observers.- Returns:
- a list of the currently registered observers
-
close
Description copied from class:ProxyInputStream
Invokes the delegate'sclose()
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.
-