bb.io
Class StreamDrainerStoring

java.lang.Object
  extended by bb.io.StreamDrainerStoring
All Implemented Interfaces:
StreamDrainer, Runnable

public class StreamDrainerStoring
extends Object
implements StreamDrainer

StreamDrainer implementation that stores all bytes read from its InputStream.

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).

Author:
Brent Boyer

Nested Class Summary
static class StreamDrainerStoring.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  ByteArrayOutputStream baos
           
private static int baosSizeInitial
           
private static int bufferSize
           
private  InputStream in
           
private  Throwable throwable
           
 
Constructor Summary
StreamDrainerStoring()
          No-arg constructor.
StreamDrainerStoring(InputStream in)
          Constructor that calls init with in.
 
Method Summary
 byte[] getBytes()
          Returns all the bytes that have been drained by run since the last time this method was called.
 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.
 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

baosSizeInitial

private static final int baosSizeInitial
See Also:
Constant Field Values

bufferSize

private static final int bufferSize
See Also:
Constant Field Values

in

private InputStream in

baos

private ByteArrayOutputStream baos

throwable

private Throwable throwable
Constructor Detail

StreamDrainerStoring

public StreamDrainerStoring()
No-arg constructor. A subsequent call to init must be made before run can be executed.


StreamDrainerStoring

public StreamDrainerStoring(InputStream in)
                     throws IllegalArgumentException
Constructor that calls init with in. This is a convenience, because it relieves the user of having to make an additional call to init.

Throws:
IllegalArgumentException - if in is null
Method Detail

init

public void init(InputStream in)
          throws IllegalArgumentException,
                 IllegalStateException
Description copied from interface: StreamDrainer
Assigns the InputStream that this instance must drain.

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.

Specified by:
init in interface StreamDrainer
Throws:
IllegalArgumentException - if in is null
IllegalStateException - if called more than once

run

public void run()
         throws IllegalStateException
Description copied from interface: StreamDrainer
Drains the InputStream supplied to 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.

Specified by:
run in interface StreamDrainer
Specified by:
run in interface Runnable
Throws:
IllegalStateException - if init has not been called yet; this method is called more than once

getBytes

public byte[] getBytes()
                throws IllegalStateException
Returns all the bytes that have been drained by run since the last time this method was called.

Side effect: any stored bytes are cleared upon return.

Specified by:
getBytes in interface StreamDrainer
Returns:
all the bytes drained by run since the last call of this method; the result is never null, but may be zero-length
Throws:
IllegalStateException - if run has never been called

getThrowable

public Throwable getThrowable()
                       throws IllegalStateException
Description copied from interface: StreamDrainer
Returns any Throwable caught by run while it was draining that it could not handle.

Specified by:
getThrowable in interface StreamDrainer
Returns:
the Throwable which aborted the draining; is null if no such Throwable occurred
Throws:
IllegalStateException - if run has never been called

setThrowable

private void setThrowable(Throwable throwable)