aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar laszlocsomor <laszlocsomor@google.com>2018-07-10 06:14:49 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-10 06:15:52 -0700
commitdda5bcedea18009a26adbb2dd01c82a7edfe9917 (patch)
treec86c1e0c8588d133eb2d1a77597da0df0b5bd5bd /src/main/java/com/google/devtools
parent9c883bf10568b818c7b5f8e80b19ba305e49206e (diff)
Bazel server, VFS: revert to using asByteSource
commit 59f17d6e0550bf63a0b6ef182e2d63474e058ede updated files to use try-with-resources when dealing with streams. The change also got rid of asByteSource, replacing it with throw-away ByteSource instances wrapping a try-with-resources-guarded InputStream. Doing so was unnecessary though, because all ByteSource methods (except for open[Buffered]InputStream) close the stream. Thanks to @jbduncan to point that out and explain in detail [1]. [1] see comment thread on https://github.com/bazelbuild/bazel/commit/59f17d6e0550bf63a0b6ef182e2d63474e058ede under `FilesetManifest.java` RELNOTES: none PiperOrigin-RevId: 203934830
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/exec/FilesetManifest.java12
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java14
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java44
3 files changed, 17 insertions, 53 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/exec/FilesetManifest.java b/src/main/java/com/google/devtools/build/lib/exec/FilesetManifest.java
index 2a54103fde..c48674a913 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/FilesetManifest.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/FilesetManifest.java
@@ -15,14 +15,13 @@ package com.google.devtools.build.lib.exec;
import static java.nio.charset.StandardCharsets.UTF_8;
-import com.google.common.io.ByteSource;
import com.google.common.io.LineProcessor;
import com.google.devtools.build.lib.actions.FilesetOutputSymlink;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.IOException;
-import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -60,13 +59,8 @@ public final class FilesetManifest {
RelativeSymlinkBehavior relSymlinkBehavior)
throws IOException {
Path file = execRoot.getRelative(AnalysisUtils.getManifestPathFromFilesetPath(manifest));
- try (InputStream in = file.getInputStream()) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.asCharSource(UTF_8)
+ try {
+ return FileSystemUtils.asByteSource(file).asCharSource(UTF_8)
.readLines(
new ManifestLineProcessor(workspaceName, manifest, relSymlinkBehavior));
} catch (IllegalStateException e) {
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 01c016905f..8258dccfc0 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
@@ -268,14 +268,12 @@ public abstract class FileSystem {
* @throws IOException if the digest could not be computed for any reason
*/
protected byte[] getDigest(final Path path, DigestHashFunction hashFunction) throws IOException {
- try (InputStream in = getInputStream(path)) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.hash(hashFunction.getHash()).asBytes();
- }
+ return new ByteSource() {
+ @Override
+ public InputStream openStream() throws IOException {
+ return getInputStream(path);
+ }
+ }.hash(hashFunction.getHash()).asBytes();
}
/**
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
index 9883030cb6..4d1e9c6533 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
@@ -852,14 +852,7 @@ public class FileSystemUtils {
* @throws IOException if there was an error
*/
public static Iterable<String> readLines(Path inputFile, Charset charset) throws IOException {
- try (InputStream in = inputFile.getInputStream()) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.asCharSource(charset).readLines();
- }
+ return asByteSource(inputFile).asCharSource(charset).readLines();
}
/**
@@ -868,28 +861,14 @@ public class FileSystemUtils {
* @throws IOException if there was an error
*/
public static byte[] readContent(Path inputFile) throws IOException {
- try (InputStream in = inputFile.getInputStream()) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.read();
- }
+ return asByteSource(inputFile).read();
}
/**
* Reads the entire file using the given charset and returns the contents as a string
*/
public static String readContent(Path inputFile, Charset charset) throws IOException {
- try (InputStream in = inputFile.getInputStream()) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.asCharSource(charset).read();
- }
+ return asByteSource(inputFile).asCharSource(charset).read();
}
/**
@@ -899,18 +878,11 @@ public class FileSystemUtils {
*/
public static byte[] readContentWithLimit(Path inputFile, int limit) throws IOException {
Preconditions.checkArgument(limit >= 0, "limit needs to be >=0, but it is %s", limit);
- try (InputStream in = inputFile.getInputStream()) {
- byte[] buffer = new byte[limit];
- try (InputStream inputStream =
- new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return in;
- }
- }.openBufferedStream()) {
- int read = ByteStreams.read(inputStream, buffer, 0, limit);
- return read == limit ? buffer : Arrays.copyOf(buffer, read);
- }
+ ByteSource byteSource = asByteSource(inputFile);
+ byte[] buffer = new byte[limit];
+ try (InputStream inputStream = byteSource.openBufferedStream()) {
+ int read = ByteStreams.read(inputStream, buffer, 0, limit);
+ return read == limit ? buffer : Arrays.copyOf(buffer, read);
}
}