Class Body.Builder

java.lang.Object
com.jsgalactic.axiom.http.Body.Builder
Enclosing class:
Body

public static final class Body.Builder extends Object
Collects bytes into a private array and turns it into a Body without copying it again. The builder owns its array until build(); it never exposes it, so the body's immutability cannot be broken through the builder. Not thread-safe, single use: after build() every method throws IllegalStateException.
  • Method Details

    • length

      public int length()
      Returns the number of bytes written so far.
      Returns:
      length in bytes
    • capacity

      public int capacity()
      Returns the number of bytes that can be written before the capacity must grow.
      Returns:
      capacity in bytes
    • capacity

      public Body.Builder capacity(int capacity)
      Grows the capacity to exactly capacity bytes, keeping the content. The old and the new array coexist while the content is moved.
      Parameters:
      capacity - new capacity; at least the current length
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the capacity is below the current length
    • write

      public Body.Builder write(ByteBuffer source)
      Appends the remaining bytes of a buffer, which are consumed, so one read goes straight from the source into the array.
      Parameters:
      source - bytes to append
      Returns:
      this builder
      Throws:
      BufferOverflowException - if the bytes do not fit in the capacity
    • build

      public Body build()
      Finishes the body. When the array is exactly full it becomes the body as is; otherwise it is trimmed once.
      Returns:
      the body
      Throws:
      IllegalArgumentException - if the content type contains control or non-Latin-1 characters
      IllegalStateException - if the builder was already used