Package org.eclipse.jetty.io
Class WriteFlusher
- java.lang.Object
-
- org.eclipse.jetty.io.WriteFlusher
-
public abstract class WriteFlusher extends java.lang.ObjectA Utility class to help implementEndPoint.write(Callback, ByteBuffer...)by callingEndPoint.flush(ByteBuffer...)until all content is written. The abstract methodonIncompleteFlush()is called when not all content has been written after a call to flush and should organize for thecompleteWrite()method to be called when a subsequent call to flush should be able to make more progress.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classWriteFlusher.CompletingStateIn CompletingState WriteFlusher is flushing buffers that have not been fully written in write().private static classWriteFlusher.FailedStateIn FailedState no more operations are allowed.private static classWriteFlusher.IdleStateIn IdleState WriteFlusher is idle and accepts new writesstatic interfaceWriteFlusher.ListenerA listener ofWriteFlusherevents.private classWriteFlusher.PendingStateIn PendingState not all buffers could be written in one go.private static classWriteFlusher.StateState represents a State of WriteFlusher.private static classWriteFlusher.StateTypeprivate static classWriteFlusher.WritingStateIn WritingState WriteFlusher is currently writing.
-
Field Summary
Fields Modifier and Type Field Description private static WriteFlusher.State__COMPLETINGprivate static WriteFlusher.State__IDLEprivate static java.util.EnumMap<WriteFlusher.StateType,java.util.Set<WriteFlusher.StateType>>__stateTransitionsprivate static WriteFlusher.State__WRITINGprivate EndPoint_endPointprivate java.util.concurrent.atomic.AtomicReference<WriteFlusher.State>_stateprivate static booleanDEBUGprivate static java.nio.ByteBuffer[]EMPTY_BUFFERSprivate static LoggerLOG
-
Constructor Summary
Constructors Modifier Constructor Description protectedWriteFlusher(EndPoint endPoint)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcompleteWrite()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 voidfail(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.InvocationTypegetCallbackInvocationType()(package private) booleanisFailed()(package private) booleanisIdle()booleanisPending()private booleanisState(WriteFlusher.StateType type)private booleanisTransitionAllowed(WriteFlusher.State currentState, WriteFlusher.State newState)voidonClose()booleanonFail(java.lang.Throwable cause)Notify the flusher of a failureprotected abstract voidonIncompleteFlush()Abstract call to be implemented by specific WriteFlushers.java.lang.StringtoStateString()java.lang.StringtoString()private booleanupdateState(WriteFlusher.State previous, WriteFlusher.State next)Tries to update the current state to the given new state.voidwrite(Callback callback, java.nio.ByteBuffer... buffers)Tries to switch state to WRITING.
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
DEBUG
private static final boolean DEBUG
-
EMPTY_BUFFERS
private static final java.nio.ByteBuffer[] EMPTY_BUFFERS
-
__stateTransitions
private static final java.util.EnumMap<WriteFlusher.StateType,java.util.Set<WriteFlusher.StateType>> __stateTransitions
-
__IDLE
private static final WriteFlusher.State __IDLE
-
__WRITING
private static final WriteFlusher.State __WRITING
-
__COMPLETING
private static final WriteFlusher.State __COMPLETING
-
_endPoint
private final EndPoint _endPoint
-
_state
private final java.util.concurrent.atomic.AtomicReference<WriteFlusher.State> _state
-
-
Constructor Detail
-
WriteFlusher
protected WriteFlusher(EndPoint endPoint)
-
-
Method Detail
-
updateState
private boolean updateState(WriteFlusher.State previous, WriteFlusher.State next)
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:
java.nio.channels.WritePendingException- if currentState is WRITING and new state is WRITING (api usage error)
-
isTransitionAllowed
private boolean isTransitionAllowed(WriteFlusher.State currentState, WriteFlusher.State newState)
-
getCallbackInvocationType
public Invocable.InvocationType 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
public void write(Callback callback, java.nio.ByteBuffer... buffers) throws java.nio.channels.WritePendingException
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 newPendingStateobject 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:
java.nio.channels.WritePendingException- if unable to write due to prior pending write
-
fail
private void fail(Callback callback, java.lang.Throwable... suppressed)
-
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
protected java.nio.ByteBuffer[] flush(java.nio.ByteBuffer[] buffers) throws java.io.IOExceptionFlushes the buffers iteratively until no progress is made.- Parameters:
buffers- The buffers to flush- Returns:
- The unflushed buffers, or null if all flushed
- Throws:
java.io.IOException- if unable to flush
-
onFail
public boolean onFail(java.lang.Throwable cause)
Notify the flusher of a failure- Parameters:
cause- The cause of the failure- Returns:
- true if the flusher passed the failure to a
Callbackinstance
-
onClose
public void onClose()
-
isFailed
boolean isFailed()
-
isIdle
boolean isIdle()
-
isPending
public boolean isPending()
-
isState
private boolean isState(WriteFlusher.StateType type)
-
toStateString
public java.lang.String toStateString()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-