HTTPCLIENT-2206: Corrected resource de-allocation by fluent response objects

This commit is contained in:
Oleg Kalnichevski 2022-02-26 14:54:26 +01:00
parent c4e480ef78
commit 73e72f2268
1 changed files with 17 additions and 10 deletions

View File

@ -26,6 +26,7 @@
*/ */
package org.apache.http.client.fluent; package org.apache.http.client.fluent;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -116,6 +117,7 @@ public class Response {
public void saveContent(final File file) throws IOException { public void saveContent(final File file) throws IOException {
assertNotConsumed(); assertNotConsumed();
try {
final StatusLine statusLine = response.getStatusLine(); final StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) { if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(), throw new HttpResponseException(statusLine.getStatusCode(),
@ -128,9 +130,14 @@ public class Response {
entity.writeTo(out); entity.writeTo(out);
} }
} finally { } finally {
this.consumed = true;
out.close(); out.close();
} }
} finally {
this.consumed = true;
if (this.response instanceof Closeable) {
((Closeable) this.response).close();
}
}
} }
} }