Interface Content.Source

All Known Implementing Classes:
AsyncContent, ByteBufferContentSource, ChunksContentSource, ContentSourceTransformer, InputStreamContentSource, OutputStreamContentSource, PathContentSource
Enclosing class:
Content

public static interface Content.Source

A source of content that can be read with a read/demand model.

To avoid leaking its resources, a source must either:

  • be read until it returns a last chunk, either EOF or a terminal failure
  • be failed

Idiomatic usage

The read/demand model typical usage is the following:


 public void onContentAvailable() {
     while (true) {
         // Read a chunk
         Chunk chunk = source.read();

         // There is no chunk, demand to be called back and exit.
         if (chunk == null) {
             source.demand(this::onContentAvailable);
             return;
         }

         // The chunk is a failure.
         if (Content.Chunk.isFailure(chunk))
         {
             boolean fatal = chunk.isLast();
             if (fatal)
             {
                 handleFatalFailure(chunk.getFailure());
                 return;
             }
             else
             {
                 handleTransientFailure(chunk.getFailure());
                 continue;
             }
         }

         // It's a valid chunk, consume the chunk's bytes.
         ByteBuffer buffer = chunk.getByteBuffer();
         // ...

         // Release the chunk when it has been consumed.
         chunk.release();

         // Exit if the Content.Source is fully consumed.
         if (chunk.isLast())
             break;
     }
 }