Class HttpBodyBean

java.lang.Object
org.apache.juneau.http.entity.HttpBodyBean
All Implemented Interfaces:
HttpBody

public final class HttpBodyBean extends Object implements HttpBody
A mutable wrapper around an HttpBody that allows overriding the content type.

Use this class when you need to wrap an existing body and attach (or override) its Content-Type.

Create instances via the static factory methods:

// Wrap a byte array body with a specific content type HttpBody body = HttpBodyBean.of(ByteArrayBody.of(bytes), "application/octet-stream");

See Also:
Since:
9.2.1
  • Method Details

    • of

      public static HttpBodyBean of(HttpBody body, String contentType)
      Creates an HttpBodyBean wrapping the given body with the specified content type.
      Parameters:
      body - The underlying body. Must not be null.
      contentType - The content type to report. May be null.
      Returns:
      A new instance. Never null.
    • of

      public static HttpBodyBean of(HttpBody body)
      Creates an HttpBodyBean wrapping the given body and retaining its content type.
      Parameters:
      body - The underlying body. Must not be null.
      Returns:
      A new instance. Never null.
    • getContentType

      Description copied from interface: HttpBody
      Returns the MIME content type of this body, e.g. "application/json; charset=UTF-8".
      Specified by:
      getContentType in interface HttpBody
      Returns:
      The content type, or null if unknown.
    • getContentLength

      public long getContentLength()
      Description copied from interface: HttpBody
      Returns the content length in bytes, or -1 if unknown.

      Implementations that buffer content may return the exact length; streaming implementations should return -1 to cause the transport to use chunked transfer encoding.

      Specified by:
      getContentLength in interface HttpBody
      Returns:
      The content length, or -1 if unknown.
    • isRepeatable

      public boolean isRepeatable()
      Description copied from interface: HttpBody
      Returns true if this body can be written more than once.

      Streaming bodies backed by a one-shot InputStream should return false. Buffered bodies (byte arrays, strings, files) should return true.

      Specified by:
      isRepeatable in interface HttpBody
      Returns:
      true if repeatable.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Description copied from interface: HttpBody
      Writes the body content to the given output stream.

      Implementations must not close out.

      Specified by:
      writeTo in interface HttpBody
      Parameters:
      out - The stream to write to. Never null.
      Throws:
      IOException - If an I/O error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object