public abstract class ResponseBody extends Object implements Closeable
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 these resources
and may ultimately cause the application to slow down or crash. Close the response body by
calling either close(), byteStream().close(), or reader().close(). The bytes() and
string() methods both close the response body automatically.
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().
| Constructor and Description |
|---|
ResponseBody() |
| Modifier and Type | Method and Description |
|---|---|
byte[] |
bytes() |
InputStream |
byteStream() |
Reader |
charStream()
Returns the response as a character stream decoded with the charset of the Content-Type header.
|
void |
close() |
abstract long |
contentLength()
Returns the number of bytes in that will returned by
bytes(), or byteStream(), or
-1 if unknown. |
abstract MediaType |
contentType() |
static ResponseBody |
create(MediaType contentType,
byte[] content)
Returns a new response body that transmits
content. |
static ResponseBody |
create(MediaType contentType,
long contentLength,
okio.BufferedSource content)
Returns a new response body that transmits
content. |
static ResponseBody |
create(MediaType contentType,
String content)
Returns a new response body that transmits
content. |
abstract okio.BufferedSource |
source() |
String |
string()
Returns the response as a string decoded with the charset of the Content-Type header.
|
public abstract MediaType contentType()
public abstract long contentLength()
bytes(), or byteStream(), or
-1 if unknown.public final InputStream byteStream()
public abstract okio.BufferedSource source()
public final byte[] bytes()
throws IOException
IOExceptionpublic final Reader charStream()
public final String string() throws IOException
IOExceptionpublic void close()
close in interface Closeableclose in interface AutoCloseablepublic static ResponseBody create(MediaType contentType, String content)
content. If contentType is non-null
and lacks a charset, this will use UTF-8.public static ResponseBody create(MediaType contentType, byte[] content)
content.public static ResponseBody create(MediaType contentType, long contentLength, okio.BufferedSource content)
content.Copyright © 2016. All Rights Reserved.