Top Description Inners Fields Constructors Methods
org.eclipse.jetty.io

public abstract Class WriteFlusher

extends Object
Class Inheritance
Imports
java.io.IOException, java.net.SocketAddress, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, .WritePendingException, java.util.Arrays, .EnumMap, .EnumSet, .Objects, .Set, java.util.concurrent.atomic.AtomicReference, org.eclipse.jetty.util.BufferUtil, .Callback, org.eclipse.jetty.util.thread.Invocable, .Invocable.InvocationType, org.slf4j.Logger, .LoggerFactory

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 and Inner Type Summary

Modifier and TypeClass 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

public 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 enum
private static class
WriteFlusher.WritingState

In WritingState WriteFlusher is currently writing.

Field Summary

Modifier and TypeField and Description
private 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

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public 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, Throwable... suppressed)

protected ByteBuffer[]

Returns:

The unflushed buffers, or null if all flushed
flush
(SocketAddress
the datagram channel to send the buffers to (used by QUIC and HTTP/3)
address
,
ByteBuffer[]
The buffers to flush
buffers
)

Flushes the buffers iteratively until no progress is made.

public Invocable.InvocationType
pack-priv boolean
pack-priv boolean
public boolean
private boolean
private boolean
public void
public boolean

Returns:

true if the flusher passed the failure to a Callback instance
onFail
(Throwable
The cause of the failure
cause
)

Notify the flusher of a failure

protected abstract void
onIncompleteFlush()

Abstract call to be implemented by specific WriteFlushers.

public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private boolean

Returns:

the previous state or null if the state transition failed
updateState
(WriteFlusher.State
the expected current state
previous
,
WriteFlusher.State
the desired new state
next
)

Tries to update the current state to the given new state.

public void
write(Callback
the callback to call on either failed or complete
callback
,
ByteBuffer...
the buffers to flush to the endpoint
buffers
)

Tries to switch state to WRITING.

public void
write(Callback callback, SocketAddress address, ByteBuffer... buffers)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

__COMPLETINGback to summary
private static final WriteFlusher.State __COMPLETING
__IDLEback to summary
private static final WriteFlusher.State __IDLE
__stateTransitionsback to summary
private static final EnumMap<WriteFlusher.StateType, Set<WriteFlusher.StateType>> __stateTransitions
__WRITINGback to summary
private static final WriteFlusher.State __WRITING
_endPointback to summary
private final EndPoint _endPoint
_stateback to summary
private final AtomicReference<WriteFlusher.State> _state
DEBUGback to summary
private static final boolean DEBUG
EMPTY_BUFFERSback to summary
private static final ByteBuffer[] EMPTY_BUFFERS
LOGback to summary
private static final Logger LOG

Constructor Detail

WriteFlusherback to summary
protected WriteFlusher(EndPoint endPoint)

Method Detail

completeWriteback to summary
public 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. 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()

failback to summary
private void fail(Callback callback, Throwable... suppressed)
flushback to summary
protected ByteBuffer[] flush(SocketAddress address, ByteBuffer[] buffers) throws IOException

Flushes the buffers iteratively until no progress is made.

Parameters
address:SocketAddress

the datagram channel to send the buffers to (used by QUIC and HTTP/3)

buffers:ByteBuffer[]

The buffers to flush

Returns:ByteBuffer[]

The unflushed buffers, or null if all flushed

Exceptions
IOException:
if unable to flush
getCallbackInvocationTypeback to summary
public Invocable.InvocationType getCallbackInvocationType()
isFailedback to summary
pack-priv boolean isFailed()
isIdleback to summary
pack-priv boolean isIdle()
isPendingback to summary
public boolean isPending()
isStateback to summary
private boolean isState(WriteFlusher.StateType type)
isTransitionAllowedback to summary
private boolean isTransitionAllowed(WriteFlusher.State currentState, WriteFlusher.State newState)
onCloseback to summary
public void onClose()
onFailback to summary
public boolean onFail(Throwable cause)

Notify the flusher of a failure

Parameters
cause:Throwable

The cause of the failure

Returns:boolean

true if the flusher passed the failure to a Callback instance

onIncompleteFlushback to summary
protected abstract void onIncompleteFlush()

Abstract call to be implemented by specific WriteFlushers. It should schedule a call to completeWrite() or onFail(Throwable) when appropriate.

toStateStringback to summary
public String toStateString()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
updateStateback to summary
private boolean updateState(WriteFlusher.State previous, WriteFlusher.State next)

Tries to update the current state to the given new state.

Parameters
previous:WriteFlusher.State

the expected current state

next:WriteFlusher.State

the desired new state

Returns:boolean

the previous state or null if the state transition failed

Exceptions
IllegalStateException:
if previous to next is not a legal state transition (api usage error)
writeback to summary
public void write(Callback callback, ByteBuffer... buffers) throws 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 new 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().

Parameters
callback:Callback

the callback to call on either failed or complete

buffers:ByteBuffer[]

the buffers to flush to the endpoint

Exceptions
WritePendingException:
if unable to write due to prior pending write
writeback to summary
public void write(Callback callback, SocketAddress address, ByteBuffer... buffers) throws WritePendingException
org.eclipse.jetty.io back to summary

private Class WriteFlusher.CompletingState

extends State
Class Inheritance

In CompletingState WriteFlusher is flushing buffers that have not been fully written in write(). If write() didn't flush all buffers in one go, it'll switch the State to PendingState. completeWrite() will then switch to this state and try to flush the remaining buffers.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from org.eclipse.jetty.io.WriteFlusher.State:
getTypetoString

Constructor Detail

CompletingStateback to summary
private CompletingState()
org.eclipse.jetty.io back to summary

private Class WriteFlusher.FailedState

extends State
Class Inheritance

In FailedState no more operations are allowed. The current implementation will never recover from this state.

Field Summary

Modifier and TypeField and Description
private final Throwable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Throwable
Inherited from org.eclipse.jetty.io.WriteFlusher.State:
getTypetoString

Field Detail

_causeback to summary
private final Throwable _cause

Constructor Detail

FailedStateback to summary
private FailedState(Throwable cause)

Method Detail

getCauseback to summary
public Throwable getCause()
org.eclipse.jetty.io back to summary

private Class WriteFlusher.IdleState

extends State
Class Inheritance

In IdleState WriteFlusher is idle and accepts new writes

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from org.eclipse.jetty.io.WriteFlusher.State:
getTypetoString

Constructor Detail

IdleStateback to summary
private IdleState()
org.eclipse.jetty.io back to summary

public Interface WriteFlusher.Listener


A listener of WriteFlusher events. If implemented by a Connection class, the onFlushed(long) event will be delivered to it.

Method Summary

Modifier and TypeMethod and Description
public void
onFlushed(long
the number of bytes flushed
bytes
)

Invoked when a WriteFlusher flushed bytes in a non-blocking way, as part of a - possibly larger - write.

Method Detail

onFlushedback to summary
public void onFlushed(long bytes) throws IOException

Invoked when a WriteFlusher flushed bytes in a non-blocking way, as part of a - possibly larger - write.

This method may be invoked multiple times, for example when writing a large buffer: a first flush of bytes, then the connection became TCP congested, and a subsequent flush of bytes when the connection became writable again.

This method is never invoked concurrently, but may be invoked by different threads, so implementations may not rely on thread-local variables.

Implementations may throw an IOException to signal that the write should fail, for example if the implementation enforces a minimum data rate.

Parameters
bytes:long

the number of bytes flushed

Exceptions
IOException:
if the write should fail
org.eclipse.jetty.io back to summary

private Class WriteFlusher.PendingState

extends State
Class Inheritance

In PendingState not all buffers could be written in one go. Then write() will switch to PendingState() and preserve the state by creating a new PendingState object with the given parameters.

Field Summary

Modifier and TypeField and Description
private final SocketAddress
private final ByteBuffer[]
private final Callback

Constructor Summary

AccessConstructor and Description
private
PendingState(Callback callback, SocketAddress address, ByteBuffer[] buffers)

Method Summary

Modifier and TypeMethod and Description
pack-priv Invocable.InvocationType
Inherited from org.eclipse.jetty.io.WriteFlusher.State:
getTypetoString

Field Detail

_addressback to summary
private final SocketAddress _address
_buffersback to summary
private final ByteBuffer[] _buffers
_callbackback to summary
private final Callback _callback

Constructor Detail

PendingStateback to summary
private PendingState(Callback callback, SocketAddress address, ByteBuffer[] buffers)

Method Detail

getCallbackInvocationTypeback to summary
pack-priv Invocable.InvocationType getCallbackInvocationType()
org.eclipse.jetty.io back to summary

private Class WriteFlusher.State

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jetty.io.WriteFlusher.IdleState, org.eclipse.jetty.io.WriteFlusher.WritingState, org.eclipse.jetty.io.WriteFlusher.FailedState, org.eclipse.jetty.io.WriteFlusher.CompletingState, org.eclipse.jetty.io.WriteFlusher.PendingState

State represents a State of WriteFlusher.

Field Summary

Modifier and TypeField and Description
private final WriteFlusher.StateType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public WriteFlusher.StateType
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_typeback to summary
private final WriteFlusher.StateType _type

Constructor Detail

Stateback to summary
private State(WriteFlusher.StateType stateType)

Method Detail

getTypeback to summary
public WriteFlusher.StateType getType()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.eclipse.jetty.io back to summary

private final Enum WriteFlusher.StateType

extends Enum<WriteFlusher.StateType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final WriteFlusher.StateType
public static final WriteFlusher.StateType
public static final WriteFlusher.StateType
public static final WriteFlusher.StateType
public static final WriteFlusher.StateType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static WriteFlusher.StateType
public static WriteFlusher.StateType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

COMPLETINGback to summary
public static final WriteFlusher.StateType COMPLETING
FAILEDback to summary
public static final WriteFlusher.StateType FAILED
IDLEback to summary
public static final WriteFlusher.StateType IDLE
PENDINGback to summary
public static final WriteFlusher.StateType PENDING
WRITINGback to summary
public static final WriteFlusher.StateType WRITING

Constructor Detail

StateTypeback to summary
private StateType()

Method Detail

valueOfback to summary
public static WriteFlusher.StateType valueOf(String name)
valuesback to summary
public static WriteFlusher.StateType[] values()
org.eclipse.jetty.io back to summary

private Class WriteFlusher.WritingState

extends State
Class Inheritance

In WritingState WriteFlusher is currently writing.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from org.eclipse.jetty.io.WriteFlusher.State:
getTypetoString

Constructor Detail

WritingStateback to summary
private WritingState()