Use Java 7's try-with-resources.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1726955 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2016-01-27 08:27:37 +00:00
parent 69b509a06d
commit 6ba70b435c
3 changed files with 25 additions and 28 deletions

View File

@ -102,9 +102,9 @@ public class BasicAuthCache implements AuthCache {
if (authScheme instanceof Serializable) {
try {
final ByteArrayOutputStream buf = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buf);
out.writeObject(authScheme);
out.close();
try (final ObjectOutputStream out = new ObjectOutputStream(buf)) {
out.writeObject(authScheme);
}
this.map.put(getKey(host), buf.toByteArray());
} catch (IOException ex) {
if (log.isWarnEnabled()) {
@ -125,10 +125,9 @@ public class BasicAuthCache implements AuthCache {
if (bytes != null) {
try {
final ByteArrayInputStream buf = new ByteArrayInputStream(bytes);
final ObjectInputStream in = new ObjectInputStream(buf);
final AuthScheme authScheme = (AuthScheme) in.readObject();
in.close();
return authScheme;
try (final ObjectInputStream in = new ObjectInputStream(buf)) {
return (AuthScheme) in.readObject();
}
} catch (IOException ex) {
if (log.isWarnEnabled()) {
log.warn("Unexpected I/O error while de-serializing auth scheme", ex);

View File

@ -279,9 +279,9 @@ public class DigestScheme implements AuthScheme, Serializable {
}
final StringBuilder sb = new StringBuilder(8);
final Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("%08x", nounceCount);
formatter.close();
try (final Formatter formatter = new Formatter(sb, Locale.US)) {
formatter.format("%08x", nounceCount);
}
final String nc = sb.toString();
if (cnonce == null) {

View File

@ -211,29 +211,27 @@ public abstract class CloseableHttpClient implements HttpClient, Closeable {
*/
@Override
public <T> T execute(final HttpHost target, final HttpRequest request,
final ResponseHandler<? extends T> responseHandler, final HttpContext context)
throws IOException {
final ResponseHandler<? extends T> responseHandler, final HttpContext context) throws IOException {
Args.notNull(responseHandler, "Response handler");
final CloseableHttpResponse response = execute(target, request, context);
try {
final T result = responseHandler.handleResponse(response);
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
return result;
} catch (final ClientProtocolException t) {
// Try to salvage the underlying connection in case of a protocol exception
final HttpEntity entity = response.getEntity();
try (final CloseableHttpResponse response = execute(target, request, context)) {
try {
final T result = responseHandler.handleResponse(response);
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch (final Exception t2) {
// Log this exception. The original exception is more
// important and will be thrown to the caller.
this.log.warn("Error consuming content after an exception.", t2);
return result;
} catch (final ClientProtocolException t) {
// Try to salvage the underlying connection in case of a protocol exception
final HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (final Exception t2) {
// Log this exception. The original exception is more
// important and will be thrown to the caller.
this.log.warn("Error consuming content after an exception.", t2);
}
throw t;
}
throw t;
} finally {
response.close();
}
}