Skip to content

Commit b3ff4ac

Browse files
authored
Merge pull request hub4j#984 from gsmet/okhttp-close-responsebody
Close Okhttp ResponseBody instances when closing the InputStream
2 parents fafb23c + 4803dac commit b3ff4ac

1 file changed

Lines changed: 70 additions & 3 deletions

File tree

src/main/java/org/kohsuke/github/extras/okhttp3/ObsoleteUrlFactory.java

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import okhttp3.Request;
1515
import okhttp3.RequestBody;
1616
import okhttp3.Response;
17+
import okhttp3.ResponseBody;
1718
import okio.Buffer;
1819
import okio.BufferedSink;
1920
import okio.Okio;
@@ -179,7 +180,7 @@ HttpURLConnection open(URL url, @Nullable Proxy proxy) {
179180
* <p>
180181
* This code configures OkHttp to handle all HTTP and HTTPS connections created with
181182
* {@link java.net.URL#openConnection()}:
182-
*
183+
*
183184
* <pre>
184185
* {
185186
* &#64;code
@@ -404,7 +405,7 @@ public InputStream getErrorStream() {
404405
try {
405406
Response response = getResponse(true);
406407
if (hasBody(response) && response.code() >= HTTP_BAD_REQUEST) {
407-
return response.body().byteStream();
408+
return new ResponseBodyInputStream(response.body());
408409
}
409410
return null;
410411
} catch (IOException e) {
@@ -486,7 +487,7 @@ public InputStream getInputStream() throws IOException {
486487
Response response = getResponse(false);
487488
if (response.code() >= HTTP_BAD_REQUEST)
488489
throw new FileNotFoundException(url.toString());
489-
return response.body().byteStream();
490+
return new ResponseBodyInputStream(response.body());
490491
}
491492

492493
@Override
@@ -957,6 +958,7 @@ static final class StreamedRequestBody extends OutputStreamRequestBody {
957958
initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength);
958959
}
959960

961+
@Override
960962
public boolean isOneShot() {
961963
return true;
962964
}
@@ -1367,4 +1369,69 @@ static final class UnexpectedException extends IOException {
13671369
super(cause);
13681370
}
13691371
}
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+
}
13701437
}

0 commit comments

Comments
 (0)