Interface Body

  • All Superinterfaces:
    ToHttpHeaders

    public interface Body
    extends ToHttpHeaders

    The Body interface represents a sequence of bytes (usually for an entity or page body) including a length and an HTTP content type.

    The central method is newInputStream() which must return a new InputStream over the underlying byte sequence. Each call must create and return a new InputStream. Simply creating and returning the same InputStream each time is not sufficient! Think of it like the Iterable.iterator() method in a collection: each iterator() call creates a new iterator with its own internal iteration state, but they all provide a view of the same underlying collection. Similarly, each call to newInputStream() creates a new InputStream providing a view of the underlying bytes. This implies that this interface is not suitable for streaming data from e.g. a network connection on demand. The underlying bytes must be available so they can be iterated over multiple times.

    The motivation for this interface is to provide a way to handle sequences of bytes while imposing as few restrictions on their internal representation as possible. By only providing sequential access, the underlying bytes can be stored as a byte array, a String, or in compressed form. They can even be concatenated from multiple internal representations on the fly. This flexibility makes it possible to handle large byte sequences while minimizing the number of full copies that have to be made and kept in memory.

    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      long contentLength()
      Return the length of the underlying byte sequence in bytes.
      @NonNull ContentType contentType()
      Return the content type of the underlying data.
      static @NonNull Body fromBytes​(byte @NonNull [] bytes)
      Return a Body containing the bytes of the given byte array with the default content-type application/octet-stream
      static @NonNull Body fromBytes​(byte @NonNull [] bytes, @NonNull ContentType contentType)
      Return a Body containing the bytes of the given byte array.
      static @NonNull Body fromBytesUnsafe​(byte @NonNull [] bytes)
      Return a Body containing the bytes of the given byte array with the default content-type application/octet-stream
      static @NonNull Body fromBytesUnsafe​(byte @NonNull [] bytes, @NonNull ContentType contentType)
      Return a Body containing the bytes of the given byte array.
      static @NonNull Body fromUtf8​(@NonNull java.lang.String utf8)
      Return a Body over the bytes of the given String encoded as UTF-8 with the default content type text/plain; charset=utf-8.
      static @NonNull Body fromUtf8​(@NonNull java.lang.String utf8, @NonNull ContentType contentType)
      Return a Body over the bytes of the given String encoded as UTF-8.
      @NonNull java.io.InputStream newInputStream()
      Create a new InputStream pointing to the start of the underlying byte sequence.
      default byte @NonNull [] toBytes()
      Return this byte sequence as a byte array.
      default @NonNull HttpHeaders toHttpHeaders()
      Return the Content-Length and Content-Type headers for this Body.
      default @NonNull java.lang.String toUtf8()
      Decode this byte sequence as UTF-8.
    • Method Detail

      • newInputStream

        @NonNull
        @NonNull java.io.InputStream newInputStream()

        Create a new InputStream pointing to the start of the underlying byte sequence.

        InputStreams created by this method must always read the same bytes as every other InputStream created by this method, but their internal read states must be completely independent.

        Returns:
        a new InputStream over the underlying bytes
      • contentLength

        long contentLength()
        Return the length of the underlying byte sequence in bytes. Must not be negative.
        Returns:
        the length of the underlying byte sequence
      • contentType

        @NonNull
        @NonNull ContentType contentType()
        Return the content type of the underlying data. This should be a MIME type suitable for use in a Content-Type header.
        Returns:
        the content type of the data
      • toHttpHeaders

        @NonNull
        default @NonNull HttpHeaders toHttpHeaders()

        Return the Content-Length and Content-Type headers for this Body.

        Overriding this method is not recommended.

        Specified by:
        toHttpHeaders in interface ToHttpHeaders
        Returns:
        HttpHeaders containing the length and type headers for this Body
      • toUtf8

        @NonNull
        default @NonNull java.lang.String toUtf8()
                                          throws java.io.IOException

        Decode this byte sequence as UTF-8.

        The default implementation decodes the bytes and returns the result. You can choose to override it with a more efficient implementation if your presentation allows.

        Returns:
        the content of this Body as UTF-8
        Throws:
        java.io.IOException - when the InputStream returned by {newInputStream()} throws IOException
        java.nio.charset.CharacterCodingException - when the underlying bytes are not valid UTF-8
      • toBytes

        default byte @NonNull [] toBytes()
                                  throws java.io.IOException

        Return this byte sequence as a byte array.

        The default implementation fills the array incrementally. You can choose to override it with a more efficient implementation, but take care not to leak mutable access to your internal data by returning a reference to a byte array that's stored in your class.

        Returns:
        the byte contents of this Body
        Throws:
        java.io.IOException - when the InputStream returned by {newInputStream()} throws an IOException
      • fromUtf8

        @NonNull
        static @NonNull Body fromUtf8​(@NonNull
                                      @NonNull java.lang.String utf8,
                                      @NonNull
                                      @NonNull ContentType contentType)
        Return a Body over the bytes of the given String encoded as UTF-8. This method retains the String and does not allocate an additional backing buffer.
        Parameters:
        utf8 - the String
        contentType - the content type for the created Body
        Returns:
        a Body of the bytes of the given String encoded as UTF-8
      • fromUtf8

        @NonNull
        static @NonNull Body fromUtf8​(@NonNull
                                      @NonNull java.lang.String utf8)
        Return a Body over the bytes of the given String encoded as UTF-8 with the default content type text/plain; charset=utf-8.
        Parameters:
        utf8 - the String
        Returns:
        a Body of the bytes of the given String encoded as UTF-8
        See Also:
        fromUtf8(String, ContentType)
      • fromBytesUnsafe

        @NonNull
        static @NonNull Body fromBytesUnsafe​(byte @NonNull [] bytes,
                                             @NonNull
                                             @NonNull ContentType contentType)

        Return a Body containing the bytes of the given byte array.

        SAFETY: this method does not copy the passed byte array. It is up to the caller to ensure that the array is NEVER MODIFIED after being passed to this method. Any modifications to the array will be reflected in the returned Body object, thereby breaking immutability guarantees. If you're not sure that the array will never be modified again, you can use fromBytes(byte[], ContentType) instead with copies the array.

        Parameters:
        bytes - the byte array
        contentType - the content type for the created Body
        Returns:
        a Body of the array's bytes
      • fromBytesUnsafe

        @NonNull
        static @NonNull Body fromBytesUnsafe​(byte @NonNull [] bytes)

        Return a Body containing the bytes of the given byte array with the default content-type application/octet-stream

        SAFETY: this method does not copy the passed byte array. It is up to the caller to ensure that the array is NEVER MODIFIED after being passed to this method. Any modifications to the array will be reflected in the returned Body object, thereby breaking immutability guarantees. If you're not sure that the array will never be modified again, you can use fromBytes(byte[]) instead with copies the array.

        Parameters:
        bytes - the byte array
        Returns:
        a Body of the array's bytes
      • fromBytes

        @NonNull
        static @NonNull Body fromBytes​(byte @NonNull [] bytes,
                                       @NonNull
                                       @NonNull ContentType contentType)

        Return a Body containing the bytes of the given byte array.

        The array is copied so that modifications don't impact the returned Body. If you can guarantee that the array won't be modified, you can use fromBytesUnsafe(byte[], ContentType) which avoids the copy.

        Parameters:
        bytes - the byte array
        contentType - the content type for the created Body
        Returns:
        a Body of the array's bytes
      • fromBytes

        @NonNull
        static @NonNull Body fromBytes​(byte @NonNull [] bytes)

        Return a Body containing the bytes of the given byte array with the default content-type application/octet-stream

        The array is copied so that modifications don't impact the returned Body. If you can guarantee that the array won't be modified, you can use fromBytesUnsafe(byte[]) which avoids the copy.

        Parameters:
        bytes - the byte array
        Returns:
        a Body of the array's bytes