Interface Content.Chunk

All Superinterfaces:
Retainable
All Known Implementing Classes:
Content.Chunk.Error
Enclosing class:
Content

public static interface Content.Chunk extends Retainable

A chunk of content indicating whether it is the last chunk.

Optionally, a release function may be specified (for example to release the ByteBuffer back into a pool), or the Retainable.release() method overridden.

  • Field Details

  • Method Details

    • from

      static Content.Chunk from(ByteBuffer byteBuffer, boolean last)

      Creates a Chunk with the given ByteBuffer.

      The returned Chunk must be released.

      Parameters:
      byteBuffer - the ByteBuffer with the bytes of this Chunk
      last - whether the Chunk is the last one
      Returns:
      a new Chunk
    • from

      static Content.Chunk from(ByteBuffer byteBuffer, boolean last, Runnable releaser)

      Creates a Chunk with the given ByteBuffer.

      The returned Chunk must be released.

      Parameters:
      byteBuffer - the ByteBuffer with the bytes of this Chunk
      last - whether the Chunk is the last one
      releaser - the code to run when this Chunk is released
      Returns:
      a new Chunk
    • from

      static Content.Chunk from(ByteBuffer byteBuffer, boolean last, Consumer<ByteBuffer> releaser)

      Creates a last/non-last Chunk with the given ByteBuffer.

      The returned Chunk must be released.

      Parameters:
      byteBuffer - the ByteBuffer with the bytes of this Chunk
      last - whether the Chunk is the last one
      releaser - the code to run when this Chunk is released
      Returns:
      a new Chunk
    • asChunk

      static Content.Chunk asChunk(ByteBuffer byteBuffer, boolean last, Retainable retainable)

      Returns the given ByteBuffer and last arguments as a Chunk, linked to the given Retainable.

      The Retainable.retain() and Retainable.release() methods of this Chunk will delegate to the given Retainable.

      Parameters:
      byteBuffer - the ByteBuffer with the bytes of this Chunk
      last - whether the Chunk is the last one
      retainable - the Retainable this Chunk links to
      Returns:
      a new Chunk
      Throws:
      IllegalArgumentException - if the Retainable cannot be retained
    • from

      static Content.Chunk.Error from(Throwable failure)

      Creates an error chunk with the given failure.

      Parameters:
      failure - the cause of the failure
      Returns:
      a new Error.Chunk
    • next

      static Content.Chunk next(Content.Chunk chunk)

      Returns the chunk that follows the given chunk.

      Next Chunk
      Input Chunk Output Chunk
      null null
      Error Error
      isLast() EOF
      any other Chunk null
    • getByteBuffer

      ByteBuffer getByteBuffer()
      Returns:
      the ByteBuffer of this Chunk
    • isLast

      boolean isLast()
      Returns:
      whether this is the last Chunk
    • remaining

      default int remaining()
      Returns:
      the number of bytes remaining in this Chunk
    • hasRemaining

      default boolean hasRemaining()
      Returns:
      whether this Chunk has remaining bytes
    • get

      default int get(byte[] bytes, int offset, int length)

      Copies the bytes from this Chunk to the given byte array.

      Parameters:
      bytes - the byte array to copy the bytes into
      offset - the offset within the byte array
      length - the maximum number of bytes to copy
      Returns:
      the number of bytes actually copied
    • skip

      default int skip(int length)

      Skips, advancing the ByteBuffer position, the given number of bytes.

      Parameters:
      length - the maximum number of bytes to skip
      Returns:
      the number of bytes actually skipped