Replace FileInputStream and FileOutputStream with static Files methods

FileInputStream and FileOutputStream rely on finalize() method to ensure
resources are closed. This implies they are added to the finalizer queue
which causes additional work for the JVM GC process.

This is an open bug on the OpenJDK [1] and the recommended workaround is
to use the Files.newInputStream and Files.newOutputStream static methods
instead.

[1] https://bugs.openjdk.java.net/browse/JDK-8080225

Change-Id: I3cef6fcf198dde2be7cd15bded8d2fa247177654
This commit is contained in:
Hector Oswaldo Caballero 2017-04-23 22:00:48 -04:00
parent fb2f355de3
commit db21e3add0
11 changed files with 35 additions and 35 deletions

View File

@ -30,9 +30,8 @@ import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -547,10 +546,10 @@ public class SubmitByMergeIfNecessaryIT extends AbstractSubmitByMerge {
try (BinaryResult request = submitPreview(change1.getChangeId(), "tgz")) {
assertThat(request.getContentType()).isEqualTo("application/x-gzip");
tempfile = File.createTempFile("test", null);
request.writeTo(new FileOutputStream(tempfile));
request.writeTo(Files.newOutputStream(tempfile.toPath()));
}
InputStream is = new GZIPInputStream(new FileInputStream(tempfile));
InputStream is = new GZIPInputStream(Files.newInputStream(tempfile.toPath()));
List<String> untarredFiles = new ArrayList<>();
try (TarArchiveInputStream tarInputStream =

View File

@ -16,9 +16,10 @@ package com.google.gerrit.httpd.raw;
import com.google.gwtexpui.linker.server.UserAgentRule;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.HashSet;
@ -102,7 +103,7 @@ class RecompileGwtUiFilter implements Filter {
mkdir(rawtmp.getParentFile());
rawtmp.deleteOnExit();
try (FileOutputStream rawout = new FileOutputStream(rawtmp);
try (OutputStream rawout = Files.newOutputStream(rawtmp.toPath());
InputStream in = zf.getInputStream(ze)) {
final byte[] buf = new byte[4096];
int n;

View File

@ -20,9 +20,9 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -293,7 +293,7 @@ public final class GerritLauncher {
private static void extractJar(ZipFile zf, ZipEntry ze, SortedMap<String, URL> jars)
throws IOException {
File tmp = createTempFile(safeName(ze), ".jar");
try (FileOutputStream out = new FileOutputStream(tmp);
try (OutputStream out = Files.newOutputStream(tmp.toPath());
InputStream in = zf.getInputStream(ze)) {
byte[] buf = new byte[4096];
int n;
@ -414,7 +414,7 @@ public final class GerritLauncher {
if (src != null) {
try (InputStream in = src.getLocation().openStream()) {
final File tmp = createTempFile("gerrit_", ".zip");
try (FileOutputStream out = new FileOutputStream(tmp)) {
try (OutputStream out = Files.newOutputStream(tmp.toPath())) {
final byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf, 0, buf.length)) > 0) {

View File

@ -39,10 +39,10 @@ import com.google.protobuf.Parser;
import com.google.protobuf.UnknownFieldSet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@ -102,7 +102,7 @@ public class ProtobufImport extends SiteProgram {
}
Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType();
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
UnknownFieldSet msg;
while ((msg = parser.parseDelimitedFrom(in)) != null) {
Map.Entry<Integer, UnknownFieldSet.Field> e =

View File

@ -24,10 +24,9 @@ import com.google.inject.assistedinject.Assisted;
import com.googlecode.prolog_cafe.compiler.Compiler;
import com.googlecode.prolog_cafe.exceptions.CompileException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
@ -134,7 +133,7 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
// Any leak of tmp caused by this method failing will be cleaned
// up by our caller when tempDir is recursively deleted.
File tmp = File.createTempFile("rules", ".pl", tempDir);
try (FileOutputStream out = new FileOutputStream(tmp)) {
try (OutputStream out = Files.newOutputStream(tmp.toPath())) {
git.open(blobId).copyTo(out);
}
return tmp;
@ -230,7 +229,7 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
jarAdd.setTime(now);
out.putNextEntry(jarAdd);
if (f.isFile()) {
try (FileInputStream in = new FileInputStream(f)) {
try (InputStream in = Files.newInputStream(f.toPath())) {
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0) {

View File

@ -54,10 +54,11 @@ import static com.google.common.truth.Truth.assert_;
import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -119,7 +120,7 @@ public abstract class HookTestCase extends LocalDiskRepositoryTestCase {
try (InputStream in = url.openStream()) {
hook = File.createTempFile("hook_", ".sh");
cleanup.add(hook);
try (FileOutputStream out = new FileOutputStream(hook)) {
try (OutputStream out = Files.newOutputStream(hook.toPath())) {
ByteStreams.copy(in, out);
}
}

View File

@ -25,12 +25,11 @@ import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@ -54,6 +53,7 @@ final class PluginInstallCommand extends SshCommand {
@Inject private PluginLoader loader;
@SuppressWarnings("resource")
@Override
protected void run() throws UnloggedFailure {
if (!loader.isRemoteAdminEnabled()) {
@ -80,8 +80,8 @@ final class PluginInstallCommand extends SshCommand {
data = in;
} else if (new File(source).isFile() && source.equals(new File(source).getAbsolutePath())) {
try {
data = new FileInputStream(new File(source));
} catch (FileNotFoundException e) {
data = Files.newInputStream(new File(source).toPath());
} catch (IOException e) {
throw die("cannot read " + source);
}
} else {

View File

@ -20,10 +20,10 @@ import static com.google.gerrit.pgm.init.InitPlugins.PLUGIN_DIR;
import com.google.gerrit.pgm.init.PluginsDistribution;
import com.google.inject.Singleton;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
@ -45,7 +45,7 @@ class UnzippedDistribution implements PluginsDistribution {
for (File p : list) {
String pluginJarName = p.getName();
String pluginName = pluginJarName.substring(0, pluginJarName.length() - JAR.length());
try (InputStream in = new FileInputStream(p)) {
try (InputStream in = Files.newInputStream(p.toPath())) {
processor.process(pluginName, in);
}
}

View File

@ -15,12 +15,12 @@
import com.google.common.io.ByteStreams;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -162,7 +162,7 @@ public class AsciiDoctor {
if (bazel) {
renderFiles(inputFiles, null);
} else {
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile))) {
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(Paths.get(zipFile)))) {
renderFiles(inputFiles, zip);
File[] cssFiles =
@ -199,7 +199,7 @@ public class AsciiDoctor {
public static void zipFile(File file, String name, ZipOutputStream zip) throws IOException {
zip.putNextEntry(new ZipEntry(name));
try (FileInputStream input = new FileInputStream(file)) {
try (InputStream input = Files.newInputStream(file.toPath())) {
ByteStreams.copy(input, zip);
}
zip.closeEntry();

View File

@ -18,13 +18,13 @@ import com.google.gerrit.server.documentation.Constants;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
@ -81,7 +81,7 @@ public class DocIndexer {
return;
}
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(outFile))) {
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(Paths.get(outFile)))) {
byte[] compressedIndex = zip(index());
JarEntry entry = new JarEntry(String.format("%s/%s", Constants.PACKAGE, Constants.INDEX_ZIP));
entry.setSize(compressedIndex.length);
@ -106,7 +106,7 @@ public class DocIndexer {
String title;
try (BufferedReader titleReader =
new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8))) {
new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath()), UTF_8))) {
title = titleReader.readLine();
if (title != null && title.startsWith("[[")) {
// Generally the first line of the txt is the title. In a few cases the

View File

@ -15,9 +15,9 @@
import com.googlecode.prolog_cafe.compiler.Compiler;
import com.googlecode.prolog_cafe.exceptions.CompileException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
@ -46,7 +46,7 @@ public class BuckPrologCompiler {
private static void jar(File jar, File classes) throws IOException {
File tmp = File.createTempFile("prolog", ".jar", tmpdir);
try {
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(tmp))) {
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(tmp.toPath()))) {
add(out, classes, "");
}
if (!tmp.renameTo(jar)) {
@ -70,7 +70,7 @@ public class BuckPrologCompiler {
}
JarEntry e = new JarEntry(prefix + name);
try (FileInputStream in = new FileInputStream(f)) {
try (InputStream in = Files.newInputStream(f.toPath())) {
e.setTime(f.lastModified());
out.putNextEntry(e);
byte[] buf = new byte[16 << 10];