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>
Direct Known Subclasses:
FormFields

public abstract class ContentSourceCompletableFuture<X> extends CompletableFuture<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();
 
  • Constructor Details

    • ContentSourceCompletableFuture

      public ContentSourceCompletableFuture(Content.Source content)
  • 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 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.

    • parse

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

      Called by parse() to parse a Content.Chunk.

      Parameters:
      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 Retainable.retain() and Retainable.release() as appropriate.
      Returns:
      The parsed X result instance or null if parsing is not yet complete
      Throws:
      Throwable - If there is an error parsing
    • onTransientFailure

      protected boolean onTransientFailure(Throwable cause)

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

      Parameters:
      cause - A transient failure obtained by reading a non-last failure chunk
      Returns:
      true if the transient failure can be ignored, false otherwise