diff options
3 files changed, 48 insertions, 44 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java index eb03a9bae7..68b0f1015b 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java @@ -139,10 +139,13 @@ public class SimpleJavaLibraryBuilder implements Closeable { public void buildGensrcJar(JavaLibraryBuildRequest build) throws IOException { JarCreator jar = new JarCreator(build.getGeneratedSourcesOutputJar()); - jar.setNormalize(true); - jar.setCompression(build.compressJar()); - jar.addDirectory(build.getSourceGenDir()); - jar.execute(); + try { + jar.setNormalize(true); + jar.setCompression(build.compressJar()); + jar.addDirectory(build.getSourceGenDir()); + } finally { + jar.execute(); + } } /** @@ -193,41 +196,43 @@ public class SimpleJavaLibraryBuilder implements Closeable { public void buildJar(JavaLibraryBuildRequest build) throws IOException { JarCreator jar = new JarCreator(build.getOutputJar()); - jar.setNormalize(true); - jar.setCompression(build.compressJar()); + try { + jar.setNormalize(true); + jar.setCompression(build.compressJar()); - for (String resourceJar : build.getResourceJars()) { - for (Path root : getJarFileSystem(Paths.get(resourceJar)).getRootDirectories()) { - Files.walkFileTree( - root, - new SimpleFileVisitor<Path>() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) - throws IOException { - // TODO(b/28452451): omit directories entries from jar files - if (dir.getNameCount() > 0) { - jar.addEntry(root.relativize(dir).toString(), dir); + for (String resourceJar : build.getResourceJars()) { + for (Path root : getJarFileSystem(Paths.get(resourceJar)).getRootDirectories()) { + Files.walkFileTree( + root, + new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) + throws IOException { + // TODO(b/28452451): omit directories entries from jar files + if (dir.getNameCount() > 0) { + jar.addEntry(root.relativize(dir).toString(), dir); + } + return FileVisitResult.CONTINUE; } - return FileVisitResult.CONTINUE; - } - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) - throws IOException { - jar.addEntry(root.relativize(path).toString(), path); - return FileVisitResult.CONTINUE; - } - }); + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) + throws IOException { + jar.addEntry(root.relativize(path).toString(), path); + return FileVisitResult.CONTINUE; + } + }); + } } - } - - jar.addDirectory(build.getClassDir()); - jar.addRootEntries(build.getRootResourceFiles()); - addResourceEntries(jar, build.getResourceFiles()); - addMessageEntries(jar, build.getMessageFiles()); + jar.addDirectory(build.getClassDir()); - jar.execute(); + jar.addRootEntries(build.getRootResourceFiles()); + addResourceEntries(jar, build.getResourceFiles()); + addMessageEntries(jar, build.getMessageFiles()); + } finally { + jar.execute(); + } } /** 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 c3a5d84182..46232bc879 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 @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collection; @@ -173,17 +174,16 @@ public class JarCreator extends JarHelper { * @throws IOException if the Jar cannot be written or any of the entries cannot be read. */ public void execute() throws IOException { - out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile))); + try (OutputStream os = new FileOutputStream(jarFile); + BufferedOutputStream bos = new BufferedOutputStream(os); + JarOutputStream out = new JarOutputStream(bos)) { + + // Create the manifest entry in the Jar file + writeManifestEntry(out, manifestContent()); - // Create the manifest entry in the Jar file - writeManifestEntry(manifestContent()); - try { for (Map.Entry<String, Path> entry : jarEntries.entrySet()) { - copyEntry(entry.getKey(), entry.getValue()); + copyEntry(out, entry.getKey(), entry.getValue()); } - } finally { - out.closeEntry(); - out.close(); } } diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarHelper.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarHelper.java index 5e7d0227ea..ff4b8e055f 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarHelper.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper/JarHelper.java @@ -54,7 +54,6 @@ public class JarHelper { // The state needed to create the Jar protected final Set<String> names = new HashSet<>(); - protected JarOutputStream out; public JarHelper(String filename) { jarFile = filename; @@ -152,7 +151,7 @@ public class JarHelper { * @param content the Manifest content to write to the manifest entry. * @throws IOException */ - protected void writeManifestEntry(byte[] content) throws IOException { + protected void writeManifestEntry(JarOutputStream out, byte[] content) throws IOException { int oldStorageMethod = storageMethod; // Do not compress small manifest files, the compressed one is frequently // larger than the original. The threshold of 256 bytes is somewhat arbitrary. @@ -171,7 +170,7 @@ public class JarHelper { * Copies file or directory entries from the file system into the jar. Directory entries will be * detected and their names automatically '/' suffixed. */ - protected void copyEntry(String name, Path path) throws IOException { + protected void copyEntry(JarOutputStream out, String name, Path path) throws IOException { if (!names.contains(name)) { if (!Files.exists(path)) { throw new FileNotFoundException(path.toAbsolutePath() + " (No such file or directory)"); |