Package org.miaixz.bus.http.bodys
Class ResponseBody
java.lang.Object
org.miaixz.bus.http.bodys.ResponseBody
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
FileInterceptor.DownloadFileProgressResponseBody,RealResponseBody
从源服务器到客户机应用程序的一次性流,包含响应主体的原始字节。 到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 -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]bytes()Returns the response as a byte array.final InputStreamfinal ReaderReturns the response as a character stream.voidclose()static ResponseBodycreate(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content) 新的响应体,它传输contentstatic ResponseBodycreate(org.miaixz.bus.core.lang.MediaType mediaType, long length, org.miaixz.bus.core.io.source.BufferSource content) 新的响应体,它传输contentstatic ResponseBody返回一个传输content的新响应体。static ResponseBodycreate(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content) 新的响应体,它传输contentabstract longlength()Returns the number of bytes in that will returned bybytes(), orbyteStream(), or -1 if unknown.abstract org.miaixz.bus.core.lang.MediaTypeabstract org.miaixz.bus.core.io.source.BufferSourcesource()final Stringstring()Returns the response as a string.
-
Constructor Details
-
ResponseBody
public ResponseBody()
-
-
Method Details
-
create
返回一个传输content的新响应体。如果mediaType是非空且缺少字符集,则使用UTF-8- Parameters:
mediaType- 媒体类型content- 内容- Returns:
- 新响应体
-
create
新的响应体,它传输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 bybytes(), orbyteStream(), or -1 if unknown. -
byteStream
-
source
public abstract org.miaixz.bus.core.io.source.BufferSource source() -
bytes
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 anOutOfMemoryError. Prefer to stream the response body if this is a possibility for your response.- Throws:
IOException
-
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
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 anOutOfMemoryError. Prefer to stream the response body if this is a possibility for your response.- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-