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
HTTP 响应体

表示从源服务器到客户端的响应内容,是一次性流,仅能读取一次。 响应体依赖有限资源(如网络套接字或缓存文件),必须通过关闭释放资源。 支持以字节流、字符流或完整字节数组/字符串形式读取内容,适合处理大型响应。

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    获取响应内容的字节数组
    获取字节流
    final Reader
    获取字符流
    void
    关闭响应体
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
    从字节数组创建响应体
    create(org.miaixz.bus.core.lang.MediaType mediaType, long length, org.miaixz.bus.core.io.source.BufferSource content)
    从数据源创建响应体
    create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
    从字符串创建响应体
    create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
    从 ByteString 创建响应体
    abstract long
    获取内容长度
    abstract org.miaixz.bus.core.lang.MediaType
    获取媒体类型
    abstract org.miaixz.bus.core.io.source.BufferSource
    获取数据源
    final 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 mediaType, String content)
      从字符串创建响应体

      如果 mediaType 非空且缺少字符集,则使用 UTF-8。

      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容字符串
      Returns:
      响应体实例
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
      从字节数组创建响应体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容字节数组
      Returns:
      响应体实例
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
      从 ByteString 创建响应体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容 ByteString
      Returns:
      响应体实例
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, long length, org.miaixz.bus.core.io.source.BufferSource content)
      从数据源创建响应体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      length - 内容长度
      content - 数据源
      Returns:
      响应体实例
      Throws:
      NullPointerException - 如果 content 为 null
    • mediaType

      public abstract org.miaixz.bus.core.lang.MediaType mediaType()
      获取媒体类型
      Returns:
      媒体类型(可能为 null)
    • length

      public abstract long length()
      获取内容长度

      返回响应的字节数,未知时返回 -1。

      Returns:
      内容长度
    • byteStream

      public final InputStream byteStream()
      获取字节流
      Returns:
      输入流
    • source

      public abstract org.miaixz.bus.core.io.source.BufferSource source()
      获取数据源
      Returns:
      数据源
    • bytes

      public final byte[] bytes() throws IOException
      获取响应内容的字节数组

      将整个响应体加载到内存中,适合小型响应。 对于大型响应可能引发 OutOfMemoryError,建议使用流式读取。

      Returns:
      字节数组
      Throws:
      IOException - 如果读取失败或长度不匹配
    • charStream

      public final Reader charStream()
      获取字符流

      自动处理字节顺序标记(BOM)或 Content-Type 指定的字符集,默认使用 UTF-8。 多次调用返回同一实例。

      Returns:
      字符流读取器
    • string

      public final String string() throws IOException
      获取响应内容的字符串

      将整个响应体加载到内存中,适合小型响应。 自动处理字节顺序标记(BOM)或 Content-Type 指定的字符集,默认使用 UTF-8。 对于大型响应可能引发 OutOfMemoryError,建议使用流式读取。

      Returns:
      字符串
      Throws:
      IOException - 如果读取失败
    • close

      public void close()
      关闭响应体

      释放关联的资源(如网络套接字或缓存文件)。

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable