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.AbstractBuilder<T extends ObservableInputStream.AbstractBuilder<T>>
For subclassing builders from
BoundedInputStream subclassses. |
static class |
ObservableInputStream.Builder
Builds instances of
ObservableInputStream. |
static class |
ObservableInputStream.Observer
Abstracts observer callback for
ObservableInputStreams. |
in| Constructor and Description |
|---|
ObservableInputStream(InputStream inputStream)
Constructs a new ObservableInputStream for the given InputStream.
|
ObservableInputStream(InputStream inputStream,
ObservableInputStream.Observer... observers)
Constructs a new ObservableInputStream for the given InputStream.
|
ObservableInputStream(ObservableInputStream.AbstractBuilder<?> builder) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(ObservableInputStream.Observer observer)
Adds an Observer.
|
void |
close()
Invokes the delegate's
InputStream.close() method. |
void |
consume()
Reads all data from the underlying
InputStream, while notifying the observers. |
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(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(). |
int |
read()
Invokes the delegate's
InputStream.read() method unless the stream is closed. |
int |
read(byte[] buffer)
Invokes the delegate's
InputStream.read(byte[]) method. |
int |
read(byte[] buffer,
int offset,
int length)
Invokes the delegate's
InputStream.read(byte[], int, int) method. |
void |
remove(ObservableInputStream.Observer observer)
Removes an Observer.
|
void |
removeAllObservers()
Removes all Observers.
|
afterRead, available, beforeRead, checkOpen, handleIOException, isClosed, mark, markSupported, reset, setReference, skip, unwrappublic ObservableInputStream(InputStream inputStream)
inputStream - the input stream to observe.public ObservableInputStream(InputStream inputStream, ObservableInputStream.Observer... observers)
inputStream - the input stream to observe.observers - List of observer callbacks.ObservableInputStream(ObservableInputStream.AbstractBuilder<?> builder) throws IOException
IOExceptionpublic void add(ObservableInputStream.Observer observer)
observer - the observer to add.public void close()
throws IOException
ProxyInputStreamInputStream.close() method.close in interface Closeableclose in interface AutoCloseableclose in class ProxyInputStreamIOException - if an I/O error occurs.public void consume()
throws IOException
InputStream, while notifying the observers.IOException - The underlying InputStream, or either of the observers has thrown an exception.public List<ObservableInputStream.Observer> getObservers()
protected void noteClosed()
throws IOException
ObservableInputStream.Observer.finished().IOException - Some observer has thrown an exception, which is being passed down.protected void noteDataByte(int value)
throws IOException
ObservableInputStream.Observer.data(int) with the given arguments.value - Passed to the observers.IOException - Some observer has thrown an exception, which is being passed down.protected void noteDataBytes(byte[] buffer,
int offset,
int length)
throws 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.IOException - Some observer has thrown an exception, which is being passed down.protected void noteError(IOException exception) throws IOException
ObservableInputStream.Observer.error(IOException) with the given argument.exception - Passed to the observers.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 IOException
ObservableInputStream.Observer.finished().IOException - Some observer has thrown an exception, which is being passed down.public int read()
throws IOException
ProxyInputStreamInputStream.read() method unless the stream is closed.read in class ProxyInputStreamEOF if we reached the end of stream.IOException - if an I/O error occurs.public int read(byte[] buffer)
throws IOException
ProxyInputStreamInputStream.read(byte[]) method.read in class ProxyInputStreambuffer - the buffer to read the bytes into.EOF if we reached the end of stream.IOException - public int read(byte[] buffer,
int offset,
int length)
throws IOException
ProxyInputStreamInputStream.read(byte[], int, int) method.read in class ProxyInputStreambuffer - the buffer to read the bytes into.offset - The start offset.length - The number of bytes to read.EOF if we reached the end of stream.IOException - public void remove(ObservableInputStream.Observer observer)
observer - the observer to removepublic void removeAllObservers()