Package org.eclipse.jetty.io
Class WriteFlusher
java.lang.Object
org.eclipse.jetty.io.WriteFlusher
A Utility class to help implement
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
In CompletingState WriteFlusher is flushing buffers that have not been fully written in write().private static class
In FailedState no more operations are allowed.private static class
In IdleState WriteFlusher is idle and accepts new writesstatic interface
A listener ofWriteFlusher
events.private class
In PendingState not all buffers could be written in one go.private static class
State represents a State of WriteFlusher.private static enum
private static class
In WritingState WriteFlusher is currently writing. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final WriteFlusher.State
private static final WriteFlusher.State
private static final EnumMap<WriteFlusher.StateType,
Set<WriteFlusher.StateType>> private static final WriteFlusher.State
private final EndPoint
private final AtomicReference<WriteFlusher.State>
private static final boolean
private static final ByteBuffer[]
private static final Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Complete a write that has not completed and that calledonIncompleteFlush()
to request a call to this method when a call toEndPoint.flush(ByteBuffer...)
is likely to be able to progress.private void
protected ByteBuffer[]
flush
(ByteBuffer[] buffers) Flushes the buffers iteratively until no progress is made.(package private) boolean
isFailed()
(package private) boolean
isIdle()
boolean
private boolean
private boolean
isTransitionAllowed
(WriteFlusher.State currentState, WriteFlusher.State newState) void
onClose()
boolean
Notify the flusher of a failureprotected abstract void
Abstract call to be implemented by specific WriteFlushers.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, ByteBuffer... buffers) Tries to switch state to WRITING.
-
Field Details
-
LOG
-
DEBUG
private static final boolean DEBUG -
EMPTY_BUFFERS
-
__stateTransitions
-
__IDLE
-
__WRITING
-
__COMPLETING
-
_endPoint
-
_state
-
-
Constructor Details
-
WriteFlusher
-
-
Method Details
-
updateState
Tries to update the current state to the given new state.- Parameters:
previous
- the expected current statenext
- the desired new state- Returns:
- the previous state or null if the state transition failed
- Throws:
WritePendingException
- if currentState is WRITING and new state is WRITING (api usage error)
-
isTransitionAllowed
-
getCallbackInvocationType
-
onIncompleteFlush
protected abstract void onIncompleteFlush()Abstract call to be implemented by specific WriteFlushers. It should schedule a call tocompleteWrite()
oronFail(Throwable)
when appropriate. -
write
Tries to switch state to WRITING. If successful it writes the given buffers to the EndPoint. If state transition fails it will fail the callback and leave the WriteFlusher in a terminal FAILED state. If not all buffers can be written in one go it creates a newPendingState
object to preserve the state and then callsonIncompleteFlush()
. The remaining buffers will be written incompleteWrite()
. If all buffers have been written it calls callback.complete().- Parameters:
callback
- the callback to call on either failed or completebuffers
- the buffers to flush to the endpoint- Throws:
WritePendingException
- if unable to write due to prior pending write
-
fail
-
completeWrite
public void completeWrite()Complete a write that has not completed and that calledonIncompleteFlush()
to request a call to this method when a call toEndPoint.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 isonFail(Throwable)
oronClose()
-
flush
Flushes the buffers iteratively until no progress is made.- Parameters:
buffers
- The buffers to flush- Returns:
- The unflushed buffers, or null if all flushed
- Throws:
IOException
- if unable to flush
-
onFail
Notify the flusher of a failure- Parameters:
cause
- The cause of the failure- Returns:
- true if the flusher passed the failure to a
Callback
instance
-
onClose
public void onClose() -
isFailed
boolean isFailed() -
isIdle
boolean isIdle() -
isPending
public boolean isPending() -
isState
-
toStateString
-
toString
-