From 0bc2020412f36b2abf75e5aba1dd318443dbbb10 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 20 Mar 2018 11:37:30 +0900 Subject: [PATCH] TransportHttp: Refactor to use try-with-resource and suppress resource warning Change-Id: I130269e7c5e46aea2152dea6b02539529208eea2 Signed-off-by: David Pursehouse --- .../eclipse/jgit/transport/TransportHttp.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java index b300d55477..87c328f452 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java @@ -336,6 +336,22 @@ public class TransportHttp extends HttpTransport implements WalkTransport, useSmartHttp = on; } + @SuppressWarnings("resource") // Closed by caller + private FetchConnection getConnection(HttpConnection c, InputStream in, + String service) throws IOException { + BaseConnection f; + if (isSmartHttp(c, service)) { + readSmartHeaders(in, service); + f = new SmartHttpFetchConnection(in); + } else { + // Assume this server doesn't support smart HTTP fetch + // and fall back on dumb object walking. + f = newDumbConnection(in); + } + f.setPeerUserAgent(c.getHeaderField(HttpSupport.HDR_SERVER)); + return (FetchConnection) f; + } + /** {@inheritDoc} */ @Override public FetchConnection openFetch() throws TransportException, @@ -343,21 +359,8 @@ public class TransportHttp extends HttpTransport implements WalkTransport, final String service = SVC_UPLOAD_PACK; try { final HttpConnection c = connect(service); - final InputStream in = openInputStream(c); - try { - BaseConnection f; - if (isSmartHttp(c, service)) { - readSmartHeaders(in, service); - f = new SmartHttpFetchConnection(in); - } else { - // Assume this server doesn't support smart HTTP fetch - // and fall back on dumb object walking. - f = newDumbConnection(in); - } - f.setPeerUserAgent(c.getHeaderField(HttpSupport.HDR_SERVER)); - return (FetchConnection) f; - } finally { - in.close(); + try (InputStream in = openInputStream(c)) { + return getConnection(c, in, service); } } catch (NotSupportedException err) { throw err; -- 2.39.5