Class AsyncContent
- All Implemented Interfaces:
- Closeable, AutoCloseable, Content.Sink, Content.Source
- Direct Known Subclasses:
- AsyncRequestContent
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 Class SummaryNested classes/interfaces inherited from interface Content.SourceContent.Source.Factory
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidclose()intcount()voidDemands to invoke the given demand callback parameter when a chunk of content is available.voidFails this content source with alastfailure chunk, failing and discarding accumulated content chunks that were not yet read.voidFails this content source with afailure chunkthat may or not may belast.voidflush()longbooleanisClosed()read()Reads a chunk of content.voidwrite(boolean last, ByteBuffer byteBuffer, Callback callback) Writes the givenByteBuffer, notifying theCallbackwhen the write is complete.Methods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Content.Sourcerewind
- 
Constructor Details- 
AsyncContentpublic AsyncContent()
 
- 
- 
Method Details- 
writeWrites the given ByteBuffer, notifying theCallbackwhen 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 Callbackand 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.Chunkreturned byread()is released
- successfully just before the Content.Chunkis returned byread(), for any empty chunkContent.Chunk.
 - Specified by:
- writein interface- Content.Sink
- Parameters:
- last- whether the ByteBuffer is the last to write
- byteBuffer- the ByteBuffer to write
- callback- the callback to notify when the write operation is complete
 
- 
flush- Throws:
- IOException
 
- 
closepublic void close()- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
 
- 
isClosedpublic boolean isClosed()
- 
getLengthpublic long getLength()- Specified by:
- getLengthin interface- Content.Source
- Returns:
- the content length, if known, or -1 if the content length is unknown
 
- 
readDescription copied from interface:Content.SourceReads 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 Content.Chunkinstance with non nullContent.Chunk.getFailure(), to signal that there was a failure trying to produce a chunk of content, or that the content production has beenfailedexternally
- a Content.Chunkinstance, containing the chunk of content.
 Once a read returns an Content.Chunkinstance with non-nullContent.Chunk.getFailure()then if the failure islastfurther reads will continue to return the same failure chunk instance, otherwise furtherread()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 Retainable.release()on the returnedContent.Chunk.Additionally, prior to the ultimate call to Retainable.release(), the reader code may make additional calls toRetainable.retain(), that must ultimately be matched by a correspondent number of calls toRetainable.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 callbackare 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).- Specified by:
- readin interface- Content.Source
- Returns:
- a chunk of content, possibly a failure instance, or null
- See Also:
 
- 
demandDescription copied from interface:Content.SourceDemands 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 Runnablecallback and a call to this method. Invocations of the passedRunnableare serialized and a callback fordemandcall is not invoked until any previousdemandcallback has returned. Thus theRunnableshould not block waiting for a callback of a future demand call.The demand callback may be invoked spuriously: a subsequent call to Content.Source.read()may returnnull.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 IllegalStateExceptionto be thrown.If the invocation of the demand callback throws an exception, then Content.Source.fail(Throwable)is called.- Specified by:
- demandin interface- Content.Source
- Parameters:
- demandCallback- the demand callback to invoke where there is a content chunk available
- See Also:
 
- 
failDescription copied from interface:Content.SourceFails this content source with a lastfailure 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 Content.Chunkinstance with a non nullContent.Chunk.getFailure().If Content.Source.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. - Specified by:
- failin interface- Content.Source
- Parameters:
- failure- the cause of the failure
- See Also:
 
- 
failDescription copied from interface:Content.SourceFails this content source with a failure chunkthat may or not may belast. Iflastistrue, then the failure is persistent and a call to this method acts asContent.Source.fail(Throwable). Otherwise the failure is transient and afailure chunkwill bereadin order with content chunks, and subsequent calls toreadmay produce other content.A Content.Sourceor itsreadermay treat a transient failure as persistent.- Specified by:
- failin interface- Content.Source
- Parameters:
- failure- A failure.
- last- true if the failure is persistent, false if the failure is transient.
- See Also:
 
- 
countpublic int count()
 
-