From fa94af156cc50d7d0b4ec5312549e4e5c01841d7 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Mon, 24 Feb 2014 15:07:42 +0100 Subject: [PATCH] Fix unreleased stream resources Some streams were not properly closed. 1. ProtoGen: The OutputStream returned by lock.getOutputStream() was not closed in case the instantiation of OutputStreamWriter failed with an IOException. 2. InitUtil: The InputStream was never closed. IO.readWholeStream(...) says that it is the responsibility of the caller to close the stream. 3. Libraries: The InputStream was not closed in case the instantiation of InputStreamReader failed with an IOException. 4. JythonShell: The InputStream was never closed. 5. LogFileCompressor: The FileOuputStream was not closed in case the instantiation of GZIPOutputStream failed with an IOException. 6. Version: The InputStream was not closed in case the instantiation of InputStreamReader failed with an IOException. 7. PrologCompiler: The FileOutputStream was not closed in case the instantiation of JarOutputStream failed with an IOException. 8. EncryptedContactStore: The FileInputStream was not closed in case PGPUtil.getDecoderStream(in) failed with an IOException. 9. HttpContactStoreConnection: The OutputStream was not closed in case out.write(body) failed with an IOException. Change-Id: I05e10ab67b2daaf85b9324a13c8c8daf510545f7 Signed-off-by: Edwin Kempin --- .../java/com/google/gerrit/pgm/ProtoGen.java | 9 +++---- .../com/google/gerrit/pgm/init/InitUtil.java | 9 ++++--- .../com/google/gerrit/pgm/init/Libraries.java | 26 +++++++++---------- .../google/gerrit/pgm/shell/JythonShell.java | 14 ++++++---- .../gerrit/pgm/util/LogFileCompressor.java | 20 +++++--------- .../com/google/gerrit/common/Version.java | 19 +++++++------- .../google/gerrit/rules/PrologCompiler.java | 9 +++---- .../server/contact/EncryptedContactStore.java | 10 ++----- .../contact/HttpContactStoreConnection.java | 6 ++--- 9 files changed, 55 insertions(+), 67 deletions(-) diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtoGen.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtoGen.java index 4512078b24..12e1e99b9b 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtoGen.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtoGen.java @@ -26,6 +26,7 @@ import org.kohsuke.args4j.Option; import java.io.BufferedWriter; import java.io.File; import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.ByteBuffer; @@ -42,9 +43,9 @@ public class ProtoGen extends AbstractProgram { } try { JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class); - PrintWriter out = new PrintWriter(new BufferedWriter( - new OutputStreamWriter(lock.getOutputStream(), "UTF-8"))); - try { + try (OutputStream o = lock.getOutputStream(); + PrintWriter out = new PrintWriter( + new BufferedWriter(new OutputStreamWriter(o, "UTF-8")))) { String header; InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt"); try { @@ -60,8 +61,6 @@ public class ProtoGen extends AbstractProgram { out.write(header.replace("@@VERSION@@", version)); jsm.generateProto(out); out.flush(); - } finally { - out.close(); } if (!lock.commit()) { throw die("Could not write to " + file); diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitUtil.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitUtil.java index 0ad756041c..dde0b065f0 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitUtil.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitUtil.java @@ -162,10 +162,11 @@ class InitUtil { static void extract(final File dst, final Class sibling, final String name) throws IOException { - final InputStream in = open(sibling, name); - if (in != null) { - ByteBuffer buf = IO.readWholeStream(in, 8192); - copy(dst, buf); + try (InputStream in = open(sibling, name)) { + if (in != null) { + ByteBuffer buf = IO.readWholeStream(in, 8192); + copy(dst, buf); + } } } diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/Libraries.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/Libraries.java index f4a673c43c..7209990d04 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/Libraries.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/Libraries.java @@ -106,21 +106,19 @@ class Libraries { } private static String read(final String p) throws IOException { - InputStream in = Libraries.class.getClassLoader().getResourceAsStream(p); - if (in == null) { - throw new FileNotFoundException("Cannot load resource " + p); - } - final Reader r = new InputStreamReader(in, "UTF-8"); - try { - final StringBuilder buf = new StringBuilder(); - final char[] tmp = new char[512]; - int n; - while (0 < (n = r.read(tmp))) { - buf.append(tmp, 0, n); + try (InputStream in = Libraries.class.getClassLoader().getResourceAsStream(p)) { + if (in == null) { + throw new FileNotFoundException("Cannot load resource " + p); + } + try (Reader r = new InputStreamReader(in, "UTF-8")) { + final StringBuilder buf = new StringBuilder(); + final char[] tmp = new char[512]; + int n; + while (0 < (n = r.read(tmp))) { + buf.append(tmp, 0, n); + } + return buf.toString(); } - return buf.toString(); - } finally { - r.close(); } } } diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/shell/JythonShell.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/shell/JythonShell.java index dfe28b7106..38f08c18ff 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/shell/JythonShell.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/shell/JythonShell.java @@ -20,6 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -165,11 +166,14 @@ public class JythonShell { } protected void execResource(final String p) { - InputStream in = JythonShell.class.getClassLoader().getResourceAsStream(p); - if (in != null) { - execStream(in, "resource " + p); - } else { - log.error("Cannot load resource " + p); + try (InputStream in = JythonShell.class.getClassLoader().getResourceAsStream(p)) { + if (in != null) { + execStream(in, "resource " + p); + } else { + log.error("Cannot load resource " + p); + } + } catch (IOException e) { + log.error(e.getMessage(), e); } } diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/LogFileCompressor.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/LogFileCompressor.java index b5af54ea66..aed1b9a286 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/LogFileCompressor.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/LogFileCompressor.java @@ -118,21 +118,15 @@ public class LogFileCompressor implements Runnable { final File dst = new File(dir, src.getName() + ".gz"); final File tmp = new File(dir, ".tmp." + src.getName()); try { - final InputStream in = new FileInputStream(src); - try { - OutputStream out = new GZIPOutputStream(new FileOutputStream(tmp)); - try { - final byte[] buf = new byte[2048]; - int n; - while (0 < (n = in.read(buf))) { - out.write(buf, 0, n); - } - } finally { - out.close(); + try (InputStream in = new FileInputStream(src); + FileOutputStream fo = new FileOutputStream(tmp); + OutputStream out = new GZIPOutputStream(fo)) { + final byte[] buf = new byte[2048]; + int n; + while (0 < (n = in.read(buf))) { + out.write(buf, 0, n); } tmp.setReadOnly(); - } finally { - in.close(); } if (!tmp.renameTo(dst)) { throw new IOException("Cannot rename " + tmp + " to " + dst); diff --git a/gerrit-server/src/main/java/com/google/gerrit/common/Version.java b/gerrit-server/src/main/java/com/google/gerrit/common/Version.java index 8ad4d48509..e69360a29d 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/common/Version.java +++ b/gerrit-server/src/main/java/com/google/gerrit/common/Version.java @@ -14,12 +14,16 @@ package com.google.gerrit.common; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Version { + private static final Logger log = LoggerFactory.getLogger(Version.class); private static final String version; public static String getVersion() { @@ -31,13 +35,11 @@ public class Version { } private static String loadVersion() { - InputStream in = Version.class.getResourceAsStream("Version"); - if (in == null) { - return null; - } - try { - BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8")); - try { + try (InputStream in = Version.class.getResourceAsStream("Version")) { + if (in == null) { + return null; + } + try (BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8"))) { String vs = r.readLine(); if (vs != null && vs.startsWith("v")) { vs = vs.substring(1); @@ -46,10 +48,9 @@ public class Version { vs = null; } return vs; - } finally { - r.close(); } } catch (IOException e) { + log.error(e.getMessage(), e); return null; } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/rules/PrologCompiler.java b/gerrit-server/src/main/java/com/google/gerrit/rules/PrologCompiler.java index ab57059815..8202ac25ea 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/rules/PrologCompiler.java +++ b/gerrit-server/src/main/java/com/google/gerrit/rules/PrologCompiler.java @@ -235,10 +235,9 @@ public class PrologCompiler implements Callable { mf.getMainAttributes().putValue("Source-Commit", metaConfig.name()); mf.getMainAttributes().putValue("Source-Blob", rulesId.name()); - FileOutputStream stream = new FileOutputStream(tmpjar); - JarOutputStream out = new JarOutputStream(stream, mf); - byte buffer[] = new byte[10240]; - try { + try (FileOutputStream stream = new FileOutputStream(tmpjar); + JarOutputStream out = new JarOutputStream(stream, mf)) { + byte buffer[] = new byte[10240]; for (String path : toBeJared) { JarEntry jarAdd = new JarEntry(path); File f = new File(tempDir, path); @@ -260,8 +259,6 @@ public class PrologCompiler implements Callable { } out.closeEntry(); } - } finally { - out.close(); } if (!tmpjar.renameTo(archiveFile)) { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/contact/EncryptedContactStore.java b/gerrit-server/src/main/java/com/google/gerrit/server/contact/EncryptedContactStore.java index 40699c83de..8c1fdb6b79 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/contact/EncryptedContactStore.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/contact/EncryptedContactStore.java @@ -108,16 +108,10 @@ class EncryptedContactStore implements ContactStore { return true; } - @SuppressWarnings("resource") private static PGPPublicKeyRingCollection readPubRing(final File pub) { - try { - InputStream in = new FileInputStream(pub); - try { - in = PGPUtil.getDecoderStream(in); + try (InputStream fin = new FileInputStream(pub); + InputStream in = PGPUtil.getDecoderStream(fin)) { return new PGPPublicKeyRingCollection(in); - } finally { - in.close(); - } } catch (IOException e) { throw new ProvisionException("Cannot read " + pub, e); } catch (PGPException e) { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/contact/HttpContactStoreConnection.java b/gerrit-server/src/main/java/com/google/gerrit/server/contact/HttpContactStoreConnection.java index 123c20d174..471f6a2f81 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/contact/HttpContactStoreConnection.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/contact/HttpContactStoreConnection.java @@ -48,9 +48,9 @@ public class HttpContactStoreConnection implements ContactStoreConnection { "application/x-www-form-urlencoded; charset=UTF-8"); conn.setDoOutput(true); conn.setFixedLengthStreamingMode(body.length); - final OutputStream out = conn.getOutputStream(); - out.write(body); - out.close(); + try (OutputStream out = conn.getOutputStream()) { + out.write(body); + } if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("Connection failed: " + conn.getResponseCode()); }