public abstract class ResponseBody extends Objectimplements 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
Returns a new response body that transmits
content.
|
static ResponseBody |
create(MediaType
Returns a new response body that transmits
content.
|
static ResponseBody |
create(MediaType
Returns a new response body that transmits
content.
|
abstract okio |
source()
|
String |
string()
Returns the response as a string decoded with the charset of the Content-Type header.
|
public abstract MediaTypecontentType()
public abstract long contentLength()
bytes(), or
byteStream(), or -1 if unknown.
public final InputStreambyteStream()
public abstract okio.BufferedSource source()
public final byte[] bytes()
throws IOException
IOException
public final ReadercharStream()
public final Stringstring() throws IOException
IOException
public void close()
public static ResponseBodycreate(MediaType contentType, String content)
content. If
contentType is non-null and lacks a charset, this will use UTF-8.
public static ResponseBodycreate(MediaType contentType, byte[] content)
content.
public static ResponseBodycreate(MediaType contentType, long contentLength, okio .BufferedSource content)
content.