Close some streams that we didn't
--
MOS_MIGRATED_REVID=107048547
diff --git a/src/main/java/com/google/devtools/build/docgen/Page.java b/src/main/java/com/google/devtools/build/docgen/Page.java
index a03a98d..d86dfb9 100644
--- a/src/main/java/com/google/devtools/build/docgen/Page.java
+++ b/src/main/java/com/google/devtools/build/docgen/Page.java
@@ -14,8 +14,6 @@
package com.google.devtools.build.docgen;
-import static java.nio.charset.StandardCharsets.UTF_8;
-
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
@@ -24,11 +22,8 @@
import java.io.File;
import java.io.FileWriter;
-import java.io.StringWriter;
import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
+import java.io.StringWriter;
/**
* Class that represents a page to be generated using the {@link TemplateEngine}.
@@ -70,11 +65,11 @@
stringWriter.close();
String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
- FileWriter fileWriter = new FileWriter(outputFile);
- for (String line : lines) {
- // Strip trailing whitespace then append newline before writing to file.
- fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
+ try (FileWriter fileWriter = new FileWriter(outputFile)) {
+ for (String line : lines) {
+ // Strip trailing whitespace then append newline before writing to file.
+ fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
+ }
}
- fileWriter.close();
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
index 2540839..aebc209 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
@@ -361,17 +361,17 @@
for (Artifact artifact : pruningManifest.getCandidateRunfiles()) {
allowedRunfiles.put(artifact.getRootRelativePath().getPathString(), artifact);
}
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(pruningManifest.getManifestFile().getPath().getInputStream()));
- String line;
- while ((line = reader.readLine()) != null) {
- Artifact artifact = allowedRunfiles.get(line);
- if (artifact != null) {
- manifest.put(artifact.getRootRelativePath(), artifact);
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(pruningManifest.getManifestFile().getPath().getInputStream()))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ Artifact artifact = allowedRunfiles.get(line);
+ if (artifact != null) {
+ manifest.put(artifact.getRootRelativePath(), artifact);
+ }
}
}
}
-
manifest = filterListForObscuringSymlinks(eventHandler, location, manifest);
// TODO(bazel-team): Create /dev/null-like Artifact to avoid nulls?
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/ZipFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/ZipFunction.java
index f0783ec..6b9bd1a 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/ZipFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/ZipFunction.java
@@ -29,6 +29,7 @@
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
@@ -116,8 +117,9 @@
// The zip file is not re-unzipped when the WORKSPACE file is changed (because it is assumed
// to be immutable) but is on server restart (which is a bug).
File outputFile = outputPath.getPathFile();
- Files.copy(reader.getInputStream(entry), outputFile.toPath(),
- StandardCopyOption.REPLACE_EXISTING);
+ try (InputStream input = reader.getInputStream(entry)) {
+ Files.copy(input, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ }
outputPath.chmod(permissions);
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationLoader.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationLoader.java
index feb5ffd..0a15404 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationLoader.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationLoader.java
@@ -43,6 +43,7 @@
import com.google.protobuf.UninitializedMessageException;
import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
@@ -224,7 +225,9 @@
return new CrosstoolProto(path.getMD5Digest(), "CROSSTOOL file " + path.getPathString()) {
@Override
public String getContents() throws IOException {
- return new String(FileSystemUtils.readContentAsLatin1(path.getInputStream()));
+ try (InputStream inputStream = path.getInputStream()) {
+ return new String(FileSystemUtils.readContentAsLatin1(inputStream));
+ }
}
};
}
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
index d396139..55d49ed 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
@@ -142,18 +142,20 @@
try {
Path canonicalPath = path.resolveSymbolicLinks();
Path mountTable = path.getRelative("/proc/mounts");
- for (String line : CharStreams.readLines(new InputStreamReader(mountTable.getInputStream(),
- ISO_8859_1))) {
- String[] words = line.split("\\s+");
- if (words.length >= 3) {
- if (!words[1].startsWith("/")) {
- continue;
- }
- Path mountPoint = path.getFileSystem().getPath(words[1]);
- int segmentCount = mountPoint.asFragment().segmentCount();
- if (canonicalPath.startsWith(mountPoint) && segmentCount > bestMountPointSegmentCount) {
- bestMountPointSegmentCount = segmentCount;
- fileSystem = words[2];
+ try (InputStreamReader reader = new InputStreamReader(mountTable.getInputStream(),
+ ISO_8859_1)) {
+ for (String line : CharStreams.readLines(reader)) {
+ String[] words = line.split("\\s+");
+ if (words.length >= 3) {
+ if (!words[1].startsWith("/")) {
+ continue;
+ }
+ Path mountPoint = path.getFileSystem().getPath(words[1]);
+ int segmentCount = mountPoint.asFragment().segmentCount();
+ if (canonicalPath.startsWith(mountPoint) && segmentCount > bestMountPointSegmentCount) {
+ bestMountPointSegmentCount = segmentCount;
+ fileSystem = words[2];
+ }
}
}
}