Class ReaderBody

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

public final class ReaderBody extends Object implements HttpBody
A non-repeatable HttpBody that streams character content from a Reader, encoded as UTF-8.

The character analog of StreamBody. Because the reader is one-shot, isRepeatable() returns false and HttpBody.getContentLength() returns -1 (the transport must use chunked transfer encoding). Content is streamed directly through writeTo(OutputStream) rather than being drained into a String first.

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

Since:
9.2.1
  • Method Details

    • of

      public static ReaderBody of(Reader reader)
      Creates a ReaderBody with text/plain content type.
      Parameters:
      reader - The reader. Must not be null.
      Returns:
      A new instance. Never null.
    • of

      public static ReaderBody of(Reader reader, String contentType)
      Creates a ReaderBody with the given content type.
      Parameters:
      reader - The reader. 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.