Module org.eclipse.jetty.io
Package org.eclipse.jetty.io.content
Class ContentSourceCompletableFuture<X>
java.lang.Object
java.util.concurrent.CompletableFuture<X>
org.eclipse.jetty.io.content.ContentSourceCompletableFuture<X>
- All Implemented Interfaces:
CompletionStage<X>,Future<X>
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();
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture
CompletableFuture.AsynchronousCompletionTaskNested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanonTransientFailure(Throwable cause) Callback method that informs the parsing about how to handle transient failures.voidparse()Initiates the parsing of theContent.Source.protected abstract Xparse(Content.Chunk chunk) Called byparse()to parse aContent.Chunk.Methods inherited from class java.util.concurrent.CompletableFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, cancel, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, exceptionallyAsync, exceptionallyAsync, exceptionallyCompose, exceptionallyComposeAsync, exceptionallyComposeAsync, exceptionNow, failedFuture, failedStage, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, resultNow, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, state, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
-
Constructor Details
-
ContentSourceCompletableFuture
-
-
Method Details
-
parse
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 thisCompletableFuture.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
CompletableFutureto be completed exceptionally. -
parse
Called by
parse()to parse aContent.Chunk.- Parameters:
chunk- The chunk containing content to parse. The chunk will never benullnor afailure chunk. If the chunk is stored away to be used later beyond the scope of this call, then implementations must callRetainable.retain()andRetainable.release()as appropriate.- Returns:
- The parsed
Xresult instance ornullif parsing is not yet complete - Throws:
Throwable- If there is an error parsing
-
onTransientFailure
Callback method that informs the parsing about how to handle transient failures.
- Parameters:
cause- A transient failure obtained by reading anon-lastfailure chunk- Returns:
trueif the transient failure can be ignored,falseotherwise
-