diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2017-11-16 02:14:20 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-16 02:16:16 -0800 |
commit | 93332281ed71a58e30db25b1fb906c014c022c31 (patch) | |
tree | a5d3b827dc09ea16d64bd668edc9af317409cb62 | |
parent | 85e4896db911f77f1223f080c540b8cbf133f873 (diff) |
buildjar: close streams
Use try-with-resources to close all streams.
I hope this will fix the spurious file deletion
failures on Windows, where the persistent
JavaBuilder worker cannot delete outputs from
previous builds because, supposedly, they are
still open, because a stale object that's ready
to be GC'd hasn't yet been actually GC'd.
Change-Id: Ia57b8bd0ba1b6ee0691d34467c92e86e35d4d71d
PiperOrigin-RevId: 175941520
3 files changed, 11 insertions, 5 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass/GenClass.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass/GenClass.java index 1aed0f9779..4bc1f922fe 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass/GenClass.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass/GenClass.java @@ -122,6 +122,9 @@ public class GenClass { String className = name.substring(0, name.length() - ".class".length()); if (prefixesContains(generatedPrefixes, className)) { Files.createDirectories(tempDir.resolve(name).getParent()); + // InputStream closing: JarFile extends ZipFile, and ZipFile.close() will close all of the + // input streams previously returned by invocations of the getInputStream method. + // See https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipFile.html#close-- Files.copy(jar.getInputStream(entry), tempDir.resolve(name)); } } diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarCreator.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarCreator.java index 46232bc879..eb25a23e4d 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarCreator.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarCreator.java @@ -147,13 +147,16 @@ public class JarCreator extends JarHelper { } private byte[] manifestContent() throws IOException { - Manifest manifest; if (manifestFile != null) { - FileInputStream in = new FileInputStream(manifestFile); - manifest = new Manifest(in); + try (FileInputStream in = new FileInputStream(manifestFile)) { + return manifestContentImpl(new Manifest(in)); + } } else { - manifest = new Manifest(); + return manifestContentImpl(new Manifest()); } + } + + private byte[] manifestContentImpl(Manifest manifest) throws IOException { Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); Attributes.Name createdBy = new Attributes.Name("Created-By"); diff --git a/third_party/ijar/test/ijar_test.sh b/third_party/ijar/test/ijar_test.sh index 5279f15f84..0b9a92fb2d 100755 --- a/third_party/ijar/test/ijar_test.sh +++ b/third_party/ijar/test/ijar_test.sh @@ -41,7 +41,7 @@ shift source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; } function cleanup() { - rm -fr "${TEST_TMPDIR:-sentinel}"/* + rm -fr "$TEST_TMPDIR"/* } trap cleanup EXIT |