public abstract class WriteFlusher
extends java.lang.Object
EndPoint.write(Callback, ByteBuffer...)
by calling
EndPoint.flush(ByteBuffer...)
until all content is written.
The abstract method onIncompleteFlush()
is called when not all content has been written after a call to
flush and should organize for the completeWrite()
method to be called when a subsequent call to flush
should be able to make more progress.Modifier and Type | Class and Description |
---|---|
private static class |
WriteFlusher.CompletingState
In CompletingState WriteFlusher is flushing buffers that have not been fully written in write().
|
private static class |
WriteFlusher.FailedState
In FailedState no more operations are allowed.
|
private static class |
WriteFlusher.IdleState
In IdleState WriteFlusher is idle and accepts new writes
|
static interface |
WriteFlusher.Listener
A listener of
WriteFlusher events. |
private class |
WriteFlusher.PendingState
In PendingState not all buffers could be written in one go.
|
private static class |
WriteFlusher.State
State represents a State of WriteFlusher.
|
private static class |
WriteFlusher.StateType |
private static class |
WriteFlusher.WritingState
In WritingState WriteFlusher is currently writing.
|
Modifier and Type | Field and Description |
---|---|
private static WriteFlusher.State |
__COMPLETING |
private static WriteFlusher.State |
__IDLE |
private static java.util.EnumMap<WriteFlusher.StateType,java.util.Set<WriteFlusher.StateType>> |
__stateTransitions |
private static WriteFlusher.State |
__WRITING |
private EndPoint |
_endPoint |
private java.util.concurrent.atomic.AtomicReference<WriteFlusher.State> |
_state |
private static boolean |
DEBUG |
private static java.nio.ByteBuffer[] |
EMPTY_BUFFERS |
private static Logger |
LOG |
Modifier | Constructor and Description |
---|---|
protected |
WriteFlusher(EndPoint endPoint) |
Modifier and Type | Method and Description |
---|---|
void |
completeWrite()
Complete a write that has not completed and that called
onIncompleteFlush() to request a call to this
method when a call to EndPoint.flush(ByteBuffer...) is likely to be able to progress. |
private void |
fail(Callback callback,
java.lang.Throwable... suppressed) |
protected java.nio.ByteBuffer[] |
flush(java.nio.ByteBuffer[] buffers)
Flushes the buffers iteratively until no progress is made.
|
Invocable.InvocationType |
getCallbackInvocationType() |
(package private) boolean |
isFailed() |
(package private) boolean |
isIdle() |
boolean |
isPending() |
private boolean |
isState(WriteFlusher.StateType type) |
private boolean |
isTransitionAllowed(WriteFlusher.State currentState,
WriteFlusher.State newState) |
void |
onClose() |
boolean |
onFail(java.lang.Throwable cause)
Notify the flusher of a failure
|
protected abstract void |
onIncompleteFlush()
Abstract call to be implemented by specific WriteFlushers.
|
java.lang.String |
toStateString() |
java.lang.String |
toString() |
private boolean |
updateState(WriteFlusher.State previous,
WriteFlusher.State next)
Tries to update the current state to the given new state.
|
void |
write(Callback callback,
java.nio.ByteBuffer... buffers)
Tries to switch state to WRITING.
|
private static final Logger LOG
private static final boolean DEBUG
private static final java.nio.ByteBuffer[] EMPTY_BUFFERS
private static final java.util.EnumMap<WriteFlusher.StateType,java.util.Set<WriteFlusher.StateType>> __stateTransitions
private static final WriteFlusher.State __IDLE
private static final WriteFlusher.State __WRITING
private static final WriteFlusher.State __COMPLETING
private final EndPoint _endPoint
private final java.util.concurrent.atomic.AtomicReference<WriteFlusher.State> _state
protected WriteFlusher(EndPoint endPoint)
private boolean updateState(WriteFlusher.State previous, WriteFlusher.State next)
previous
- the expected current statenext
- the desired new statejava.nio.channels.WritePendingException
- if currentState is WRITING and new state is WRITING (api usage error)private boolean isTransitionAllowed(WriteFlusher.State currentState, WriteFlusher.State newState)
public Invocable.InvocationType getCallbackInvocationType()
protected abstract void onIncompleteFlush()
completeWrite()
or onFail(Throwable)
when appropriate.public void write(Callback callback, java.nio.ByteBuffer... buffers) throws java.nio.channels.WritePendingException
PendingState
object to preserve the state
and then calls onIncompleteFlush()
. The remaining buffers will be written in completeWrite()
.
If all buffers have been written it calls callback.complete().callback
- the callback to call on either failed or completebuffers
- the buffers to flush to the endpointjava.nio.channels.WritePendingException
- if unable to write due to prior pending writeprivate void fail(Callback callback, java.lang.Throwable... suppressed)
public void completeWrite()
onIncompleteFlush()
to request a call to this
method when a call to EndPoint.flush(ByteBuffer...)
is likely to be able to progress.
It tries to switch from PENDING to COMPLETING. If state transition fails, then it does nothing as the callback
should have been already failed. That's because the only way to switch from PENDING outside this method is
onFail(Throwable)
or onClose()
protected java.nio.ByteBuffer[] flush(java.nio.ByteBuffer[] buffers) throws java.io.IOException
buffers
- The buffers to flushjava.io.IOException
- if unable to flushpublic boolean onFail(java.lang.Throwable cause)
cause
- The cause of the failureCallback
instancepublic void onClose()
boolean isFailed()
boolean isIdle()
public boolean isPending()
private boolean isState(WriteFlusher.StateType type)
public java.lang.String toStateString()
public java.lang.String toString()
toString
in class java.lang.Object