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

public abstract Class ContentSourceCompletableFuture<X>

extends CompletableFuture<X>
Class Inheritance
Imports
java.io.EOFException, java.util.concurrent.CompletableFuture, org.eclipse.jetty.io.Content

A utility class to convert content from a Content.Source to an instance available via a CompletableFuture.

An example usage to asynchronously read UTF-8 content is:

public static class CompletableUTF8String extends ContentSourceCompletableFuture<String>;
{
    private final Utf8StringBuilder builder = new Utf8StringBuilder();

    public CompletableUTF8String(Content.Source content)
    {
        super(content);
    }

    @Override
    protected String parse(Content.Chunk chunk) throws Throwable
    {
        // Accumulate the chunk bytes.
        if (chunk.hasRemaining())
            builder.append(chunk.getByteBuffer());

        // Not the last chunk, the result is not ready yet.
        if (!chunk.isLast())
            return null;

        // The result is ready.
        return builder.takeCompleteString(IllegalStateException::new);
    }
}

CompletableUTF8String cs = new CompletableUTF8String(source);
cs.parse();
String s = cs.get();

Field Summary

Modifier and TypeField and Description
private final Content.Source

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if the transient failure can be ignored, false otherwise
onTransientFailure
(Throwable
A transient failure obtained by reading a non-last failure chunk
cause
)

Callback method that informs the parsing about how to handle transient failures.

public void
parse()

Initiates the parsing of the Content.Source.

protected abstract X

Returns:

The parsed X result instance or null if parsing is not yet complete
parse
(Content.Chunk
The chunk containing content to parse. The chunk will never be null nor a failure chunk. If the chunk is stored away to be used later beyond the scope of this call, then implementations must call Content.Chunk#retain() and Content.Chunk#release() as appropriate.
chunk
)

Called by parse() to parse a org.eclipse.jetty.io.Content.Chunk.

Inherited from java.util.concurrent.CompletableFuture:
acceptEitheracceptEitherAsyncacceptEitherAsyncallOfanyOfapplyToEitherapplyToEitherAsyncapplyToEitherAsynccancelcompletecompleteAsynccompleteAsynccompletedFuturecompletedStagecompleteExceptionallycompleteOnTimeoutcopydefaultExecutordelayedExecutordelayedExecutorexceptionallyexceptionallyAsyncexceptionallyAsyncexceptionallyComposeexceptionallyComposeAsyncexceptionallyComposeAsyncexceptionNowfailedFuturefailedStagegetgetgetNowgetNumberOfDependentshandlehandleAsynchandleAsyncisCancelledisCompletedExceptionallyisDonejoinminimalCompletionStagenewIncompleteFutureobtrudeExceptionobtrudeValueorTimeoutresultNowrunAfterBothrunAfterBothAsyncrunAfterBothAsyncrunAfterEitherrunAfterEitherAsyncrunAfterEitherAsyncrunAsyncrunAsyncstatesupplyAsyncsupplyAsyncthenAcceptthenAcceptAsyncthenAcceptAsyncthenAcceptBoththenAcceptBothAsyncthenAcceptBothAsyncthenApplythenApplyAsyncthenApplyAsyncthenCombinethenCombineAsyncthenCombineAsyncthenComposethenComposeAsyncthenComposeAsyncthenRunthenRunAsyncthenRunAsynctoCompletableFuturetoStringwhenCompletewhenCompleteAsyncwhenCompleteAsync

Field Detail

_contentback to summary
private final Content.Source _content

Constructor Detail

ContentSourceCompletableFutureback to summary
public ContentSourceCompletableFuture(Content.Source content)

Method Detail

onTransientFailureback to summary
protected boolean onTransientFailure(Throwable cause)

Callback method that informs the parsing about how to handle transient failures.

Parameters
cause:Throwable

A transient failure obtained by reading a non-last failure chunk

Returns:boolean

true if the transient failure can be ignored, false otherwise

parseback to summary
public void parse()

Initiates the parsing of the Content.Source.

For every valid chunk that is read, parse(Content.Chunk) is called, until a result is produced that is used to complete this CompletableFuture.

Internally, this method is called multiple times to progress the parsing in response to Content.Source#demand(Runnable) calls.

Exceptions thrown during parsing result in this CompletableFuture to be completed exceptionally.

parseback to summary
protected abstract X parse(Content.Chunk chunk) throws Throwable

Called by parse() to parse a org.eclipse.jetty.io.Content.Chunk.

Parameters
chunk:Content.Chunk

The chunk containing content to parse. The chunk will never be null nor a failure chunk. If the chunk is stored away to be used later beyond the scope of this call, then implementations must call Content.Chunk#retain() and Content.Chunk#release() as appropriate.

Returns:X

The parsed X result instance or null if parsing is not yet complete

Exceptions
Throwable:
If there is an error parsing