Browse Source

TransportHttp: Refactor to use try-with-resource and suppress resource warning

Change-Id: I130269e7c5e46aea2152dea6b02539529208eea2
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.0
David Pursehouse 7 years ago
parent
commit
0bc2020412
  1. 33
      org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java

33
org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java

@ -336,6 +336,22 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
useSmartHttp = on; 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} */ /** {@inheritDoc} */
@Override @Override
public FetchConnection openFetch() throws TransportException, public FetchConnection openFetch() throws TransportException,
@ -343,21 +359,8 @@ public class TransportHttp extends HttpTransport implements WalkTransport,
final String service = SVC_UPLOAD_PACK; final String service = SVC_UPLOAD_PACK;
try { try {
final HttpConnection c = connect(service); final HttpConnection c = connect(service);
final InputStream in = openInputStream(c); try (InputStream in = openInputStream(c)) {
try { return getConnection(c, in, service);
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();
} }
} catch (NotSupportedException err) { } catch (NotSupportedException err) {
throw err; throw err;

Loading…
Cancel
Save