diff options
author | laszlocsomor <laszlocsomor@google.com> | 2018-07-10 06:14:49 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-10 06:15:52 -0700 |
commit | dda5bcedea18009a26adbb2dd01c82a7edfe9917 (patch) | |
tree | c86c1e0c8588d133eb2d1a77597da0df0b5bd5bd /src/main/java/com/google/devtools/build/lib/vfs | |
parent | 9c883bf10568b818c7b5f8e80b19ba305e49206e (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/build/lib/vfs')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java | 14 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java | 44 |
2 files changed, 14 insertions, 44 deletions
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); } } |