|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.io.StreamDrainerForwarding
public class StreamDrainerForwarding
StreamDrainer implementation that immediately forwards all bytes read from its InputStream to another OutputStream.
This class is multithread safe: almost every method is synchronized.
The sole exception is run
, which internally synchronizes those code blocks which involve mutable state
using the same lock as the other methods (i.e. the instance itself).
Nested Class Summary | |
---|---|
static class |
StreamDrainerForwarding.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private static int |
bufferSize
|
private static byte[] |
bytesEmpty
|
private InputStream |
in
|
private OutputStream |
out
|
private boolean |
runCalled
|
private Throwable |
throwable
|
Constructor Summary | |
---|---|
StreamDrainerForwarding(InputStream in,
OutputStream out)
Constructor that assigns out with the corresponding parameter,
and calls init with the in parameter. |
|
StreamDrainerForwarding(OutputStream out)
Constructor that simply assigns out with the correspondingparameter. |
Method Summary | |
---|---|
byte[] |
getBytes()
Because this class never stores the bytes drained by run ,
this method always returns an empty byte[]. |
Throwable |
getThrowable()
Returns any Throwable caught by run while it was draining that it could not handle. |
void |
init(InputStream in)
Assigns the InputStream that this instance must drain. |
protected void |
onBytesRead(byte[] buffer,
int numberRead)
Hook method for the event that bytes were read from in by run . |
void |
run()
Drains the InputStream supplied to init . |
private void |
setThrowable(Throwable throwable)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final int bufferSize
private static final byte[] bytesEmpty
private final OutputStream out
private InputStream in
private boolean runCalled
private Throwable throwable
Constructor Detail |
---|
public StreamDrainerForwarding(OutputStream out) throws IllegalArgumentException
out
with the correspondingparameter.
A subsequent call to init
must be made before run
can be executed.
IllegalArgumentException
public StreamDrainerForwarding(InputStream in, OutputStream out) throws IllegalArgumentException
out
with the corresponding parameter,
and calls init
with the in parameter.
This is a convenience, because it relieves the user of having to make an additional call to init.
IllegalArgumentException
- if out is null; in is nullMethod Detail |
---|
public void init(InputStream in) throws IllegalArgumentException, IllegalStateException
StreamDrainer
Note that some implementations may have a constructor which takes an InputStream arg. In these cases, that constructor must call this method and the user should not subsequently call it again.
init
in interface StreamDrainer
IllegalArgumentException
- if in is null
IllegalStateException
- if called more than oncepublic void run() throws IllegalStateException
init
.
The fate of the drained bytes is implementation dependent (see getBytes
).
Other than the IllegalStateException described below,
this method guarantees to never throw any Throwable once draining has started.
Instead, if a Throwable is thrown that cannot be internally handled,
this method guarantees to store it for future retrieval by getThrowable
before aborting execution.
Note: when finished draining, will flush (but not close) out
.
run
in interface StreamDrainer
run
in interface Runnable
IllegalStateException
- if init has not been called yet; this method is called more than onceprotected void onBytesRead(byte[] buffer, int numberRead)
in
by run
.
When this method is called, run will have already written those bytes to out
.
The implementation here does nothing, because writing to out is all that this class does.
Subclasses, however, may wish to do additional processing.
For example, if the subclass is monitoring some error stream like System.err,
then it may wish to do additional error notification.
public byte[] getBytes() throws IllegalStateException
run
,
this method always returns an empty byte[].
getBytes
in interface StreamDrainer
IllegalStateException
- if run has never been calledpublic Throwable getThrowable() throws IllegalStateException
StreamDrainer
run
while it was draining that it could not handle.
getThrowable
in interface StreamDrainer
IllegalStateException
- if run has never been calledprivate void setThrowable(Throwable throwable)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |