From cca887e1e7e55c9c04318060014ce10d17643d52 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 13 Jan 2017 01:58:04 +0000 Subject: Support Paths in JarCreater and JarHelper Instead of passing strings representing paths in the default filesystem. -- PiperOrigin-RevId: 144395118 MOS_MIGRATED_REVID=144395118 --- .../build/buildjar/jarhelper/JarCreator.java | 30 ++++++++++++++++------ .../build/buildjar/jarhelper/JarHelper.java | 21 +++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) (limited to 'src') 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 5f081f7dff..90ecf2cc14 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,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collection; import java.util.Map; import java.util.TreeMap; @@ -35,7 +37,7 @@ public class JarCreator extends JarHelper { // Map from Jar entry names to files. Use TreeMap so we can establish a canonical order for the // entries regardless in what order they get added. - private final Map jarEntries = new TreeMap<>(); + private final TreeMap jarEntries = new TreeMap<>(); private String manifestFile; private String mainClass; @@ -47,16 +49,27 @@ public class JarCreator extends JarHelper { * Adds an entry to the Jar file, normalizing the name. * * @param entryName the name of the entry in the Jar file - * @param fileName the name of the input file for the entry + * @param path the path of the input for the entry * @return true iff a new entry was added */ - public boolean addEntry(String entryName, String fileName) { + public boolean addEntry(String entryName, Path path) { if (entryName.startsWith("/")) { entryName = entryName.substring(1); } else if (entryName.startsWith("./")) { entryName = entryName.substring(2); } - return jarEntries.put(entryName, fileName) == null; + return jarEntries.put(entryName, path) == null; + } + + /** + * Adds an entry to the Jar file, normalizing the name. + * + * @param entryName the name of the entry in the Jar file + * @param fileName the name of the input file for the entry + * @return true iff a new entry was added + */ + public boolean addEntry(String entryName, String fileName) { + return addEntry(entryName, Paths.get(fileName)); } /** @@ -82,7 +95,7 @@ public class JarCreator extends JarHelper { if (files != null) { for (File file : files) { String entryName = prefix != null ? prefix + "/" + file.getName() : file.getName(); - jarEntries.put(entryName, file.getAbsolutePath()); + jarEntries.put(entryName, file.toPath()); if (file.isDirectory()) { addDirectory(entryName, file); } @@ -100,7 +113,8 @@ public class JarCreator extends JarHelper { */ public void addRootEntries(Collection entries) { for (String entry : entries) { - jarEntries.put(new File(entry).getName(), entry); + Path path = Paths.get(entry); + jarEntries.put(path.getFileName().toString(), path); } } @@ -157,8 +171,8 @@ public class JarCreator extends JarHelper { // Create the manifest entry in the Jar file writeManifestEntry(manifestContent()); try { - for (Map.Entry entry : jarEntries.entrySet()) { - copyEntry(entry.getKey(), new File(entry.getValue())); + for (Map.Entry entry : jarEntries.entrySet()) { + copyEntry(entry.getKey(), entry.getValue()); } } finally { out.closeEntry(); 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 48525fc738..5e7d0227ea 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 @@ -14,10 +14,10 @@ package com.google.devtools.build.buildjar.jarhelper; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashSet; import java.util.Set; import java.util.jar.JarEntry; @@ -171,23 +171,24 @@ 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, File file) throws IOException { + protected void copyEntry(String name, Path path) throws IOException { if (!names.contains(name)) { - if (!file.exists()) { - throw new FileNotFoundException(file.getAbsolutePath() + " (No such file or directory)"); + if (!Files.exists(path)) { + throw new FileNotFoundException(path.toAbsolutePath() + " (No such file or directory)"); } - boolean isDirectory = file.isDirectory(); + boolean isDirectory = Files.isDirectory(path); if (isDirectory && !name.endsWith("/")) { name = name + '/'; // always normalize directory names before checking set } if (names.add(name)) { if (verbose) { - System.err.println("adding " + file); + System.err.println("adding " + path); } // Create a new entry - long size = isDirectory ? 0 : file.length(); + long size = isDirectory ? 0 : Files.size(path); JarEntry outEntry = new JarEntry(name); - long newtime = normalize ? normalizedTimestamp(name) : file.lastModified(); + long newtime = + normalize ? normalizedTimestamp(name) : Files.getLastModifiedTime(path).toMillis(); outEntry.setTime(newtime); outEntry.setSize(size); if (size == 0L) { @@ -201,7 +202,7 @@ public class JarHelper { // It would be nicer to do this via DigestInputStream, but // the architecture of ZipOutputStream requires us to know the CRC-32 // before we write the data to the stream. - byte[] bytes = Files.readAllBytes(file.toPath()); + byte[] bytes = Files.readAllBytes(path); CRC32 crc = new CRC32(); crc.update(bytes); outEntry.setCrc(crc.getValue()); @@ -209,7 +210,7 @@ public class JarHelper { out.write(bytes); } else { out.putNextEntry(outEntry); - Files.copy(file.toPath(), out); + Files.copy(path, out); } } out.closeEntry(); -- cgit v1.2.3