private Source getTransferStream(Response response) throws IOException { if (!HttpEngine.hasBody(response)) { return httpConnection.newFixedLengthSource(0); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { return httpConnection.newChunkedSource(httpEngine); } long contentLength = OkHeaders.contentLength(response); if (contentLength != -1) { return httpConnection.newFixedLengthSource(contentLength); } // Wrap the input stream from the connection (rather than just returning // "socketIn" directly here), so that we can control its use after the // reference escapes. return httpConnection.newUnknownLengthSource(); }
@Override public ResponseBody openResponseBody(Response response) throws IOException { Source source = getTransferStream(response); return new RealResponseBody(response.headers(), Okio.buffer(source)); }