|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.OutputStream
java.io.FilterOutputStream
org.dellroad.stuff.io.AsyncOutputStream
public class AsyncOutputStream
An OutputStream that performs writes using a background thread, so that
write, flush, and close operations never block.
If the underlying output stream throws an IOException during any operation,
this instance will re-throw the exception for all subsequent operations.
Instances use an internal buffer whose size is configured at construction time;
if the buffer overflows, a BufferOverflowException is thrown.
Instances of this class are thread safe, and moreover writes are atomic: if multiple threads are writing at the same time the bytes written in any single method invocation are written contiguously to the underlying output.
| Field Summary | |
|---|---|
protected Logger |
log
|
| Fields inherited from class java.io.FilterOutputStream |
|---|
out |
| Constructor Summary | |
|---|---|
AsyncOutputStream(OutputStream out,
int bufsize,
String name)
Constructor. |
|
| Method Summary | |
|---|---|
int |
availableBufferSpace()
Get the number of free bytes remaining in the output buffer. |
void |
close()
Close this instance. |
void |
flush()
Flush output. |
int |
getBufferSize()
Get the capacity of this instance's output buffer. |
IOException |
getException()
Get the exception thrown by the underlying output stream, if any. |
boolean |
isWorkOutstanding()
Determine if there is outstanding work still to be performed (writes, flushes, and/or close operations) by the background thread. |
boolean |
waitForIdle(long timeout)
Wait for all outstanding work to complete. |
boolean |
waitForSpace(int numBytes,
long timeout)
Wait for buffer space availability. |
void |
write(byte[] data,
int off,
int len)
Write data. |
void |
write(int b)
Write data. |
| Methods inherited from class java.io.FilterOutputStream |
|---|
write |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected final Logger log
| Constructor Detail |
|---|
public AsyncOutputStream(OutputStream out,
int bufsize,
String name)
out - underlying output streambufsize - maximum number of bytes we can buffername - name for this instance; used to create the name of the background thread| Method Detail |
|---|
public void write(int b)
throws IOException
This method will never block. To effect a normal blocking write, use waitForSpace(int, long) first.
write in class FilterOutputStreamb - byte to write (lower 8 bits)
IOException - if an exception has been thrown by the underlying stream
IOException - if this instance has been closed
BufferOverflowException - if the buffer does not have room for the new byte
public void write(byte[] data,
int off,
int len)
throws IOException
This method will never block. To effect a normal blocking write, invoke waitForSpace(int, long) first.
write in class FilterOutputStreamdata - bytes to writeoff - starting offset in bufferlen - number of bytes to write
IOException - if an exception has been thrown by the underlying stream
IOException - if this instance has been closed
BufferOverflowException - if the buffer does not have room for the new data
IllegalArgumentException - if len is negative
public void flush()
throws IOException
If additional data is written and then a second flush is requested before the first flush has actually occurred, the first flush will be canceled and only the second flush will be applied. Normally this is not a problem because the act of writing more data and then flushing forces earlier data to be flushed as well.
This method will never block. To block until the underlying flush operation completes, invoke waitForIdle(long).
flush in interface Flushableflush in class FilterOutputStreamIOException - if this instance has been closed
IOException - if an exception has been detected on the underlying stream
IOException - if the current thread is interrupted; the nested exception will an InterruptedException
public void close()
throws IOException
If this instance has already been closed, nothing happens.
This method will never block. To block until the underlying close operation completes, invoke waitForIdle(long).
close in interface Closeableclose in class FilterOutputStreamIOException - if an exception has been detected on the underlying streampublic IOException getException()
null if none has been thrown by the underlying streampublic int getBufferSize()
public int availableBufferSpace()
throws IOException
IOException - if this instance is or has been closed
IOException - if an exception has been detected on the underlying streamwaitForSpace(int, long)
public boolean isWorkOutstanding()
throws IOException
IOException - if this instance is or has been closed
IOException - if an exception has been detected on the underlying streamwaitForIdle(long)
public boolean waitForSpace(int numBytes,
long timeout)
throws IOException,
InterruptedException
numBytes - amount of buffer space requiredtimeout - maximum time to wait in milliseconds, or zero for infinite
IOException - if this instance is or has been closed
IOException - if an exception has been detected on the underlying stream
IllegalArgumentException - if numBytes is greater than the configured buffer size
IllegalArgumentException - if timeout is negative
InterruptedException - if the current thread is interruptedavailableBufferSpace()
public boolean waitForIdle(long timeout)
throws IOException,
InterruptedException
timeout - maximum time to wait in milliseconds, or zero for infinite
IOException - if this instance is or has been closed
IOException - if an exception has been detected on the underlying stream
IllegalArgumentException - if timeout is negative
InterruptedException - if the current thread is interruptedisWorkOutstanding()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||