Class StreamBody

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

public final class StreamBody extends Object implements HttpBody
A non-repeatable HttpBody that streams content from an InputStream.

Because the stream is one-shot, isRepeatable() returns false and HttpBody.getContentLength() returns -1 (the transport must use chunked transfer encoding).

The caller is responsible for closing the wrapped stream after the response has been consumed.

Since:
9.2.1
  • Method Details

    • of

      public static StreamBody of(InputStream stream)
      Creates a StreamBody with application/octet-stream content type.
      Parameters:
      stream - The input stream. Must not be null.
      Returns:
      A new instance. Never null.
    • of

      public static StreamBody of(InputStream stream, String contentType)
      Creates a StreamBody with the given content type.
      Parameters:
      stream - The input stream. Must not be null.
      contentType - The MIME content type. May 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.
    • 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.
    • 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.