Merge "Fix unreleased stream resources"

This commit is contained in:
Saša Živkov
2014-02-25 10:02:06 +00:00
committed by Gerrit Code Review
9 changed files with 55 additions and 67 deletions

View File

@@ -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);

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -235,10 +235,9 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
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<PrologCompiler.Status> {
}
out.closeEntry();
}
} finally {
out.close();
}
if (!tmpjar.renameTo(archiveFile)) {

View File

@@ -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) {

View File

@@ -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());
}