-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public abstract class ResponseBody implements Closeable
A one-shot stream from the origin server to the client application with the raw bytes of the response body. Each response body is supported by an active connection to the webserver. This imposes both obligations and limits on the client application.
The response body must be closed.Each response body is backed by a limited resource like a socket (live network responses) or an open file (for cached responses). Failing to close the response body will leak resources and may ultimately cause the application to slow down or crash.Both this class and MobonResponse implement Closeable. Closing a response simply closes its response body. If you invoke execute or implement you must close this body by calling any of the following methods:
- MobonResponse.close()
- MobonResponse.body().close()
- MobonResponse.body().source().close()
- MobonResponse.body().charStream().close()
- MobonResponse.body().byteStream().close()
- MobonResponse.body().bytes()
- MobonResponse.body().string()
There is no benefit to invoking multiple
{@code close()}methods for the same response body.For synchronous calls, the easiest way to make sure a response body is closed is with a
{@code * try}block. With this structure the compiler inserts an implicit{@code finally}clause that calls{@code close()}for you.
You can use a similar block for asynchronous calls:{@code * * Call call = client.newCall(mobonRequest); * try (MobonResponse response = call.execute()) { * ... // Use the response. * } * }
These examples will not work if you're consuming the response body on another thread. In such cases the consuming thread must call close when it has finished reading the response body. The response body can be consumed only once.{@code * * Call call = client.newCall(mobonRequest); * call.enqueue(new Callback() { * public void onResponse(Call call, MobonResponse response) throws IOException { * try (ResponseBody responseBody = response.body()) { * ... // Use the response. * } * } * * public void onFailure(Call call, IOException e) { * ... // Handle the failure. * } * }); * }This class may be used to stream very large responses. For example, it is possible to use this class to read a response that is larger than the entire memory allocated to the current process. It can even stream a response larger than the total storage on the current device, which is a common requirement for video streaming applications.
Because this class does not buffer the full response in memory, the application may not re-read the bytes of the response. Use this one shot to read the entire response into memory with bytes or string. Or stream the response with either source, byteStream, or charStream.
-
-
Method Summary
Modifier and Type Method Description abstract MediaTypecontentType()abstract longcontentLength()Returns the number of bytes in that will returned by bytes, or byteStream, or-1 if unknown. final InputStreambyteStream()abstract BufferedSourcesource()final Array<byte>bytes()Returns the response as a byte array. final ReadercharStream()Returns the response as a character stream decoded with the charset of the Content-Type header. final Stringstring()Returns the response as a string decoded with the charset of the Content-Type header. voidclose()static ResponseBodycreate(@Nullable() MediaType contentType, String content)Returns a new response body that transmits {@code content}.static ResponseBodycreate(@Nullable() MediaType contentType, Array<byte> content)Returns a new response body that transmits {@code content}.static ResponseBodycreate(@Nullable() MediaType contentType, long contentLength, BufferedSource content)Returns a new response body that transmits {@code content}.-
-
Method Detail
-
contentType
@Nullable() abstract MediaType contentType()
-
contentLength
abstract long contentLength()
Returns the number of bytes in that will returned by bytes, or byteStream, or-1 if unknown.
-
byteStream
final InputStream byteStream()
-
source
abstract BufferedSource source()
-
bytes
final Array<byte> bytes()
Returns the response as a byte array.
This method loads entire response body into memory. If the response body is very large thismay trigger an OutOfMemoryError. Prefer to stream the response body if this is apossibility for your response.
-
charStream
final Reader charStream()
Returns the response as a character stream decoded with the charset of the Content-Type header.If that header is either absent or lacks a charset, this will attempt to decode the responsebody in accordance to its BOM orUTF-8.
-
string
final String string()
Returns the response as a string decoded with the charset of the Content-Type header. If thatheader is either absent or lacks a charset, this will attempt to decode the response body inaccordance to its BOM or UTF-8.Closes ResponseBody automatically.
This method loads entire response body into memory. If the response body is very large thismay trigger an OutOfMemoryError. Prefer to stream the response body if this is apossibility for your response.
-
close
void close()
-
create
static ResponseBody create(@Nullable() MediaType contentType, String content)
Returns a new response body that transmits
{@code content}. If{@code contentType}is non-nulland lacks a charset, this will use UTF-8.
-
create
static ResponseBody create(@Nullable() MediaType contentType, Array<byte> content)
Returns a new response body that transmits
{@code content}.
-
create
static ResponseBody create(@Nullable() MediaType contentType, long contentLength, BufferedSource content)
Returns a new response body that transmits
{@code content}.
-
-
-
-