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
从源服务器到客户机应用程序的一次性流,包含响应主体的原始字节。 到web服务器的活动连接支持每个响应主体。 这对客户机应用程序施加了义务和限制,每个响应主体由一个有限的资源(如socket(实时网络响应)或一个打开的 文件(用于缓存的响应)来支持。如果不关闭响应体,将会泄漏资源并减慢或崩溃 这个类和Response都实现了Closeable。关闭一个响应就是关闭它的响应体。如果您 调用NewCall.execute()或实现Callback.onResponse(org.miaixz.bus.http.NewCall, org.miaixz.bus.http.Response),则必须通过 调用以下任何方法来关闭此主体:
  • Response.close()
  • Response.body().close()
  • Response.body().source().close()
  • Response.body().charStream().close()
  • Response.body().byteStream().close()
  • Response.body().bytes()
  • Response.body().string()
这个类可以用来传输非常大的响应。例如,可以使用这个类来读取大于分配给当前进程的整个内存的响应。 它甚至可以传输大于当前设备总存储的响应,这是视频流应用程序的一个常见需求 因为这个类不会在内存中缓冲完整的响应,所以应用程序可能不会重新读取响应的字节。使用bytes()string()将整个响应读入内存。或者使用source()byteStream()charStream()来处理响应
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    Returns the response as a byte array.
     
    final Reader
    Returns the response as a character stream.
    void
     
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
    新的响应体,它传输content
    create(org.miaixz.bus.core.lang.MediaType mediaType, long length, org.miaixz.bus.core.io.source.BufferSource content)
    新的响应体,它传输content
    create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
    返回一个传输content的新响应体。如果mediaType是非空且缺少字符集,则使用UTF-8
    create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
    新的响应体,它传输content
    abstract long
    Returns the number of bytes in that will returned by bytes(), or byteStream(), or -1 if unknown.
    abstract org.miaixz.bus.core.lang.MediaType
     
    abstract org.miaixz.bus.core.io.source.BufferSource
     
    final String
    Returns the response 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 mediaType, String content)
      返回一个传输content的新响应体。如果mediaType是非空且缺少字符集,则使用UTF-8
      Parameters:
      mediaType - 媒体类型
      content - 内容
      Returns:
      新响应体
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
      新的响应体,它传输content
      Parameters:
      mediaType - 媒体类型
      content - 内容
      Returns:
      新响应体
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
      新的响应体,它传输content
      Parameters:
      mediaType - 媒体类型
      content - 内容
      Returns:
      新响应体
    • create

      public static ResponseBody create(org.miaixz.bus.core.lang.MediaType mediaType, long length, org.miaixz.bus.core.io.source.BufferSource content)
      新的响应体,它传输content
      Parameters:
      mediaType - 媒体类型
      length - 内容大小
      content - 内容
      Returns:
      新响应体
    • mediaType

      public abstract org.miaixz.bus.core.lang.MediaType mediaType()
    • length

      public abstract long length()
      Returns the number of bytes in that will returned by bytes(), or byteStream(), or -1 if unknown.
    • byteStream

      public final InputStream byteStream()
    • source

      public abstract org.miaixz.bus.core.io.source.BufferSource source()
    • bytes

      public final byte[] bytes() throws IOException
      Returns the response as a byte array. This method loads entire response body into memory. If the response body is very large this may trigger an OutOfMemoryError. Prefer to stream the response body if this is a possibility for your response.
      Throws:
      IOException
    • charStream

      public final Reader charStream()
      Returns the response as a character stream. If the response starts with a ByteOrder Mark (BOM), it is consumed and used to determine the charset of the response bytes. Otherwise if the response has a Content-Type header that specifies a charset, that is used to determine the charset of the response bytes. Otherwise the response bytes are decoded as UTF-8.
    • string

      public final String string() throws IOException
      Returns the response as a string. If the response starts with a ByteOrder Mark (BOM), it is consumed and used to determine the charset of the response bytes. Otherwise if the response has a Content-Type header that specifies a charset, that is used to determine the charset of the response bytes. Otherwise the response bytes are decoded as UTF-8. This method loads entire response body into memory. If the response body is very large this may trigger an OutOfMemoryError. Prefer to stream the response body if this is a possibility for your response.
      Throws:
      IOException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable