diff options
4 files changed, 19 insertions, 65 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); } } diff --git a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java index a1d7acbe29..89327e3f26 100644 --- a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java +++ b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java @@ -20,13 +20,12 @@ import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; -import com.google.common.io.ByteSource; import com.google.devtools.build.lib.skylark.util.SkylarkTestCase; import com.google.devtools.build.lib.syntax.ParserInputSource; +import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.skydoc.rendering.RuleInfo; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Paths; import java.util.Map; import java.util.Map.Entry; @@ -51,16 +50,7 @@ public final class SkydocTest extends SkylarkTestCase { @Override public ParserInputSource inputSource(String pathString) throws IOException { Path path = fileSystem.getPath(pathString); - byte[] bytes = null; - try (InputStream in = path.getInputStream()) { - bytes = new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return in; - } - }.read(); - } - + byte[] bytes = FileSystemUtils.asByteSource(path).read(); return ParserInputSource.create(bytes, path.asFragment()); } }); |