diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java index 04151e9afe..0ac263f299 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java @@ -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 untarredFiles = new ArrayList<>(); try (TarArchiveInputStream tarInputStream = diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/RecompileGwtUiFilter.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/RecompileGwtUiFilter.java index 651d718cb0..90aedbe453 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/RecompileGwtUiFilter.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/RecompileGwtUiFilter.java @@ -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; diff --git a/gerrit-launcher/src/main/java/com/google/gerrit/launcher/GerritLauncher.java b/gerrit-launcher/src/main/java/com/google/gerrit/launcher/GerritLauncher.java index a7af05646a..b22ba49de6 100644 --- a/gerrit-launcher/src/main/java/com/google/gerrit/launcher/GerritLauncher.java +++ b/gerrit-launcher/src/main/java/com/google/gerrit/launcher/GerritLauncher.java @@ -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 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) { diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtobufImport.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtobufImport.java index 07e7921054..ad47f0c81b 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtobufImport.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtobufImport.java @@ -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 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 e = diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java index 0ea0f1a1d8..4ad770189c 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java @@ -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 { // 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 { 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) { diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/tools/hooks/HookTestCase.java b/gerrit-server/src/test/java/com/google/gerrit/server/tools/hooks/HookTestCase.java index 21c876462f..3d4a1a0917 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/tools/hooks/HookTestCase.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/tools/hooks/HookTestCase.java @@ -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); } } diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/PluginInstallCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/PluginInstallCommand.java index 5a47cb0899..820052c17f 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/PluginInstallCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/PluginInstallCommand.java @@ -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 { diff --git a/gerrit-war/src/main/java/com/google/gerrit/httpd/UnzippedDistribution.java b/gerrit-war/src/main/java/com/google/gerrit/httpd/UnzippedDistribution.java index b5a1daef90..ec92fbab7d 100644 --- a/gerrit-war/src/main/java/com/google/gerrit/httpd/UnzippedDistribution.java +++ b/gerrit-war/src/main/java/com/google/gerrit/httpd/UnzippedDistribution.java @@ -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); } } diff --git a/lib/asciidoctor/java/AsciiDoctor.java b/lib/asciidoctor/java/AsciiDoctor.java index d765cc1e56..219cc24436 100644 --- a/lib/asciidoctor/java/AsciiDoctor.java +++ b/lib/asciidoctor/java/AsciiDoctor.java @@ -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(); diff --git a/lib/asciidoctor/java/DocIndexer.java b/lib/asciidoctor/java/DocIndexer.java index 395f9fe82f..fbb7f94583 100644 --- a/lib/asciidoctor/java/DocIndexer.java +++ b/lib/asciidoctor/java/DocIndexer.java @@ -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 diff --git a/lib/prolog/java/BuckPrologCompiler.java b/lib/prolog/java/BuckPrologCompiler.java index d3f41c08cf..cc3e39e603 100644 --- a/lib/prolog/java/BuckPrologCompiler.java +++ b/lib/prolog/java/BuckPrologCompiler.java @@ -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];