Interface Body


public interface Body
The body of an HTTP Response.
See Also:
  • Method Details

    • writeTo

      void writeTo(OutputStream outputStream) throws IOException
      Writes the response body to the provided OutputStream. The number of bytes written must match what is returned by responseLength().

      It is not expected that calling this method will close any underlying resources of the Body, though an implementation may choose to do so.

      This method should not close the provided OutputStream.

      Parameters:
      outputStream - The OutputStream to write the body to.
      Throws:
      IOException - If something goes wrong while writing to the OutputStream.
    • responseLength

      default ResponseLength responseLength()
      The length of the response body that will be written when writeTo(OutputStream) is called.
      Returns:
      The length of the response body.
    • defaultContentType

      default Optional<String> defaultContentType()
      The content type that should be used for this Body.

      It is expected that some types of bodies will have an opinion about what Content-Type header should be sent along with them. The value returned by this method will be used if there is no content type specified by the user.

      Returns:
      The content type to use by default.
    • of

      static Body of(String value)
      Constructs a body which wraps the given String.

      By default UTF-8 will be used to encode the String into the bytes that will be sent by the returned Body.

      Parameters:
      value - The String to send.
      Returns:
      A body which wraps the given String.
    • of

      static Body of(String value, Charset charset)
      Constructs a body which wraps the given String.

      The provided Charset will be used to encode the String into the bytes that will be sent by the returned Body.

      Parameters:
      value - The String to send.
      charset - The Charset to use for encoding the String.
      Returns:
      A body which wraps the given String.
    • of

      static Body of(byte[] value)
      Creates a Body wrapping the given byte[].

      Note that this does not do any defensive copying of the underlying byte array.

      Parameters:
      value - The byte[] to send.
      Returns:
      A body which wraps the given byte[].
    • of

      static Body of(InputStream inputStream)
      Creates a Body wrapping the given byte[].

      Note that the InputStream provided will not be closed when writeTo(OutputStream) is called.

      Parameters:
      inputStream - The InputStream to send.
      Returns:
      A body which wraps the given InputStream.
    • of

      static Body of(InputStream inputStream, ResponseLength responseLength)
      Creates a Body wrapping the given byte[].

      Note that the InputStream provided will not be closed when writeTo(OutputStream) is called.

      Parameters:
      inputStream - inputStream The InputStream to send.
      responseLength - The expected number of bytes that will be written when InputStream.transferTo(OutputStream) is called.
      Returns:
      A body which wraps the given InputStream.
    • empty

      static Body empty()
      Creates an empty Body which will send no bytes to the client.
      Returns:
      An empty body.