Class ContentSourceCompletableFuture<X>

java.lang.Object
java.util.concurrent.CompletableFuture<X>
org.eclipse.jetty.io.content.ContentSourceCompletableFuture<X>
All Implemented Interfaces:
Runnable, CompletionStage<X>, Future<X>, Invocable, Invocable.Task
Direct Known Subclasses:
FormFields

public abstract class ContentSourceCompletableFuture<X> extends CompletableFuture<X> implements Invocable.Task

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();