- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
FileInterceptor.DownloadFileProgressResponseBody,RealResponseBody
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 -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]bytes()Returns the response body as a byte array.final InputStreamReturns the response body as a byte stream.final ReaderReturns the response body as a character stream.voidclose()Closes the response body, releasing any associated resources (like network sockets or cached files).abstract longReturns the number of bytes in the response.abstract org.miaixz.bus.core.lang.MediaTypeReturns the media type of this response body.static ResponseBodycreate(org.miaixz.bus.core.lang.MediaType contentType, byte[] content) Creates a new response body from a byte array.static ResponseBodycreate(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.static ResponseBodyCreates a new response body from a string.static ResponseBodycreate(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content) Creates a new response body from aByteString.abstract org.miaixz.bus.core.io.source.BufferSourcesource()Returns the data source for this response body.final Stringstring()Returns the response body as a string.
-
Constructor Details
-
ResponseBody
public ResponseBody()
-
-
Method Details
-
create
Creates a new response body from a string.If
contentTypeis 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
ResponseBodyinstance.
-
create
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
ResponseBodyinstance.
-
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 aByteString.- Parameters:
contentType- The media type of the content, which may be null.content- The content as aByteString.- Returns:
- A new
ResponseBodyinstance.
-
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
ResponseBodyinstance. - 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
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
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
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
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-