From 6ba70b435c0313725cd872ea283b9596f0b414fd Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Wed, 27 Jan 2016 08:27:37 +0000 Subject: [PATCH] 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 --- .../http/impl/auth/BasicAuthCache.java | 13 ++++--- .../client5/http/impl/auth/DigestScheme.java | 6 ++-- .../http/impl/sync/CloseableHttpClient.java | 34 +++++++++---------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicAuthCache.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicAuthCache.java index fe82e1af8..980138836 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicAuthCache.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicAuthCache.java @@ -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); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java index fc1f95682..4d7c34544 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java @@ -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) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/CloseableHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/CloseableHttpClient.java index 37b9f55c6..eec221c7f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/CloseableHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/CloseableHttpClient.java @@ -211,29 +211,27 @@ public abstract class CloseableHttpClient implements HttpClient, Closeable { */ @Override public T execute(final HttpHost target, final HttpRequest request, - final ResponseHandler responseHandler, final HttpContext context) - throws IOException { + final ResponseHandler 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(); } }