1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-18 20:40:58 +03:00

Close the source after reading regardless of result.

This commit is contained in:
Jake Wharton
2014-04-24 11:29:03 -07:00
parent 7052d7691e
commit 92e427d7c6
2 changed files with 9 additions and 2 deletions

View File

@@ -189,7 +189,6 @@ public class JavaApiConverterTest {
assertEquals("Fantastic", response.message());
Headers okResponseHeaders = response.headers();
assertEquals("baz", okResponseHeaders.get("xyzzy"));
assertEquals(body, response.body().string());
if (isSecure) {
Handshake handshake = response.handshake();
assertNotNull(handshake);
@@ -204,6 +203,7 @@ public class JavaApiConverterTest {
} else {
assertNull(response.handshake());
}
assertEquals(body, response.body().string());
}
@Test public void createOkResponse_fromCacheResponse() throws Exception {

View File

@@ -15,6 +15,7 @@
*/
package com.squareup.okhttp;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.http.OkHeaders;
import java.io.Closeable;
import java.io.IOException;
@@ -205,8 +206,14 @@ public final class Response {
// }
// return bytes;
BufferedSource source = source();
Buffer buffer = new Buffer();
long contentRead = buffer.writeAll(source());
long contentRead;
try {
contentRead = buffer.writeAll(source);
} finally {
Util.closeQuietly(source);
}
if (contentLength != -1 && contentLength != contentRead) {
throw new IOException("Content-Length and stream length disagree");
}