14
14
import okhttp3 .Request ;
15
15
import okhttp3 .RequestBody ;
16
16
import okhttp3 .Response ;
17
+ import okhttp3 .ResponseBody ;
17
18
import okio .Buffer ;
18
19
import okio .BufferedSink ;
19
20
import okio .Okio ;
@@ -179,7 +180,7 @@ HttpURLConnection open(URL url, @Nullable Proxy proxy) {
179
180
* <p>
180
181
* This code configures OkHttp to handle all HTTP and HTTPS connections created with
181
182
* {@link java.net.URL#openConnection()}:
182
- *
183
+ *
183
184
* <pre>
184
185
* {
185
186
* @code
@@ -404,7 +405,7 @@ public InputStream getErrorStream() {
404
405
try {
405
406
Response response = getResponse (true );
406
407
if (hasBody (response ) && response .code () >= HTTP_BAD_REQUEST ) {
407
- return response .body (). byteStream ( );
408
+ return new ResponseBodyInputStream ( response .body ());
408
409
}
409
410
return null ;
410
411
} catch (IOException e ) {
@@ -486,7 +487,7 @@ public InputStream getInputStream() throws IOException {
486
487
Response response = getResponse (false );
487
488
if (response .code () >= HTTP_BAD_REQUEST )
488
489
throw new FileNotFoundException (url .toString ());
489
- return response .body (). byteStream ( );
490
+ return new ResponseBodyInputStream ( response .body ());
490
491
}
491
492
492
493
@ Override
@@ -957,6 +958,7 @@ static final class StreamedRequestBody extends OutputStreamRequestBody {
957
958
initOutputStream (Okio .buffer (pipe .sink ()), expectedContentLength );
958
959
}
959
960
961
+ @ Override
960
962
public boolean isOneShot () {
961
963
return true ;
962
964
}
@@ -1367,4 +1369,69 @@ static final class UnexpectedException extends IOException {
1367
1369
super (cause );
1368
1370
}
1369
1371
}
1372
+
1373
+ /**
1374
+ * Make sure both the ResponseBody and the InputStream are closed when the InputStream coming from the ResponseBody
1375
+ * is closed.
1376
+ */
1377
+ private static final class ResponseBodyInputStream extends InputStream {
1378
+
1379
+ private final ResponseBody responseBody ;
1380
+
1381
+ private final InputStream inputStream ;
1382
+
1383
+ private ResponseBodyInputStream (ResponseBody responseBody ) {
1384
+ this .responseBody = responseBody ;
1385
+ this .inputStream = responseBody .byteStream ();
1386
+ }
1387
+
1388
+ @ Override
1389
+ public int read () throws IOException {
1390
+ return inputStream .read ();
1391
+ }
1392
+
1393
+ @ Override
1394
+ public int read (byte b []) throws IOException {
1395
+ return inputStream .read (b );
1396
+ }
1397
+
1398
+ @ Override
1399
+ public int read (byte b [], int off , int len ) throws IOException {
1400
+ return inputStream .read (b , off , len );
1401
+ }
1402
+
1403
+ @ Override
1404
+ public long skip (long n ) throws IOException {
1405
+ return inputStream .skip (n );
1406
+ }
1407
+
1408
+ @ Override
1409
+ public int available () throws IOException {
1410
+ return inputStream .available ();
1411
+ }
1412
+
1413
+ @ Override
1414
+ public synchronized void mark (int readlimit ) {
1415
+ inputStream .mark (readlimit );
1416
+ }
1417
+
1418
+ @ Override
1419
+ public synchronized void reset () throws IOException {
1420
+ inputStream .reset ();
1421
+ }
1422
+
1423
+ @ Override
1424
+ public boolean markSupported () {
1425
+ return inputStream .markSupported ();
1426
+ }
1427
+
1428
+ @ Override
1429
+ public void close () throws IOException {
1430
+ try {
1431
+ inputStream .close ();
1432
+ } finally {
1433
+ responseBody .close ();
1434
+ }
1435
+ }
1436
+ }
1370
1437
}
0 commit comments