Module bus.http

Class ResponseBody

java.lang.Object
org.miaixz.bus.http.bodys.ResponseBody
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
FileInterceptor.DownloadFileProgressResponseBody, RealResponseBody

public abstract class ResponseBody extends Object implements Closeable
The body of an HTTP response.

This class represents the content of a response from the origin server to the client. It is a one-shot stream that can only be read once. The response body relies on limited resources (like network sockets or cached files) and must be closed to release them. It supports reading content as a byte stream, character stream, or a complete byte array/string, making it suitable for handling large responses.

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    Returns the response body as a byte array.
    Returns the response body as a byte stream.
    final Reader
    Returns the response body as a character stream.
    void
    Closes the response body, releasing any associated resources (like network sockets or cached files).
    abstract long
    Returns the number of bytes in the response.
    abstract org.miaixz.bus.core.lang.MediaType
    Returns the media type of this response body.
    create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content)
    Creates a new response body from a byte array.
    create(org.miaixz.bus.core.lang.MediaType contentType, long length, org.miaixz.bus.core.io.source.BufferSource content)
    Creates a new response body from a data source.
    create(org.miaixz.bus.core.lang.MediaType contentType, String content)
    Creates a new response body from a string.
    create(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content)
    Creates a new response body from a ByteString.
    abstract org.miaixz.bus.core.io.source.BufferSource
    Returns the data source for this response body.
    final String
    Returns the response body as a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResponseBody

      public ResponseBody()
  • Method Details

    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType contentType, String content)
      Creates a new response body from a string.

      If contentType is non-null and lacks a charset, UTF-8 will be used.

      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content string.
      Returns:
      A new ResponseBody instance.
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content)
      Creates a new response body from a byte array.
      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content as a byte array.
      Returns:
      A new ResponseBody instance.
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content)
      Creates a new response body from a ByteString.
      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content as a ByteString.
      Returns:
      A new ResponseBody instance.
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType contentType, long length, org.miaixz.bus.core.io.source.BufferSource content)
      Creates a new response body from a data source.
      Parameters:
      contentType - The media type of the content, which may be null.
      length - The content length.
      content - The data source.
      Returns:
      A new ResponseBody instance.
      Throws:
      NullPointerException - if content is null.
    • contentType

      public abstract org.miaixz.bus.core.lang.MediaType contentType()
      Returns the media type of this response body.
      Returns:
      The media type, which may be null.
    • contentLength

      public abstract long contentLength()
      Returns the number of bytes in the response. This will be -1 if the size is unknown.
      Returns:
      The content length.
    • byteStream

      public final InputStream byteStream()
      Returns the response body as a byte stream.
      Returns:
      The input stream.
    • source

      public abstract org.miaixz.bus.core.io.source.BufferSource source()
      Returns the data source for this response body.
      Returns:
      The data source.
    • bytes

      public final byte[] bytes() throws IOException
      Returns the response body as a byte array.

      This method loads the entire response body into memory and is suitable for small responses. For large responses, this may cause an OutOfMemoryError, and stream-based reading is recommended.

      Returns:
      The byte array.
      Throws:
      IOException - if reading fails or the length does not match.
    • charStream

      public final Reader charStream()
      Returns the response body as a character stream.

      This method automatically handles the Byte Order Mark (BOM) or the charset specified in the Content-Type, defaulting to UTF-8. Multiple calls will return the same instance.

      Returns:
      The character stream reader.
    • string

      public final String string() throws IOException
      Returns the response body as a string.

      This method loads the entire response body into memory and is suitable for small responses. It automatically handles the Byte Order Mark (BOM) or the charset specified in the Content-Type, defaulting to UTF-8. For large responses, this may cause an OutOfMemoryError, and stream-based reading is recommended.

      Returns:
      The string.
      Throws:
      IOException - if reading fails.
    • close

      public void close()
      Closes the response body, releasing any associated resources (like network sockets or cached files).
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable