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.MessageDigestCalculatingInputStream
Modifier and Type | Class and Description |
---|---|
static class |
ObservableInputStream.Observer |
in
Constructor and Description |
---|
ObservableInputStream(InputStream pProxy)
Creates a new ObservableInputStream for the given InputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
add(ObservableInputStream.Observer pObserver)
Adds an Observer.
|
void |
close()
Invokes the delegate's
close() method. |
void |
consume()
Reads all data from the underlying
InputStream , while notifying the
observers. |
protected List<ObservableInputStream.Observer> |
getObservers()
Gets all currently registered observers.
|
protected void |
noteClosed()
Notifies the observers by invoking
ObservableInputStream.Observer.finished() . |
protected void |
noteDataByte(int pDataByte)
Notifies the observers by invoking
ObservableInputStream.Observer.data(int)
with the given arguments. |
protected void |
noteDataBytes(byte[] pBuffer,
int pOffset,
int pLength)
Notifies the observers by invoking
ObservableInputStream.Observer.data(byte[],int,int)
with the given arguments. |
protected void |
noteError(IOException pException)
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
read() method. |
int |
read(byte[] pBuffer)
Invokes the delegate's
read(byte[]) method. |
int |
read(byte[] pBuffer,
int pOffset,
int pLength)
Invokes the delegate's
read(byte[], int, int) method. |
void |
remove(ObservableInputStream.Observer pObserver)
Removes an Observer.
|
void |
removeAllObservers()
Removes all Observers.
|
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip
public ObservableInputStream(InputStream pProxy)
pProxy
- the input stream to proxypublic void add(ObservableInputStream.Observer pObserver)
pObserver
- the observer to addpublic void remove(ObservableInputStream.Observer pObserver)
pObserver
- the observer to removepublic void removeAllObservers()
public int read() throws IOException
ProxyInputStream
read()
method.read
in class ProxyInputStream
IOException
- if an I/O error occurspublic int read(byte[] pBuffer) throws IOException
ProxyInputStream
read(byte[])
method.read
in class ProxyInputStream
pBuffer
- the buffer to read the bytes intoIOException
- if an I/O error occurspublic int read(byte[] pBuffer, int pOffset, int pLength) throws IOException
ProxyInputStream
read(byte[], int, int)
method.read
in class ProxyInputStream
pBuffer
- the buffer to read the bytes intopOffset
- The start offsetpLength
- The number of bytes to readIOException
- if an I/O error occursprotected void noteDataBytes(byte[] pBuffer, int pOffset, int pLength) throws IOException
ObservableInputStream.Observer.data(byte[],int,int)
with the given arguments.pBuffer
- Passed to the observers.pOffset
- Passed to the observers.pLength
- Passed to the observers.IOException
- Some observer has thrown an exception, which is being
passed down.protected void noteFinished() throws IOException
ObservableInputStream.Observer.finished()
.IOException
- Some observer has thrown an exception, which is being
passed down.protected void noteDataByte(int pDataByte) throws IOException
ObservableInputStream.Observer.data(int)
with the given arguments.pDataByte
- Passed to the observers.IOException
- Some observer has thrown an exception, which is being
passed down.protected void noteError(IOException pException) throws IOException
ObservableInputStream.Observer.error(IOException)
with the given argument.pException
- 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 noteClosed() throws IOException
ObservableInputStream.Observer.finished()
.IOException
- Some observer has thrown an exception, which is being
passed down.protected List<ObservableInputStream.Observer> getObservers()
public void close() throws IOException
ProxyInputStream
close()
method.close
in interface Closeable
close
in interface AutoCloseable
close
in class ProxyInputStream
IOException
- if an I/O error occurspublic void consume() throws IOException
InputStream
, while notifying the
observers.IOException
- The underlying InputStream
, or either of the
observers has thrown an exception.Copyright © 2002–2017 The Apache Software Foundation. All rights reserved.