Class ResponseBody

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:
    RealResponseBody


    public abstract class ResponseBody
    extends Object
    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 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.

    The response body can be consumed only once.

    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 Detail

      • ResponseBody

        public ResponseBody()
    • Method Detail

      • contentType

        public abstract MediaType contentType()
      • contentLength

        public abstract long contentLength()
        Returns the number of bytes in that will returned by bytes(), or byteStream(), or -1 if unknown.
      • source

        public abstract okio.BufferedSource source()
      • charStream

        public 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 response body as UTF-8.
      • string

        public final String string()
                            throws IOException
        Returns the response as a string 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 response body as UTF-8.
        Throws:
        IOException
      • close

        public void close()
      • create

        public static ResponseBody create(MediaType contentType,
                                          String content)
        Returns a new response body that transmits content. If contentType is non-null and lacks a charset, this will use UTF-8.
      • create

        public static ResponseBody create(MediaType contentType,
                                          byte[] content)
        Returns a new response body that transmits content.
      • create

        public static ResponseBody create(MediaType contentType,
                                          long contentLength,
                                          okio.BufferedSource content)
        Returns a new response body that transmits content.