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

public Class AsyncContent

extends Object
implements Sink, Source, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, org.eclipse.jetty.io.Content.Source, org.eclipse.jetty.io.Content.Sink
Imports
java.io.Closeable, .IOException, .InterruptedIOException, java.nio.ByteBuffer, java.util.ArrayDeque, .List, .Objects, .Queue, org.eclipse.jetty.io.Content, .Retainable, org.eclipse.jetty.io.internal.ByteBufferChunk, org.eclipse.jetty.util.BufferUtil, .Callback, org.eclipse.jetty.util.thread.AutoLock, .SerializedInvoker

A Content.Source that is also a Content.Sink. Content written to the Content.Sink is converted to a Content.Chunk and made available to calls to the read() method. If necessary, any Runnable passed to the demand(Runnable) method is invoked once content is written to the Content.Sink.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final AsyncContent.AsyncChunk
private final Queue<Content.Chunk>
private Runnable
private final SerializedInvoker
private long
private final AutoLock.WithCondition
private Content.Chunk
private boolean
private static final int
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public int
public void
demand(Runnable
the demand callback to invoke where there is a content chunk available
demandCallback
)

Implements org.eclipse.jetty.io.Content.Source.demand.

Demands to invoke the given demand callback parameter when a chunk of content is available.

public void
fail(Throwable
the cause of the failure
failure
)

Implements org.eclipse.jetty.io.Content.Source.fail.

Fails this content source with a last failure chunk, failing and discarding accumulated content chunks that were not yet read.

public void
fail(Throwable
A failure.
failure
,
boolean
true if the failure is persistent, false if the failure is transient.
last
)

Overrides default org.eclipse.jetty.io.Content.Source.fail.

Fails this content source with a failure chunk that may or not may be last.

public void
public long
private void
public boolean
private void
offer(Content.Chunk chunk)

The callback is stored to be failed in case fail() is called or succeeded if and only if the chunk is terminal, as non-terminal chunks have to bind the succeeding of the callback to their release.

public Content.Chunk
read()

Implements org.eclipse.jetty.io.Content.Source.read.

Reads a chunk of content.

private void
runDemandCallback(Runnable demandCallback)

public void
write(boolean
whether the ByteBuffer is the last to write
last
,
ByteBuffer
the ByteBuffer to write
byteBuffer
,
Callback
the callback to notify when the write operation is complete
callback
)

Implements org.eclipse.jetty.io.Content.Sink.write.

Writes the given ByteBuffer, notifying the Callback when the write is complete.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ASYNC_EOFback to summary
private static final AsyncContent.AsyncChunk ASYNC_EOF
chunksback to summary
private final Queue<Content.Chunk> chunks
demandCallbackback to summary
private Runnable demandCallback
invokerback to summary
private final SerializedInvoker invoker
lengthback to summary
private long length
lockback to summary
private final AutoLock.WithCondition lock
persistentFailureback to summary
private Content.Chunk persistentFailure
readClosedback to summary
private boolean readClosed
UNDETERMINED_LENGTHback to summary
private static final int UNDETERMINED_LENGTH
writeClosedback to summary
private boolean writeClosed

Constructor Detail

AsyncContentback to summary
public AsyncContent()

Method Detail

closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
countback to summary
public int count()
demandback to summary
public void demand(Runnable demandCallback)

Implements org.eclipse.jetty.io.Content.Source.demand.

Doc from org.eclipse.jetty.io.Content.Source.demand.

Demands to invoke the given demand callback parameter when a chunk of content is available.

See how to use this method idiomatically.

Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the Runnable callback and a call to this method. Invocations of the passed Runnable are serialized and a callback for demand call is not invoked until any previous demand callback has returned. Thus the Runnable should not block waiting for a callback of a future demand call.

The demand callback may be invoked spuriously: a subsequent call to read() may return null.

Calling this method establishes a pending demand, which is fulfilled when the demand callback is invoked.

Calling this method when there is already a pending demand results in an IllegalStateException to be thrown.

If the invocation of the demand callback throws an exception, then fail(Throwable) is called.

Parameters
demandCallback:Runnable

the demand callback to invoke where there is a content chunk available

Annotations
@Override
failback to summary
public void fail(Throwable failure)

Implements org.eclipse.jetty.io.Content.Source.fail.

Doc from org.eclipse.jetty.io.Content.Source.fail.

Fails this content source with a last failure chunk, failing and discarding accumulated content chunks that were not yet read.

The failure may be notified to the content reader at a later time, when the content reader reads a content chunk, via a Chunk instance with a non null Chunk#getFailure().

If read() has returned a last chunk, this is a no operation.

Typical failure: the content being aborted by user code, or idle timeouts.

If this method has already been called, then it is a no operation.

Parameters
failure:Throwable

the cause of the failure

Annotations
@Override
failback to summary
public void fail(Throwable failure, boolean last)

Overrides default org.eclipse.jetty.io.Content.Source.fail.

Doc from org.eclipse.jetty.io.Content.Source.fail.

Fails this content source with a failure chunk that may or not may be last. If last is true, then the failure is persistent and a call to this method acts as fail(Throwable). Otherwise the failure is transient and a failure chunk will be read in order with content chunks, and subsequent calls to read may produce other content.

A Content.Source or its reader may treat a transient failure as persistent.

Parameters
failure:Throwable

A failure.

last:boolean

true if the failure is persistent, false if the failure is transient.

Annotations
@Override
flushback to summary
public void flush() throws IOException
getLengthback to summary
public long getLength()

Overrides default org.eclipse.jetty.io.Content.Source.getLength.

Returns:long

Doc from org.eclipse.jetty.io.Content.Source.getLength.

the content length, if known, or -1 if the content length is unknown

Annotations
@Override
invokeDemandCallbackback to summary
private void invokeDemandCallback()
isClosedback to summary
public boolean isClosed()
offerback to summary
private void offer(Content.Chunk chunk)

The callback is stored to be failed in case fail() is called or succeeded if and only if the chunk is terminal, as non-terminal chunks have to bind the succeeding of the callback to their release.

readback to summary
public Content.Chunk read()

Implements org.eclipse.jetty.io.Content.Source.read.

Doc from org.eclipse.jetty.io.Content.Source.read.

Reads a chunk of content.

See how to use this method idiomatically.

The returned chunk could be:

  • null, to signal that there isn't a chunk of content available
  • an Chunk instance with non null Chunk#getFailure(), to signal that there was a failure trying to produce a chunk of content, or that the content production has been failed externally
  • a Chunk instance, containing the chunk of content.

Once a read returns an Chunk instance with non-null Chunk#getFailure() then if the failure is last further reads will continue to return the same failure chunk instance, otherwise further read() operations may return different non-failure chunks.

Once a read returns a last chunk, further reads will continue to return a last chunk (although the instance may be different).

The content reader code must ultimately arrange for a call to Chunk#release() on the returned Chunk.

Additionally, prior to the ultimate call to Chunk#release(), the reader code may make additional calls to Chunk#retain(), that must ultimately be matched by a correspondent number of calls to Chunk#release().

Concurrent reads from different threads are not recommended, as they are inherently in a race condition.

Reads performed outside the invocation context of a demand callback are allowed. However, reads performed with a pending demand are inherently in a race condition (the thread that reads with the thread that invokes the demand callback).

Returns:Content.Chunk

a chunk of content, possibly a failure instance, or null

Annotations
@Override
runDemandCallbackback to summary
private void runDemandCallback(Runnable demandCallback)
writeback to summary
public void write(boolean last, ByteBuffer byteBuffer, Callback callback)

Implements org.eclipse.jetty.io.Content.Sink.write.

Doc from org.eclipse.jetty.io.Content.Sink.write.

Writes the given ByteBuffer, notifying the Callback when the write is complete.

Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the Callback and a call to this method.

The write completes:

  • immediately with a failure when this instance is closed or already has a failure
  • successfully when a non empty Content.Chunk returned by read() is released
  • successfully just before the Content.Chunk is returned by read(), for any empty chunk Content.Chunk.
Parameters
last:boolean

whether the ByteBuffer is the last to write

byteBuffer:ByteBuffer

the ByteBuffer to write

callback:Callback

the callback to notify when the write operation is complete

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

private Class AsyncContent.AsyncChunk

extends ByteBufferChunk
implements Callback
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.Callback, org.eclipse.jetty.util.thread.Invocable

Field Summary

Modifier and TypeField and Description
private final Callback
private final Retainable.ReferenceCounter

Constructor Summary

AccessConstructor and Description
public
AsyncChunk(boolean last, ByteBuffer byteBuffer, Callback callback)

Method Summary

Modifier and TypeMethod and Description
public boolean
canRetain()

Overrides default org.eclipse.jetty.io.Retainable.canRetain.

Returns whether this resource is referenced counted by calls to retain() and release().

public void
failed(Throwable
the reason for the operation failure
x
)

Overrides default org.eclipse.jetty.util.Callback.failed.

Callback invoked when the operation fails.

public boolean
release()

Overrides default org.eclipse.jetty.io.Retainable.release.

Releases this resource, potentially decrementing a reference count (if any).

public void
retain()

Overrides default org.eclipse.jetty.io.Retainable.retain.

Retains this resource, potentially incrementing a reference count if there are resources that will be released.

public void
succeeded()

Overrides default org.eclipse.jetty.util.Callback.succeeded.

Callback invoked when the operation completes.

Inherited from org.eclipse.jetty.io.internal.ByteBufferChunk:
getByteBufferisLasttoString

Field Detail

callbackback to summary
private final Callback callback
referenceCounterback to summary
private final Retainable.ReferenceCounter referenceCounter

Constructor Detail

AsyncChunkback to summary
public AsyncChunk(boolean last, ByteBuffer byteBuffer, Callback callback)

Method Detail

canRetainback to summary
public boolean canRetain()

Overrides default org.eclipse.jetty.io.Retainable.canRetain.

Doc from org.eclipse.jetty.io.Retainable.canRetain.

Returns whether this resource is referenced counted by calls to retain() and release().

Implementations may decide that special resources are not not referenced counted (for example, static constants) so calling retain() is a no-operation, and calling release() on those special resources is a no-operation that always returns true.

Returns:boolean

true if calls to retain() are reference counted.

Annotations
@Override
failedback to summary
public void failed(Throwable x)

Overrides default org.eclipse.jetty.util.Callback.failed.

Doc from org.eclipse.jetty.util.Callback.failed.

Callback invoked when the operation fails.

Parameters
x:Throwable

the reason for the operation failure

Annotations
@Override
releaseback to summary
public boolean release()

Overrides default org.eclipse.jetty.io.Retainable.release.

Doc from org.eclipse.jetty.io.Retainable.release.

Releases this resource, potentially decrementing a reference count (if any).

Returns:boolean

true when the reference count goes to zero or if there was no reference count, false otherwise.

Annotations
@Override
retainback to summary
public void retain()

Overrides default org.eclipse.jetty.io.Retainable.retain.

Doc from org.eclipse.jetty.io.Retainable.retain.

Retains this resource, potentially incrementing a reference count if there are resources that will be released.

Annotations
@Override
succeededback to summary
public void succeeded()

Overrides default org.eclipse.jetty.util.Callback.succeeded.

Doc from org.eclipse.jetty.util.Callback.succeeded.

Callback invoked when the operation completes.

Annotations
@Override