public class AsyncTextStreamWriter extends AsyncAgent
The writer converts available lines and appends the chosen line breaks, encodes the chars
into a ByteRingBuffer
. Then those bytes are written into an OutputStream.
Use AsyncAgent.applyAsync()
or AsyncAgent.startApplyLoopAsync()
to
write lines through getStringRingBuffer()
.
This class is a convenience wrapper around the individual elements of the output stack.
Note: The caller is responsible for opening and closing the stream as needed.
WhenReady
,
AsyncAgent
,
ByteRingBuffer
,
CharRingBuffer
,
AsyncLineJoiner
Constructor and Description |
---|
AsyncTextStreamWriter(java.io.OutputStream outputStream,
StringRingBuffer stringRingBuffer,
CharRingBuffer charRingBuffer,
ByteRingBuffer byteRingBuffer,
TextStreamAsyncOptions textStreamAsyncOptions)
Constructs a new AsyncTextStreamWriter instance to write into the given OutputStream.
|
AsyncTextStreamWriter(java.io.OutputStream outputStream,
TextStreamAsyncOptions textStreamAsyncOptions)
Constructs a new AsyncTextStreamWriter instance to write to the given OutputStream.
|
Modifier and Type | Method and Description |
---|---|
protected void |
action()
"action" function that gets executed as part of the apply loop.
|
protected boolean |
done()
"done" predicate that returns true when this async agent wants to quit the current async loop.
|
StringRingBuffer |
getStringRingBuffer()
Returns the underlying
StringRingBuffer from where text lines are read. |
boolean |
isEmpty()
Checks whether all the underlying ring buffers are empty.
|
boolean |
isEOF()
Checks whether this text stream writer has reached EOF.
|
protected boolean |
ready()
"ready" predicate that returns true when lines can be written to the string ring buffer.
|
applyAsync, isIdle, setIdle, setIdleAndThrow, startApplyLoopAsync
public AsyncTextStreamWriter(java.io.OutputStream outputStream, TextStreamAsyncOptions textStreamAsyncOptions)
outputStream
- An OutputStream to write into.textStreamAsyncOptions
- TextStreamAsyncOptions
that will control all async operations on this instance.public AsyncTextStreamWriter(java.io.OutputStream outputStream, StringRingBuffer stringRingBuffer, CharRingBuffer charRingBuffer, ByteRingBuffer byteRingBuffer, TextStreamAsyncOptions textStreamAsyncOptions)
outputStream
- An OutputStream to write into.stringRingBuffer
- A StringRingBuffer
to read lines from. May be null
.
In that case, an implicit StringRingBuffer
is created.charRingBuffer
- A CharRingBuffer
to join lines into. May be null
.
In that case, an implicit CharRingBuffer
is created.byteRingBuffer
- A ByteRingBuffer
to encode chars to. May be null
.
In that case, an implicit ByteRingBuffer
is created.textStreamAsyncOptions
- TextStreamAsyncOptions
that will control all async operations on this instance.public StringRingBuffer getStringRingBuffer()
StringRingBuffer
from where text lines are read.StringRingBuffer
.public boolean isEOF()
protected boolean ready()
ready
in class AsyncAgent
AsyncAgent.action()
.protected boolean done()
done
in class AsyncAgent
protected void action()
action
in class AsyncAgent
public boolean isEmpty()
AsyncAgent.applyAsync()
is used because
that method would return as soon as a set of lines is sent down to the AsyncLineJoiner
,
but there may be chars and bytes still in the pipeline.
If you want to continue execution when all chars and bytes have been flushed to the OutputStream,
you may use this method in conjunction with WhenReady.completeAsync(java.util.function.Predicate<S>, R, S)
.