diff options
Diffstat (limited to 'src')
15 files changed, 130 insertions, 75 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/LauncherFileWriteAction.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/LauncherFileWriteAction.java index ed1cdb17ae..d02c9cb4bb 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/actions/LauncherFileWriteAction.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/LauncherFileWriteAction.java @@ -75,8 +75,9 @@ public final class LauncherFileWriteAction extends AbstractFileWriteAction { // single-machine execution environment, but problematic with remote execution. Preconditions.checkState(OS.getCurrent() == OS.WINDOWS); return out -> { - InputStream in = ctx.getInputPath(this.launcher).getInputStream(); - ByteStreams.copy(in, out); + try (InputStream in = ctx.getInputPath(this.launcher).getInputStream()) { + ByteStreams.copy(in, out); + } long dataLength = this.launchInfo.write(out); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.order(ByteOrder.LITTLE_ENDIAN); // All Windows versions are little endian. diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java index ede9cd2620..46c42b606b 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java @@ -46,6 +46,7 @@ import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; import com.google.devtools.build.skyframe.ValueOrException; import java.io.IOException; +import java.io.InputStream; import java.util.Map; import java.util.Properties; @@ -311,9 +312,10 @@ public class AndroidSdkRepositoryFunction extends AndroidRepositoryFunction { env.getValueOrThrow(releaseFileKey, IOException.class); Properties properties = new Properties(); - properties.load(sourcePropertiesFilePath.getInputStream()); + try (InputStream in = sourcePropertiesFilePath.getInputStream()) { + properties.load(in); + } return properties; - } catch (IOException e) { String error = String.format( "Could not read %s in Android SDK: %s", sourcePropertiesFilePath, e.getMessage()); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/SdkMavenRepository.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/SdkMavenRepository.java index 1efca1efde..97ebf914f8 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/SdkMavenRepository.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/SdkMavenRepository.java @@ -21,6 +21,7 @@ import com.google.common.collect.Ordering; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; @@ -199,8 +200,10 @@ final class SdkMavenRepository { private static final String DEFAULT_PACKAGING = "jar"; static Pom parse(Path path) throws IOException, ParserConfigurationException, SAXException { - Document pomDocument = - DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path.getInputStream()); + Document pomDocument = null; + try (InputStream in = path.getInputStream()) { + pomDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); + } Node packagingNode = pomDocument.getElementsByTagName("packaging").item(0); String packaging = packagingNode == null ? DEFAULT_PACKAGING : packagingNode.getTextContent(); MavenCoordinate coordinate = MavenCoordinate.create( 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 c48674a913..2a54103fde 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,13 +15,14 @@ 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; @@ -59,8 +60,13 @@ public final class FilesetManifest { RelativeSymlinkBehavior relSymlinkBehavior) throws IOException { Path file = execRoot.getRelative(AnalysisUtils.getManifestPathFromFilesetPath(manifest)); - try { - return FileSystemUtils.asByteSource(file).asCharSource(UTF_8) + try (InputStream in = file.getInputStream()) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.asCharSource(UTF_8) .readLines( new ManifestLineProcessor(workspaceName, manifest, relSymlinkBehavior)); } catch (IllegalStateException e) { diff --git a/src/main/java/com/google/devtools/build/lib/exec/TestLogHelper.java b/src/main/java/com/google/devtools/build/lib/exec/TestLogHelper.java index 65d1d61d15..409c10d903 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/TestLogHelper.java +++ b/src/main/java/com/google/devtools/build/lib/exec/TestLogHelper.java @@ -48,7 +48,6 @@ public class TestLogHelper { */ public static void writeTestLog(Path testOutput, String testName, OutputStream out) throws IOException { - InputStream input = null; PrintStream printOut = new PrintStream(new BufferedOutputStream(out)); try { final String outputHeader = "==================== Test output for " + testName + ":"; @@ -58,9 +57,10 @@ public class TestLogHelper { printOut.println(outputHeader); printOut.flush(); - input = testOutput.getInputStream(); FilterTestHeaderOutputStream filteringOutputStream = getHeaderFilteringOutputStream(printOut); - ByteStreams.copy(input, filteringOutputStream); + try (InputStream input = testOutput.getInputStream()) { + ByteStreams.copy(input, filteringOutputStream); + } if (!filteringOutputStream.foundHeader()) { try (InputStream inputAgain = testOutput.getInputStream()) { @@ -71,9 +71,6 @@ public class TestLogHelper { printOut.println(outputFooter); } finally { printOut.flush(); - if (input != null) { - input.close(); - } } } diff --git a/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java index 26a24b1e85..2088dabf5b 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java @@ -20,7 +20,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.ByteStreams; -import com.google.common.io.Closeables; import com.google.devtools.build.lib.actions.ActionExecutionContext; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.CommandLineExpansionException; @@ -418,21 +417,18 @@ public abstract class TestStrategy implements TestActionContext { /** In rare cases, we might write something to stderr. Append it to the real test.log. */ protected static void appendStderr(Path stdOut, Path stdErr) throws IOException { FileStatus stat = stdErr.statNullable(); - OutputStream out = null; - InputStream in = null; if (stat != null) { try { if (stat.getSize() > 0) { if (stdOut.exists()) { stdOut.setWritable(true); } - out = stdOut.getOutputStream(true); - in = stdErr.getInputStream(); - ByteStreams.copy(in, out); + try (OutputStream out = stdOut.getOutputStream(true); + InputStream in = stdErr.getInputStream()) { + ByteStreams.copy(in, out); + } } } finally { - Closeables.close(out, true); - Closeables.close(in, true); stdErr.delete(); } } 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 8258dccfc0..01c016905f 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,12 +268,14 @@ 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 { - return new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return getInputStream(path); - } - }.hash(hashFunction.getHash()).asBytes(); + try (InputStream in = getInputStream(path)) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.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 999c470c8b..e2ab1d8002 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 @@ -400,7 +400,10 @@ public class FileSystemUtils { throw new IOException("error copying file: " + "couldn't delete destination: " + e.getMessage()); } - asByteSource(from).copyTo(asByteSink(to)); + try (InputStream in = from.getInputStream(); + OutputStream out = to.getOutputStream()) { + ByteStreams.copy(in, out); + } to.setLastModifiedTime(from.getLastModifiedTime()); // Preserve mtime. if (!from.isWritable()) { to.setWritable(false); // Make file read-only if original was read-only. @@ -427,7 +430,10 @@ public class FileSystemUtils { // Fallback to a copy. FileStatus stat = from.stat(Symlinks.NOFOLLOW); if (stat.isFile()) { - asByteSource(from).copyTo(asByteSink(to)); + try (InputStream in = from.getInputStream(); + OutputStream out = to.getOutputStream()) { + ByteStreams.copy(in, out); + } to.setLastModifiedTime(stat.getLastModifiedTime()); // Preserve mtime. if (!from.isWritable()) { to.setWritable(false); // Make file read-only if original was read-only. @@ -817,7 +823,14 @@ public class FileSystemUtils { * @throws IOException if there was an error */ public static Iterable<String> readLines(Path inputFile, Charset charset) throws IOException { - return asByteSource(inputFile).asCharSource(charset).readLines(); + try (InputStream in = inputFile.getInputStream()) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.asCharSource(charset).readLines(); + } } /** @@ -826,14 +839,28 @@ public class FileSystemUtils { * @throws IOException if there was an error */ public static byte[] readContent(Path inputFile) throws IOException { - return asByteSource(inputFile).read(); + try (InputStream in = inputFile.getInputStream()) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.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 { - return asByteSource(inputFile).asCharSource(charset).read(); + try (InputStream in = inputFile.getInputStream()) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.asCharSource(charset).read(); + } } /** @@ -843,11 +870,18 @@ 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); - 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); + 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); + } } } diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContextTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContextTest.java index a4907abfac..d22c376d9d 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContextTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContextTest.java @@ -182,10 +182,10 @@ public class SkylarkRepositoryContextTest { private void testOutputFile(Path path, String content) throws IOException { assertThat(path.exists()).isTrue(); - assertThat( - CharStreams.toString( - new InputStreamReader(path.getInputStream(), StandardCharsets.UTF_8))) - .isEqualTo(content); + try (InputStreamReader reader = + new InputStreamReader(path.getInputStream(), StandardCharsets.UTF_8)) { + assertThat(CharStreams.toString(reader)).isEqualTo(content); + } } @Test diff --git a/src/test/java/com/google/devtools/build/lib/remote/GrpcRemoteCacheTest.java b/src/test/java/com/google/devtools/build/lib/remote/GrpcRemoteCacheTest.java index 29bfeb24fb..4fc5869276 100644 --- a/src/test/java/com/google/devtools/build/lib/remote/GrpcRemoteCacheTest.java +++ b/src/test/java/com/google/devtools/build/lib/remote/GrpcRemoteCacheTest.java @@ -74,6 +74,7 @@ import io.grpc.inprocess.InProcessServerBuilder; import io.grpc.stub.StreamObserver; import io.grpc.util.MutableHandlerRegistry; import java.io.IOException; +import java.io.InputStream; import java.util.concurrent.Executors; import org.junit.After; import org.junit.AfterClass; @@ -176,10 +177,10 @@ public class GrpcRemoteCacheTest { Scratch scratch = new Scratch(); scratch.file(authTlsOptions.googleCredentials, new JacksonFactory().toString(json)); - CallCredentials creds = - GoogleAuthUtils.newCallCredentials( - scratch.resolve(authTlsOptions.googleCredentials).getInputStream(), - authTlsOptions.googleAuthScopes); + CallCredentials creds = null; + try (InputStream in = scratch.resolve(authTlsOptions.googleCredentials).getInputStream()) { + GoogleAuthUtils.newCallCredentials(in, authTlsOptions.googleAuthScopes); + } RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class); RemoteRetrier retrier = new RemoteRetrier( diff --git a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java index 689e50f24d..e5bf9ea8e9 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java @@ -22,6 +22,7 @@ import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Collection; @@ -133,9 +134,9 @@ public final class Scratch { } public String readFile(String pathName) throws IOException { - return new String( - ByteStreams.toByteArray(resolve(pathName).getInputStream()), - DEFAULT_CHARSET); + try (InputStream in = resolve(pathName).getInputStream()) { + return new String(ByteStreams.toByteArray(in), DEFAULT_CHARSET); + } } /** Like {@code scratch.file}, but the lines are added to the end if the file already exists. */ diff --git a/src/test/java/com/google/devtools/build/lib/vfs/NativePathTest.java b/src/test/java/com/google/devtools/build/lib/vfs/NativePathTest.java index 5dc43a2567..42f34c30ae 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/NativePathTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/NativePathTest.java @@ -222,16 +222,16 @@ public class NativePathTest { @Test public void testInputOutputStreams() throws IOException { Path path = fs.getPath(aFile.getPath()); - OutputStream out = path.getOutputStream(); - for (int i = 0; i < 256; i++) { - out.write(i); + try (OutputStream out = path.getOutputStream()) { + for (int i = 0; i < 256; i++) { + out.write(i); + } } - out.close(); - InputStream in = path.getInputStream(); - for (int i = 0; i < 256; i++) { - assertThat(in.read()).isEqualTo(i); + try (InputStream in = path.getInputStream()) { + for (int i = 0; i < 256; i++) { + assertThat(in.read()).isEqualTo(i); + } + assertThat(in.read()).isEqualTo(-1); } - assertThat(in.read()).isEqualTo(-1); - in.close(); } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java index 3799cfdd46..b34c0c0412 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java @@ -201,9 +201,9 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { // Create an "/in" directory directly on the output delegate to bypass the // UnionFileSystem's mapping. assertThat(inDelegate.getPath("/in").createDirectory()).isTrue(); - OutputStream outStream = inDelegate.getPath("/in/bar.txt").getOutputStream(); - outStream.write('i'); - outStream.close(); + try (OutputStream outStream = inDelegate.getPath("/in/bar.txt").getOutputStream()) { + outStream.write('i'); + } Path outFoo = unionfs.getPath("/out/foo"); unionfs.createSymbolicLink(outFoo, PathFragment.create("../in/bar.txt")); @@ -218,9 +218,10 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { Path resolved = unionfs.resolveSymbolicLinks(outFoo); assertThat(resolved.getFileSystem()).isSameAs(unionfs); - InputStream barInput = resolved.getInputStream(); - int barChar = barInput.read(); - barInput.close(); + int barChar = -1; + try (InputStream barInput = resolved.getInputStream()) { + barChar = barInput.read(); + } assertThat(barChar).isEqualTo('i'); } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystemTest.java index 30f4064b60..1989e9bb34 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystemTest.java @@ -155,10 +155,11 @@ public class InMemoryFileSystemTest extends SymlinkAwareFileSystemTest { assertThat(file.isWritable()).isFalse(); assertThat(file.isExecutable()).isFalse(); assertThat(file.getLastModifiedTime()).isEqualTo(300); - BufferedReader reader = new BufferedReader( - new InputStreamReader(file.getInputStream(), Charset.defaultCharset())); - assertThat(reader.readLine()).isEqualTo(TEST_FILE_DATA); - assertThat(reader.readLine()).isNull(); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(file.getInputStream(), Charset.defaultCharset()))) { + assertThat(reader.readLine()).isEqualTo(TEST_FILE_DATA); + assertThat(reader.readLine()).isNull(); + } Path symlink = base.getRelative("symlink" + i); assertThat(symlink.exists()).isTrue(); @@ -239,10 +240,11 @@ public class InMemoryFileSystemTest extends SymlinkAwareFileSystemTest { assertThat(file.isExecutable()).isEqualTo(i % 4 == 0); assertThat(file.getLastModifiedTime()).isEqualTo(i); if (file.isReadable()) { - BufferedReader reader = new BufferedReader( - new InputStreamReader(file.getInputStream(), Charset.defaultCharset())); - assertThat(reader.readLine()).isEqualTo(TEST_FILE_DATA); - assertThat(reader.readLine()).isNull(); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(file.getInputStream(), Charset.defaultCharset()))) { + assertThat(reader.readLine()).isEqualTo(TEST_FILE_DATA); + assertThat(reader.readLine()).isNull(); + } } Path symlink = base.getRelative("symlink_" + threadId + "_" + i); 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 0f0d79ffb6..870fa67e52 100644 --- a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java +++ b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java @@ -20,12 +20,13 @@ 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; @@ -50,7 +51,15 @@ public final class SkydocTest extends SkylarkTestCase { @Override public ParserInputSource inputSource(String pathString) throws IOException { Path path = fileSystem.getPath(pathString); - byte[] bytes = FileSystemUtils.asByteSource(path).read(); + byte[] bytes = null; + try (InputStream in = path.getInputStream()) { + bytes = new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.read(); + } return ParserInputSource.create(bytes, path.asFragment()); } |