public class ObservableInputStream extends ProxyInputStream
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.
MessageDigestInputStream
Modifier and Type | Class and Description |
---|---|
static class |
ObservableInputStream.Observer
Abstracts observer callback for
ObservableInputStream s. |
Modifier and Type | Field and Description |
---|---|
private java.util.List<ObservableInputStream.Observer> |
observers |
Modifier | Constructor and Description |
---|---|
|
ObservableInputStream(java.io.InputStream inputStream)
Constructs a new ObservableInputStream for the given InputStream.
|
private |
ObservableInputStream(java.io.InputStream inputStream,
java.util.List<ObservableInputStream.Observer> observers)
Constructs a new ObservableInputStream for the given InputStream.
|
|
ObservableInputStream(java.io.InputStream inputStream,
ObservableInputStream.Observer... observers)
Constructs a new ObservableInputStream for the given InputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
add(ObservableInputStream.Observer observer)
Adds an Observer.
|
void |
close()
Invokes the delegate's
close() method. |
void |
consume()
Reads all data from the underlying
InputStream , while notifying the observers. |
private void |
forEachObserver(IOConsumer<ObservableInputStream.Observer> action) |
java.util.List<ObservableInputStream.Observer> |
getObservers()
Gets a copy of currently registered observers.
|
protected void |
noteClosed()
Notifies the observers by invoking
ObservableInputStream.Observer.finished() . |
protected void |
noteDataByte(int value)
Notifies the observers by invoking
ObservableInputStream.Observer.data(int) with the given arguments. |
protected void |
noteDataBytes(byte[] buffer,
int offset,
int length)
Notifies the observers by invoking
ObservableInputStream.Observer.data(byte[],int,int) with the given arguments. |
protected void |
noteError(java.io.IOException exception)
Notifies the observers by invoking
ObservableInputStream.Observer.error(IOException) with the given argument. |
protected void |
noteFinished()
Notifies the observers by invoking
ObservableInputStream.Observer.finished() . |
private void |
notify(byte[] buffer,
int offset,
int result,
java.io.IOException ioe) |
int |
read()
Invokes the delegate's
read() method. |
int |
read(byte[] buffer)
Invokes the delegate's
read(byte[]) method. |
int |
read(byte[] buffer,
int offset,
int length)
Invokes the delegate's
read(byte[], int, int) method. |
void |
remove(ObservableInputStream.Observer observer)
Removes an Observer.
|
void |
removeAllObservers()
Removes all Observers.
|
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip
private final java.util.List<ObservableInputStream.Observer> observers
public ObservableInputStream(java.io.InputStream inputStream)
inputStream
- the input stream to observe.private ObservableInputStream(java.io.InputStream inputStream, java.util.List<ObservableInputStream.Observer> observers)
inputStream
- the input stream to observe.observers
- List of observer callbacks.public ObservableInputStream(java.io.InputStream inputStream, ObservableInputStream.Observer... observers)
inputStream
- the input stream to observe.observers
- List of observer callbacks.public void add(ObservableInputStream.Observer observer)
observer
- the observer to add.public void close() throws java.io.IOException
ProxyInputStream
close()
method.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class ProxyInputStream
java.io.IOException
- if an I/O error occurs.public void consume() throws java.io.IOException
InputStream
, while notifying the observers.java.io.IOException
- The underlying InputStream
, or either of the observers has thrown an exception.private void forEachObserver(IOConsumer<ObservableInputStream.Observer> action) throws java.io.IOException
java.io.IOException
public java.util.List<ObservableInputStream.Observer> getObservers()
protected void noteClosed() throws java.io.IOException
ObservableInputStream.Observer.finished()
.java.io.IOException
- Some observer has thrown an exception, which is being passed down.protected void noteDataByte(int value) throws java.io.IOException
ObservableInputStream.Observer.data(int)
with the given arguments.value
- Passed to the observers.java.io.IOException
- Some observer has thrown an exception, which is being passed down.protected void noteDataBytes(byte[] buffer, int offset, int length) throws java.io.IOException
ObservableInputStream.Observer.data(byte[],int,int)
with the given arguments.buffer
- Passed to the observers.offset
- Passed to the observers.length
- Passed to the observers.java.io.IOException
- Some observer has thrown an exception, which is being passed down.protected void noteError(java.io.IOException exception) throws java.io.IOException
ObservableInputStream.Observer.error(IOException)
with the given argument.exception
- Passed to the observers.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.protected void noteFinished() throws java.io.IOException
ObservableInputStream.Observer.finished()
.java.io.IOException
- Some observer has thrown an exception, which is being passed down.private void notify(byte[] buffer, int offset, int result, java.io.IOException ioe) throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
ProxyInputStream
read()
method.read
in class ProxyInputStream
java.io.IOException
- if an I/O error occurs.public int read(byte[] buffer) throws java.io.IOException
ProxyInputStream
read(byte[])
method.read
in class ProxyInputStream
buffer
- the buffer to read the bytes intojava.io.IOException
- if an I/O error occurs.public int read(byte[] buffer, int offset, int length) throws java.io.IOException
ProxyInputStream
read(byte[], int, int)
method.read
in class ProxyInputStream
buffer
- the buffer to read the bytes intooffset
- The start offsetlength
- The number of bytes to readjava.io.IOException
- if an I/O error occurs.public void remove(ObservableInputStream.Observer observer)
observer
- the observer to removepublic void removeAllObservers()