Class Body

java.lang.Object
com.jsgalactic.axiom.http.Body

public final class Body extends Object
Immutable request content and its declared content type.

A body owns a private copy of its bytes: factories copy their input, and bytes() returns a fresh copy, so neither the producer nor any reader can change what others see. Bodies are safe to share between threads. Transports enforce size limits before creating a body; a body itself has no limit beyond the array size. toString() reports only the length and media type, never content.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Collects bytes into a private array and turns it into a Body without copying it again.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a read-only view of the content without copying it.
    builder(String contentType, int capacity)
    Starts a body whose array is filled incrementally and then handed to the body without another copy.
    byte[]
    Returns a copy of the content.
    Returns the lowercase value of the content type's charset parameter, unquoted.
    Returns the Content-Type header value as received.
    static Body
    Returns the shared empty body without a content type.
    boolean
    equals(Object other)
     
    int
     
    boolean
    Reports whether there is no content.
    int
    Returns the content length in bytes.
    Returns the lowercase type/subtype of the content type, without parameters.
    static Body
    of(String contentType, byte[] bytes)
    Creates a body from a copy of the supplied bytes.
    static Body
    of(String contentType, ByteBuffer... parts)
    Creates a body by concatenating the remaining bytes of each buffer in order, copying them once.
    Describes the body without revealing its content.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • empty

      public static Body empty()
      Returns the shared empty body without a content type.
      Returns:
      empty body
    • of

      public static Body of(String contentType, byte[] bytes)
      Creates a body from a copy of the supplied bytes.
      Parameters:
      contentType - Content-Type header value, or null when absent
      bytes - content; copied
      Returns:
      body
      Throws:
      IllegalArgumentException - if the content type contains control or non-Latin-1 characters
    • of

      public static Body of(String contentType, ByteBuffer... parts)
      Creates a body by concatenating the remaining bytes of each buffer in order, copying them once. The buffers' positions are not changed.
      Parameters:
      contentType - Content-Type header value, or null when absent
      parts - content buffers; read, not retained
      Returns:
      body
      Throws:
      IllegalArgumentException - if the combined length exceeds the maximum array size or the content type is invalid
    • builder

      public static Body.Builder builder(String contentType, int capacity)
      Starts a body whose array is filled incrementally and then handed to the body without another copy. Transports use it to read network buffers straight into the array that becomes the request body, so a body is copied exactly once on its way in.
      Parameters:
      contentType - Content-Type header value, or null when absent
      capacity - initial capacity in bytes; not negative
      Returns:
      an empty builder
      Throws:
      IllegalArgumentException - for a negative or oversized capacity
    • contentType

      public Optional<String> contentType()
      Returns the Content-Type header value as received.
      Returns:
      raw content type, if declared
    • mediaType

      public Optional<String> mediaType()
      Returns the lowercase type/subtype of the content type, without parameters.
      Returns:
      media type, or empty when absent or malformed
    • charset

      public Optional<String> charset()
      Returns the lowercase value of the content type's charset parameter, unquoted.
      Returns:
      charset name, or empty when absent
    • length

      public int length()
      Returns the content length in bytes.
      Returns:
      length
    • isEmpty

      public boolean isEmpty()
      Reports whether there is no content.
      Returns:
      true for zero bytes
    • bytes

      public byte[] bytes()
      Returns a copy of the content.
      Returns:
      fresh array owned by the caller
    • asReadOnlyBuffer

      public ByteBuffer asReadOnlyBuffer()
      Returns a read-only view of the content without copying it.
      Returns:
      read-only buffer positioned at zero
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Describes the body without revealing its content.
      Overrides:
      toString in class Object
      Returns:
      length and media type