package sun.tools.jar;
import java.io.*;
import java.lang.module.Configuration;
import java.lang.module.FindException;
import java.lang.module.InvalidModuleDescriptorException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Exports;
import java.lang.module.ModuleDescriptor.Opens;
import java.lang.module.ModuleDescriptor.Provides;
import java.lang.module.ModuleDescriptor.Version;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.MessageFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import jdk.internal.module.Checks;
import jdk.internal.module.ModuleHashes;
import jdk.internal.module.ModuleHashesBuilder;
import jdk.internal.module.ModuleInfo;
import jdk.internal.module.ModuleInfoExtender;
import jdk.internal.module.ModuleResolution;
import jdk.internal.module.ModuleTarget;
import jdk.internal.util.jar.JarIndex;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.jar.JarFile.MANIFEST_NAME;
import static java.util.stream.Collectors.joining;
import static jdk.internal.util.jar.JarIndex.INDEX_NAME;
public class Main {
String program;
PrintWriter out, err;
String fname, mname, ename;
String zname = "";
String rootjar = null;
private static final int BASE_VERSION = 0;
private static class Entry {
final String name;
final File file;
final boolean isDir;
Entry(File file, String name, boolean isDir) {
this.file = file;
this.isDir = isDir;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Entry)) return false;
return this.file.equals(((Entry)o).file);
}
@Override
public int hashCode() {
return file.hashCode();
}
}
Map<String, Entry> entryMap = new HashMap<>();
Set<Entry> entries = new LinkedHashSet<>();
Map<String,byte[]> moduleInfos = new HashMap<>();
Map<Integer,Set<String>> pathsMap = new HashMap<>();
Map<Integer,String[]> filesMap = new HashMap<>();
boolean isMultiRelease;
int releaseValue = -1;
boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, nflag, pflag, dflag;
boolean suppressDeprecateMsg = false;
Consumer<PrintWriter> info;
Version moduleVersion;
Pattern modulesToHash;
ModuleResolution moduleResolution = ModuleResolution.empty();
ModuleFinder moduleFinder = ModuleFinder.of();
static final String MODULE_INFO = "module-info.class";
static final String MANIFEST_DIR = "META-INF/";
static final String VERSIONS_DIR = MANIFEST_DIR + "versions/";
static final String VERSION = "1.0";
static final int VERSIONS_DIR_LENGTH = VERSIONS_DIR.length();
private static ResourceBundle rsrc;
private static final boolean useExtractionTime =
Boolean.getBoolean("sun.tools.jar.useExtractionTime");
static {
try {
rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");
} catch (MissingResourceException e) {
throw new Error("Fatal: Resource for jar is missing");
}
}
static String getMsg(String key) {
try {
return (rsrc.getString(key));
} catch (MissingResourceException e) {
throw new Error("Error in message file");
}
}
static String formatMsg(String key, String arg) {
String msg = getMsg(key);
String[] args = new String[1];
args[0] = arg;
return MessageFormat.format(msg, (Object[]) args);
}
static String formatMsg2(String key, String arg, String arg1) {
String msg = getMsg(key);
String[] args = new String[2];
args[0] = arg;
args[1] = arg1;
return MessageFormat.format(msg, (Object[]) args);
}
public Main(PrintStream out, PrintStream err, String program) {
this.out = new PrintWriter(out, true);
this.err = new PrintWriter(err, true);
this.program = program;
}
public Main(PrintWriter out, PrintWriter err, String program) {
this.out = out;
this.err = err;
this.program = program;
}
private static File createTempFileInSameDirectoryAs(File file)
throws IOException {
File dir = file.getParentFile();
if (dir == null)
dir = new File(".");
return File.createTempFile("jartmp", null, dir);
}
private boolean ok;
@SuppressWarnings({"removal"})
public synchronized boolean run(String args[]) {
ok = true;
if (!parseArgs(args)) {
return false;
}
File tmpFile = null;
try {
if (cflag || uflag) {
if (fname != null) {
zname = fname.replace(File.separatorChar, '/');
if (zname.startsWith("./")) {
zname = zname.substring(2);
}
}
}
if (cflag) {
Manifest manifest = null;
if (!Mflag) {
if (mname != null) {
try (InputStream in = new FileInputStream(mname)) {
manifest = new Manifest(new BufferedInputStream(in));
}
} else {
manifest = new Manifest();
}
addVersion(manifest);
addCreatedBy(manifest);
if (isAmbiguousMainClass(manifest)) {
return false;
}
if (ename != null) {
addMainClass(manifest, ename);
}
if (isMultiRelease) {
addMultiRelease(manifest);
}
}
expand();
if (!moduleInfos.isEmpty()) {
Set<String> jentries = new HashSet<>();
Set<String> packages = new HashSet<>();
entries.stream()
.filter(e -> !e.isDir)
.forEach( e -> {
addPackageIfNamed(packages, e.name);
jentries.add(e.name);
});
addExtendedModuleAttributes(moduleInfos, packages);
if (!checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries))
return false;
} else if (moduleVersion != null || modulesToHash != null) {
error(getMsg("error.module.options.without.info"));
return false;
}
if (vflag && fname == null) {
vflag = false;
}
final String tmpbase = (fname == null)
? "tmpjar"
: fname.substring(fname.indexOf(File.separatorChar) + 1);
tmpFile = createTemporaryFile(tmpbase, ".jar");
try (OutputStream out = new FileOutputStream(tmpFile)) {
create(new BufferedOutputStream(out, 4096), manifest);
}
if (nflag) {
if (!suppressDeprecateMsg) {
warn(formatMsg("warn.flag.is.deprecated", "-n"));
}
File packFile = createTemporaryFile(tmpbase, ".pack");
try {
java.util.jar.Pack200.Packer packer = java.util.jar.Pack200.newPacker();
Map<String, String> p = packer.properties();
p.put(java.util.jar.Pack200.Packer.EFFORT, "1");
try (JarFile jarFile = new JarFile(tmpFile.getCanonicalPath());
OutputStream pack = new FileOutputStream(packFile))
{
packer.pack(jarFile, pack);
}
if (tmpFile.exists()) {
tmpFile.delete();
}
tmpFile = createTemporaryFile(tmpbase, ".jar");
try (OutputStream out = new FileOutputStream(tmpFile);
JarOutputStream jos = new JarOutputStream(out))
{
java.util.jar.Pack200.Unpacker unpacker = java.util.jar.Pack200.newUnpacker();
unpacker.unpack(packFile, jos);
}
} finally {
Files.deleteIfExists(packFile.toPath());
}
}
validateAndClose(tmpFile);
} else if (uflag) {
File inputFile = null;
if (fname != null) {
inputFile = new File(fname);
tmpFile = createTempFileInSameDirectoryAs(inputFile);
} else {
vflag = false;
tmpFile = createTemporaryFile("tmpjar", ".jar");
}
expand();
try (FileInputStream in = (fname != null) ? new FileInputStream(inputFile)
: new FileInputStream(FileDescriptor.in);
FileOutputStream out = new FileOutputStream(tmpFile);
InputStream manifest = (!Mflag && (mname != null)) ?
(new FileInputStream(mname)) : null;
) {
boolean updateOk = update(in, new BufferedOutputStream(out),
manifest, moduleInfos, null);
if (ok) {
ok = updateOk;
}
}
validateAndClose(tmpFile);
} else if (tflag) {
replaceFSC(filesMap);
String[] files = filesMapToFiles(filesMap);
if (fname != null) {
list(fname, files);
} else {
InputStream in = new FileInputStream(FileDescriptor.in);
try {
list(new BufferedInputStream(in), files);
} finally {
in.close();
}
}
} else if (xflag) {
replaceFSC(filesMap);
String[] files = filesMapToFiles(filesMap);
if (fname != null && files != null) {
extract(fname, files);
} else {
InputStream in = (fname == null)
? new FileInputStream(FileDescriptor.in)
: new FileInputStream(fname);
try {
if (!extract(new BufferedInputStream(in), files) && fname != null) {
extract(fname, files);
}
} finally {
in.close();
}
}
} else if (iflag) {
String[] files = filesMap.get(BASE_VERSION);
genIndex(rootjar, files);
} else if (dflag) {
boolean found;
if (fname != null) {
try (ZipFile zf = new ZipFile(fname)) {
found = describeModule(zf);
}
} else {
try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) {
found = describeModuleFromStream(fin);
}
}
if (!found)
error(getMsg("error.module.descriptor.not.found"));
}
} catch (IOException e) {
fatalError(e);
ok = false;
} catch (Error ee) {
ee.printStackTrace();
ok = false;
} catch (Throwable t) {
t.printStackTrace();
ok = false;
} finally {
if (tmpFile != null && tmpFile.exists())
tmpFile.delete();
}
out.flush();
err.flush();
return ok;
}
private void validateAndClose(File tmpfile) throws IOException {
if (ok && isMultiRelease) {
try (ZipFile zf = new ZipFile(tmpfile)) {
ok = Validator.validate(this, zf);
if (!ok) {
error(formatMsg("error.validator.jarfile.invalid", fname));
}
} catch (IOException e) {
error(formatMsg2("error.validator.jarfile.exception", fname, e.getMessage()));
}
}
Path path = tmpfile.toPath();
try {
if (ok) {
if (fname != null) {
Files.move(path, Paths.get(fname), StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(path, new FileOutputStream(FileDescriptor.out));
}
}
} finally {
Files.deleteIfExists(path);
}
}
private String[] filesMapToFiles(Map<Integer,String[]> filesMap) {
if (filesMap.isEmpty()) return null;
return filesMap.entrySet()
.stream()
.flatMap(this::filesToEntryNames)
.toArray(String[]::new);
}
Stream<String> filesToEntryNames(Map.Entry<Integer,String[]> fileEntries) {
int version = fileEntries.getKey();
Set<String> cpaths = pathsMap.get(version);
return Stream.of(fileEntries.getValue())
.map(f -> toVersionedName(toEntryName(f, cpaths, false), version));
}
boolean parseArgs(String args[]) {
try {
args = CommandLine.parse(args);
} catch (FileNotFoundException e) {
fatalError(formatMsg("error.cant.open", e.getMessage()));
return false;
} catch (IOException e) {
fatalError(e);
return false;
}
int count = 1;
try {
String flags = args[0];
if (flags.startsWith("--") ||
(flags.startsWith("-") && flags.length() == 2)) {
try {
count = GNUStyleOptions.parseOptions(this, args);
} catch (GNUStyleOptions.BadArgs x) {
if (info == null) {
if (x.showUsage) {
usageError(x.getMessage());
} else {
error(x.getMessage());
}
return false;
}
}
if (info != null) {
info.accept(out);
return true;
}
} else {
if (flags.startsWith("-")) {
flags = flags.substring(1);
}
for (int i = 0; i < flags.length(); i++) {
switch (flags.charAt(i)) {
case 'c':
if (xflag || tflag || uflag || iflag) {
usageError(getMsg("error.multiple.main.operations"));
return false;
}
cflag = true;
break;
case 'u':
if (cflag || xflag || tflag || iflag) {
usageError(getMsg("error.multiple.main.operations"));
return false;
}
uflag = true;
break;
case 'x':
if (cflag || uflag || tflag || iflag) {
usageError(getMsg("error.multiple.main.operations"));
return false;
}
xflag = true;
break;
case 't':
if (cflag || uflag || xflag || iflag) {
usageError(getMsg("error.multiple.main.operations"));
return false;
}
tflag = true;
break;
case 'M':
Mflag = true;
break;
case 'v':
vflag = true;
break;
case 'f':
fname = args[count++];
break;
case 'm':
mname = args[count++];
break;
case '0':
flag0 = true;
break;
case 'i':
if (cflag || uflag || xflag || tflag) {
usageError(getMsg("error.multiple.main.operations"));
return false;
}
rootjar = args[count++];
iflag = true;
break;
case 'n':
nflag = true;
break;
case 'e':
ename = args[count++];
break;
case 'P':
pflag = true;
break;
default:
usageError(formatMsg("error.illegal.option",
String.valueOf(flags.charAt(i))));
return false;
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
usageError(getMsg("main.usage.summary"));
return false;
}
if (!cflag && !tflag && !xflag && !uflag && !iflag && !dflag) {
usageError(getMsg("error.bad.option"));
return false;
}
int n = args.length - count;
if (n > 0) {
int version = BASE_VERSION;
int k = 0;
String[] nameBuf = new String[n];
pathsMap.put(version, new HashSet<>());
try {
for (int i = count; i < args.length; i++) {
if (args[i].equals("-C")) {
if (dflag) {
usageError(getMsg("error.bad.dflag"));
return false;
}
String dir = args[++i];
dir = (dir.endsWith(File.separator) ?
dir : (dir + File.separator));
dir = dir.replace(File.separatorChar, '/');
boolean hasUNC = (File.separatorChar == '\\'&& dir.startsWith("//"));
while (dir.indexOf("//") > -1) {
dir = dir.replace("//", "/");
}
if (hasUNC) {
dir = "/" + dir;
}
pathsMap.get(version).add(dir);
nameBuf[k++] = dir + args[++i];
} else if (args[i].startsWith("--release")) {
int v = BASE_VERSION;
try {
v = Integer.valueOf(args[++i]);
} catch (NumberFormatException x) {
error(formatMsg("error.release.value.notnumber", args[i]));
}
if (v < 9) {
usageError(formatMsg("error.release.value.toosmall", String.valueOf(v)));
return false;
}
if (k > 0) {
String[] files = new String[k];
System.arraycopy(nameBuf, 0, files, 0, k);
filesMap.put(version, files);
isMultiRelease = version > BASE_VERSION;
}
k = 0;
nameBuf = new String[n];
version = v;
releaseValue = version;
pathsMap.put(version, new HashSet<>());
} else {
if (dflag) {
usageError(getMsg("error.bad.dflag"));
return false;
}
nameBuf[k++] = args[i];
}
}
} catch (ArrayIndexOutOfBoundsException e) {
usageError(getMsg("error.bad.file.arg"));
return false;
}
if (k > 0) {
String[] files = new String[k];
System.arraycopy(nameBuf, 0, files, 0, k);
filesMap.put(version, files);
isMultiRelease = version > BASE_VERSION;
}
} else if (cflag && (mname == null)) {
usageError(getMsg("error.bad.cflag"));
return false;
} else if (uflag) {
if ((mname != null) || (ename != null) || moduleVersion != null) {
return true;
} else {
usageError(getMsg("error.bad.uflag"));
return false;
}
}
return true;
}
void addPackageIfNamed(Set<String> packages, String name) {
if (name.startsWith(VERSIONS_DIR)) {
int i0 = VERSIONS_DIR_LENGTH;
int i = name.indexOf('/', i0);
if (i <= 0) {
warn(formatMsg("warn.release.unexpected.versioned.entry", name));
return;
}
while (i0 < i) {
char c = name.charAt(i0);
if (c < '0' || c > '9') {
warn(formatMsg("warn.release.unexpected.versioned.entry", name));
return;
}
i0++;
}
name = name.substring(i + 1, name.length());
}
String pn = toPackageName(name);
if (Checks.isPackageName(pn)) {
packages.add(pn);
}
}
private String toEntryName(String name, Set<String> cpaths, boolean isDir) {
name = name.replace(File.separatorChar, '/');
if (isDir) {
name = name.endsWith("/") ? name : name + "/";
}
String matchPath = "";
for (String path : cpaths) {
if (name.startsWith(path) && path.length() > matchPath.length()) {
matchPath = path;
}
}
name = safeName(name.substring(matchPath.length()));
if (name.startsWith("./")) {
name = name.substring(2);
}
return name;
}
private static String toVersionedName(String name, int version) {
return version > BASE_VERSION
? VERSIONS_DIR + version + "/" + name : name;
}
private static String toPackageName(String path) {
int index = path.lastIndexOf('/');
if (index != -1) {
return path.substring(0, index).replace('/', '.');
} else {
return "";
}
}
private void expand() throws IOException {
for (int version : filesMap.keySet()) {
String[] files = filesMap.get(version);
expand(null, files, pathsMap.get(version), version);
}
}
private void expand(File dir, String[] files, Set<String> cpaths, int version)
throws IOException
{
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
File f;
if (dir == null)
f = new File(files[i]);
else
f = new File(dir, files[i]);
boolean isDir = f.isDirectory();
String name = toEntryName(f.getPath(), cpaths, isDir);
if (version != BASE_VERSION) {
if (name.startsWith(VERSIONS_DIR)) {
error(formatMsg2("error.release.unexpected.versioned.entry",
name, String.valueOf(version)));
ok = false;
return;
}
name = toVersionedName(name, version);
}
if (f.isFile()) {
Entry e = new Entry(f, name, false);
if (isModuleInfoEntry(name)) {
moduleInfos.putIfAbsent(name, Files.readAllBytes(f.toPath()));
if (uflag)
entryMap.put(name, e);
} else if (entries.add(e)) {
if (uflag)
entryMap.put(name, e);
}
} else if (isDir) {
Entry e = new Entry(f, name, true);
if (entries.add(e)) {
if (entryMap.containsKey(name)) {
entries.remove(e);
} else {
entryMap.put(name, e);
}
expand(f, f.list(), cpaths, version);
}
} else {
error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
ok = false;
}
}
}
void create(OutputStream out, Manifest manifest) throws IOException
{
try (ZipOutputStream zos = new JarOutputStream(out)) {
if (flag0) {
zos.setMethod(ZipOutputStream.STORED);
}
if (manifest != null) {
if (vflag) {
output(getMsg("out.added.manifest"));
}
ZipEntry e = new ZipEntry(MANIFEST_DIR);
e.setTime(System.currentTimeMillis());
e.setSize(0);
e.setCrc(0);
zos.putNextEntry(e);
e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, manifest);
}
zos.putNextEntry(e);
manifest.write(zos);
zos.closeEntry();
}
updateModuleInfo(moduleInfos, zos);
for (Entry entry : entries) {
addFile(zos, entry);
}
}
}
private char toUpperCaseASCII(char c) {
return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');
}
private boolean equalsIgnoreCase(String s, String upper) {
assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);
int len;
if ((len = s.length()) != upper.length())
return false;
for (int i = 0; i < len; i++) {
char c1 = s.charAt(i);
char c2 = upper.charAt(i);
if (c1 != c2 && toUpperCaseASCII(c1) != c2)
return false;
}
return true;
}
boolean update(InputStream in, OutputStream out,
InputStream newManifest,
Map<String,byte[]> moduleInfos,
JarIndex jarIndex) throws IOException
{
ZipInputStream zis = new ZipInputStream(in);
ZipOutputStream zos = new JarOutputStream(out);
ZipEntry e = null;
boolean foundManifest = false;
boolean updateOk = true;
Set<String> jentries = new HashSet<>();
if (jarIndex != null) {
addIndex(jarIndex, zos);
}
while ((e = zis.getNextEntry()) != null) {
String name = e.getName();
boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);
boolean isModuleInfoEntry = isModuleInfoEntry(name);
if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))
|| (Mflag && isManifestEntry)) {
continue;
} else if (isManifestEntry && ((newManifest != null) ||
(ename != null) || isMultiRelease)) {
foundManifest = true;
if (newManifest != null) {
FileInputStream fis = new FileInputStream(mname);
boolean ambiguous = isAmbiguousMainClass(new Manifest(fis));
fis.close();
if (ambiguous) {
return false;
}
}
Manifest old = new Manifest(zis);
if (newManifest != null) {
old.read(newManifest);
}
if (!updateManifest(old, zos)) {
return false;
}
} else if (moduleInfos != null && isModuleInfoEntry) {
moduleInfos.putIfAbsent(name, zis.readAllBytes());
} else {
boolean isDir = e.isDirectory();
if (!entryMap.containsKey(name)) {
ZipEntry e2 = new ZipEntry(name);
e2.setMethod(e.getMethod());
e2.setTime(e.getTime());
e2.setComment(e.getComment());
e2.setExtra(e.getExtra());
if (e.getMethod() == ZipEntry.STORED) {
e2.setSize(e.getSize());
e2.setCrc(e.getCrc());
}
zos.putNextEntry(e2);
copy(zis, zos);
} else {
Entry ent = entryMap.get(name);
addFile(zos, ent);
entryMap.remove(name);
entries.remove(ent);
isDir = ent.isDir;
}
if (!isDir) {
jentries.add(name);
}
}
}
for (Entry entry : entries) {
addFile(zos, entry);
if (!entry.isDir) {
jentries.add(entry.name);
}
}
if (!foundManifest) {
if (newManifest != null) {
Manifest m = new Manifest(newManifest);
updateOk = !isAmbiguousMainClass(m);
if (updateOk) {
if (!updateManifest(m, zos)) {
updateOk = false;
}
}
} else if (ename != null) {
if (!updateManifest(new Manifest(), zos)) {
updateOk = false;
}
}
}
if (updateOk) {
if (moduleInfos != null && !moduleInfos.isEmpty()) {
Set<String> pkgs = new HashSet<>();
jentries.forEach( je -> addPackageIfNamed(pkgs, je));
addExtendedModuleAttributes(moduleInfos, pkgs);
updateOk = checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries);
updateModuleInfo(moduleInfos, zos);
} else if (moduleVersion != null || modulesToHash != null) {
error(getMsg("error.module.options.without.info"));
updateOk = false;
}
}
zis.close();
zos.close();
return updateOk;
}
private void addIndex(JarIndex index, ZipOutputStream zos)
throws IOException
{
ZipEntry e = new ZipEntry(INDEX_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
CRC32OutputStream os = new CRC32OutputStream();
index.write(os);
os.updateEntry(e);
}
zos.putNextEntry(e);
index.write(zos);
zos.closeEntry();
}
private void updateModuleInfo(Map<String,byte[]> moduleInfos, ZipOutputStream zos)
throws IOException
{
String fmt = uflag ? "out.update.module-info": "out.added.module-info";
for (Map.Entry<String,byte[]> mi : moduleInfos.entrySet()) {
String name = mi.getKey();
byte[] bytes = mi.getValue();
ZipEntry e = new ZipEntry(name);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32ModuleInfo(e, bytes);
}
zos.putNextEntry(e);
zos.write(bytes);
zos.closeEntry();
if (vflag) {
output(formatMsg(fmt, name));
}
}
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
if (isMultiRelease) {
addMultiRelease(m);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private static final boolean isWinDriveLetter(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
private String safeName(String name) {
if (!pflag) {
int len = name.length();
int i = name.lastIndexOf("../");
if (i == -1) {
i = 0;
} else {
i += 3;
}
if (File.separatorChar == '\\') {
while (i < len) {
int off = i;
if (i + 1 < len &&
name.charAt(i + 1) == ':' &&
isWinDriveLetter(name.charAt(i))) {
i += 2;
}
while (i < len && name.charAt(i) == '/') {
i++;
}
if (i == off) {
break;
}
}
} else {
while (i < len && name.charAt(i) == '/') {
i++;
}
}
if (i != 0) {
name = name.substring(i);
}
}
return name;
}
private void addVersion(Manifest m) {
Attributes global = m.getMainAttributes();
if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {
global.put(Attributes.Name.MANIFEST_VERSION, VERSION);
}
}
private void addCreatedBy(Manifest m) {
Attributes global = m.getMainAttributes();
if (global.getValue(new Attributes.Name("Created-By")) == null) {
String javaVendor = System.getProperty("java.vendor");
String jdkVersion = System.getProperty("java.version");
global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +
javaVendor + ")");
}
}
private void addMainClass(Manifest m, String mainApp) {
Attributes global = m.getMainAttributes();
global.put(Attributes.Name.MAIN_CLASS, mainApp);
}
private void addMultiRelease(Manifest m) {
Attributes global = m.getMainAttributes();
global.put(Attributes.Name.MULTI_RELEASE, "true");
}
private boolean isAmbiguousMainClass(Manifest m) {
if (ename != null) {
Attributes global = m.getMainAttributes();
if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {
usageError(getMsg("error.bad.eflag"));
return true;
}
}
return false;
}
void addFile(ZipOutputStream zos, Entry entry) throws IOException {
File file = entry.file;
String name = entry.name;
boolean isDir = entry.isDir;
if (name.isEmpty() || name.equals(".") || name.equals(zname)) {
return;
} else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))
&& !Mflag) {
if (vflag) {
output(formatMsg("out.ignore.entry", name));
}
return;
} else if (name.equals(MODULE_INFO)) {
throw new Error("Unexpected module info: " + name);
}
long size = isDir ? 0 : file.length();
if (vflag) {
out.print(formatMsg("out.adding", name));
}
ZipEntry e = new ZipEntry(name);
e.setTime(file.lastModified());
if (size == 0) {
e.setMethod(ZipEntry.STORED);
e.setSize(0);
e.setCrc(0);
} else if (flag0) {
crc32File(e, file);
}
zos.putNextEntry(e);
if (!isDir) {
copy(file, zos);
}
zos.closeEntry();
if (vflag) {
size = e.getSize();
long csize = e.getCompressedSize();
out.print(formatMsg2("out.size", String.valueOf(size),
String.valueOf(csize)));
if (e.getMethod() == ZipEntry.DEFLATED) {
long ratio = 0;
if (size != 0) {
ratio = ((size - csize) * 100) / size;
}
output(formatMsg("out.deflated", String.valueOf(ratio)));
} else {
output(getMsg("out.stored"));
}
}
}
private byte[] copyBuf = new byte[8192];
private void copy(InputStream from, OutputStream to) throws IOException {
int n;
while ((n = from.read(copyBuf)) != -1)
to.write(copyBuf, 0, n);
}
private void copy(File from, OutputStream to) throws IOException {
try (InputStream in = new FileInputStream(from)) {
copy(in, to);
}
}
private void copy(InputStream from, File to) throws IOException {
try (OutputStream out = new FileOutputStream(to)) {
copy(from, out);
}
}
private void crc32ModuleInfo(ZipEntry e, byte[] bytes) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
in.transferTo(os);
os.updateEntry(e);
}
private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
m.write(os);
os.updateEntry(e);
}
private void crc32File(ZipEntry e, File f) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
copy(f, os);
if (os.n != f.length()) {
throw new JarException(formatMsg(
"error.incorrect.length", f.getPath()));
}
os.updateEntry(e);
}
void replaceFSC(Map<Integer, String []> filesMap) {
filesMap.keySet().forEach(version -> {
String[] files = filesMap.get(version);
if (files != null) {
for (int i = 0; i < files.length; i++) {
files[i] = files[i].replace(File.separatorChar, '/');
}
}
});
}
@SuppressWarnings("serial")
Set<ZipEntry> newDirSet() {
return new HashSet<ZipEntry>() {
public boolean add(ZipEntry e) {
return ((e == null || useExtractionTime) ? false : super.add(e));
}};
}
void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {
for (ZipEntry ze : zes) {
long lastModified = ze.getTime();
if (lastModified != -1) {
String name = safeName(ze.getName().replace(File.separatorChar, '/'));
if (name.length() != 0) {
File f = new File(name.replace('/', File.separatorChar));
f.setLastModified(lastModified);
}
}
}
}
boolean extract(InputStream in, String files[]) throws IOException {
ZipInputStream zis = new ZipInputStream(in);
ZipEntry e;
boolean entriesFound = false;
Set<ZipEntry> dirs = newDirSet();
while ((e = zis.getNextEntry()) != null) {
entriesFound = true;
if (files == null) {
dirs.add(extractFile(zis, e));
} else {
String name = e.getName();
for (String file : files) {
if (name.startsWith(file)) {
dirs.add(extractFile(zis, e));
break;
}
}
}
}
updateLastModifiedTime(dirs);
return entriesFound;
}
void extract(String fname, String files[]) throws IOException {
ZipFile zf = new ZipFile(fname);
Set<ZipEntry> dirs = newDirSet();
Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) {
ZipEntry e = zes.nextElement();
if (files == null) {
dirs.add(extractFile(zf.getInputStream(e), e));
} else {
String name = e.getName();
for (String file : files) {
if (name.startsWith(file)) {
dirs.add(extractFile(zf.getInputStream(e), e));
break;
}
}
}
}
zf.close();
updateLastModifiedTime(dirs);
}
ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
ZipEntry rc = null;
String name = safeName(e.getName().replace(File.separatorChar, '/'));
if (name.length() == 0) {
return rc;
}
File f = new File(name.replace('/', File.separatorChar));
if (e.isDirectory()) {
if (f.exists()) {
if (!f.isDirectory()) {
throw new IOException(formatMsg("error.create.dir",
f.getPath()));
}
} else {
if (!f.mkdirs()) {
throw new IOException(formatMsg("error.create.dir",
f.getPath()));
} else {
rc = e;
}
}
if (vflag) {
output(formatMsg("out.create", name));
}
} else {
if (f.getParent() != null) {
File d = new File(f.getParent());
if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
throw new IOException(formatMsg(
"error.create.dir", d.getPath()));
}
}
try {
copy(is, f);
} finally {
if (is instanceof ZipInputStream)
((ZipInputStream)is).closeEntry();
else
is.close();
}
if (vflag) {
if (e.getMethod() == ZipEntry.DEFLATED) {
output(formatMsg("out.inflated", name));
} else {
output(formatMsg("out.extracted", name));
}
}
}
if (!useExtractionTime) {
long lastModified = e.getTime();
if (lastModified != -1) {
f.setLastModified(lastModified);
}
}
return rc;
}
void list(InputStream in, String files[]) throws IOException {
ZipInputStream zis = new ZipInputStream(in);
ZipEntry e;
while ((e = zis.getNextEntry()) != null) {
zis.closeEntry();
printEntry(e, files);
}
}
void list(String fname, String files[]) throws IOException {
ZipFile zf = new ZipFile(fname);
Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) {
printEntry(zes.nextElement(), files);
}
zf.close();
}
void dumpIndex(String rootjar, JarIndex index) throws IOException {
File jarFile = new File(rootjar);
Path jarPath = jarFile.toPath();
Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();
try {
if (update(Files.newInputStream(jarPath),
Files.newOutputStream(tmpPath),
null, null, index)) {
try {
Files.move(tmpPath, jarPath, REPLACE_EXISTING);
} catch (IOException e) {
throw new IOException(getMsg("error.write.file"), e);
}
}
} finally {
Files.deleteIfExists(tmpPath);
}
}
private HashSet<String> jarPaths = new HashSet<String>();
List<String> getJarPath(String jar) throws IOException {
List<String> files = new ArrayList<String>();
files.add(jar);
jarPaths.add(jar);
String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));
JarFile rf = new JarFile(jar.replace('/', File.separatorChar));
if (rf != null) {
Manifest man = rf.getManifest();
if (man != null) {
Attributes attr = man.getMainAttributes();
if (attr != null) {
String value = attr.getValue(Attributes.Name.CLASS_PATH);
if (value != null) {
StringTokenizer st = new StringTokenizer(value);
while (st.hasMoreTokens()) {
String ajar = st.nextToken();
if (!ajar.endsWith("/")) {
ajar = path.concat(ajar);
if (! jarPaths.contains(ajar)) {
files.addAll(getJarPath(ajar));
}
}
}
}
}
}
}
rf.close();
return files;
}
void genIndex(String rootjar, String[] files) throws IOException {
List<String> jars = getJarPath(rootjar);
int njars = jars.size();
String[] jarfiles;
if (njars == 1 && files != null) {
for (int i = 0; i < files.length; i++) {
jars.addAll(getJarPath(files[i]));
}
njars = jars.size();
}
jarfiles = jars.toArray(new String[njars]);
JarIndex index = new JarIndex(jarfiles);
dumpIndex(rootjar, index);
}
void printEntry(ZipEntry e, String[] files) throws IOException {
if (files == null) {
printEntry(e);
} else {
String name = e.getName();
for (String file : files) {
if (name.startsWith(file)) {
printEntry(e);
return;
}
}
}
}
void printEntry(ZipEntry e) throws IOException {
if (vflag) {
StringBuilder sb = new StringBuilder();
String s = Long.toString(e.getSize());
for (int i = 6 - s.length(); i > 0; --i) {
sb.append(' ');
}
sb.append(s).append(' ').append(new Date(e.getTime()).toString());
sb.append(' ').append(e.getName());
output(sb.toString());
} else {
output(e.getName());
}
}
void usageError(String s) {
err.println(s);
err.println(getMsg("main.usage.summary.try"));
}
void fatalError(Exception e) {
e.printStackTrace();
}
void fatalError(String s) {
error(program + ": " + s);
}
protected void output(String s) {
out.println(s);
}
void error(String s) {
err.println(s);
}
void warn(String s) {
err.println(s);
}
public static void main(String args[]) {
Main jartool = new Main(System.out, System.err, "jar");
System.exit(jartool.run(args) ? 0 : 1);
}
private static class CRC32OutputStream extends java.io.OutputStream {
final CRC32 crc = new CRC32();
long n = 0;
CRC32OutputStream() {}
public void write(int r) throws IOException {
crc.update(r);
n++;
}
public void write(byte[] b, int off, int len) throws IOException {
crc.update(b, off, len);
n += len;
}
public void updateEntry(ZipEntry e) {
e.setMethod(ZipEntry.STORED);
e.setSize(n);
e.setCrc(crc.getValue());
}
}
private File createTemporaryFile(String tmpbase, String suffix) {
File tmpfile = null;
try {
tmpfile = File.createTempFile(tmpbase, suffix);
} catch (IOException | SecurityException e) {
}
if (tmpfile == null) {
if (fname != null) {
try {
File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();
tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);
} catch (IOException ioe) {
fatalError(ioe);
}
} else {
fatalError(new IOException(getMsg("error.create.tempfile")));
}
}
return tmpfile;
}
interface ModuleInfoEntry {
String name();
Optional<String> uriString();
InputStream bytes() throws IOException;
}
static class ZipFileModuleInfoEntry implements ModuleInfoEntry {
private final ZipFile zipFile;
private final ZipEntry entry;
ZipFileModuleInfoEntry(ZipFile zipFile, ZipEntry entry) {
this.zipFile = zipFile;
this.entry = entry;
}
@Override public String name() { return entry.getName(); }
@Override public InputStream bytes() throws IOException {
return zipFile.getInputStream(entry);
}
@Override public Optional<String> uriString() {
String uri = (Paths.get(zipFile.getName())).toUri().toString();
uri = "jar:" + uri + "/!" + entry.getName();
return Optional.of(uri);
}
}
static class StreamedModuleInfoEntry implements ModuleInfoEntry {
private final String name;
private final byte[] bytes;
StreamedModuleInfoEntry(String name, byte[] bytes) {
this.name = name;
this.bytes = bytes;
}
@Override public String name() { return name; }
@Override public InputStream bytes() throws IOException {
return new ByteArrayInputStream(bytes);
}
@Override public Optional<String> uriString() {
return Optional.empty();
}
}
private boolean describeModule(ZipFile zipFile) throws IOException {
ZipFileModuleInfoEntry[] infos = zipFile.stream()
.filter(e -> isModuleInfoEntry(e.getName()))
.sorted(ENTRY_COMPARATOR)
.map(e -> new ZipFileModuleInfoEntry(zipFile, e))
.toArray(ZipFileModuleInfoEntry[]::new);
if (infos.length == 0) {
String fn = zipFile.getName();
ModuleFinder mf = ModuleFinder.of(Paths.get(fn));
try {
Set<ModuleReference> mref = mf.findAll();
if (mref.isEmpty()) {
output(formatMsg("error.unable.derive.automodule", fn));
return true;
}
ModuleDescriptor md = mref.iterator().next().descriptor();
output(getMsg("out.automodule") + "\n");
describeModule(md, null, null, "");
} catch (FindException e) {
String msg = formatMsg("error.unable.derive.automodule", fn);
Throwable t = e.getCause();
if (t != null)
msg = msg + "\n" + t.getMessage();
output(msg);
}
} else {
return describeModuleFromEntries(infos);
}
return true;
}
private boolean describeModuleFromStream(FileInputStream fis)
throws IOException
{
List<ModuleInfoEntry> infos = new LinkedList<>();
try (BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis)) {
ZipEntry e;
while ((e = zis.getNextEntry()) != null) {
String ename = e.getName();
if (isModuleInfoEntry(ename)) {
infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes()));
}
}
}
if (infos.size() == 0)
return false;
ModuleInfoEntry[] sorted = infos.stream()
.sorted(Comparator.comparing(ModuleInfoEntry::name, ENTRYNAME_COMPARATOR))
.toArray(ModuleInfoEntry[]::new);
return describeModuleFromEntries(sorted);
}
private boolean lessThanEqualReleaseValue(ModuleInfoEntry entry) {
return intVersionFromEntry(entry) <= releaseValue ? true : false;
}
private static String versionFromEntryName(String name) {
String s = name.substring(VERSIONS_DIR_LENGTH);
return s.substring(0, s.indexOf("/"));
}
private static int intVersionFromEntry(ModuleInfoEntry entry) {
String name = entry.name();
if (!name.startsWith(VERSIONS_DIR))
return BASE_VERSION;
String s = name.substring(VERSIONS_DIR_LENGTH);
s = s.substring(0, s.indexOf('/'));
return Integer.valueOf(s);
}
private boolean describeModuleFromEntries(ModuleInfoEntry[] infos)
throws IOException
{
assert infos.length > 0;
String releases = Arrays.stream(infos)
.filter(e -> !e.name().equals(MODULE_INFO))
.map(ModuleInfoEntry::name)
.map(Main::versionFromEntryName)
.collect(joining(" "));
if (!releases.isEmpty())
output("releases: " + releases + "\n");
if (releaseValue != -1) {
ModuleInfoEntry entry = null;
int i = 0;
while (i < infos.length && lessThanEqualReleaseValue(infos[i])) {
entry = infos[i];
i++;
}
if (entry == null) {
output(formatMsg("error.no.operative.descriptor",
String.valueOf(releaseValue)));
return false;
}
String uriString = entry.uriString().orElse("");
try (InputStream is = entry.bytes()) {
describeModule(is, uriString);
}
} else {
if (infos[0].name().equals(MODULE_INFO)) {
String uriString = infos[0].uriString().orElse("");
try (InputStream is = infos[0].bytes()) {
describeModule(is, uriString);
}
} else {
output(getMsg("error.no.root.descriptor"));
}
}
return true;
}
static <T> String toLowerCaseString(Collection<T> set) {
if (set.isEmpty()) { return ""; }
return " " + set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT))
.sorted().collect(joining(" "));
}
static <T> String toString(Collection<T> set) {
if (set.isEmpty()) { return ""; }
return " " + set.stream().map(e -> e.toString()).sorted().collect(joining(" "));
}
private void describeModule(InputStream entryInputStream, String uriString)
throws IOException
{
ModuleInfo.Attributes attrs = ModuleInfo.read(entryInputStream, null);
ModuleDescriptor md = attrs.descriptor();
ModuleTarget target = attrs.target();
ModuleHashes hashes = attrs.recordedHashes();
describeModule(md, target, hashes, uriString);
}
private void describeModule(ModuleDescriptor md,
ModuleTarget target,
ModuleHashes hashes,
String uriString)
throws IOException
{
StringBuilder sb = new StringBuilder();
sb.append(md.toNameAndVersion());
if (!uriString.isEmpty())
sb.append(" ").append(uriString);
if (md.isOpen())
sb.append(" open");
if (md.isAutomatic())
sb.append(" automatic");
sb.append("\n");
md.exports().stream()
.sorted(Comparator.comparing(Exports::source))
.filter(e -> !e.isQualified())
.forEach(e -> sb.append("exports ").append(e.source())
.append(toLowerCaseString(e.modifiers()))
.append("\n"));
md.requires().stream().sorted()
.forEach(r -> sb.append("requires ").append(r.name())
.append(toLowerCaseString(r.modifiers()))
.append("\n"));
md.uses().stream().sorted()
.forEach(s -> sb.append("uses ").append(s).append("\n"));
md.provides().stream()
.sorted(Comparator.comparing(Provides::service))
.forEach(p -> sb.append("provides ").append(p.service())
.append(" with")
.append(toString(p.providers()))
.append("\n"));
md.exports().stream()
.sorted(Comparator.comparing(Exports::source))
.filter(Exports::isQualified)
.forEach(e -> sb.append("qualified exports ").append(e.source())
.append(" to").append(toLowerCaseString(e.targets()))
.append("\n"));
md.opens().stream()
.sorted(Comparator.comparing(Opens::source))
.filter(o -> !o.isQualified())
.forEach(o -> sb.append("opens ").append(o.source())
.append(toLowerCaseString(o.modifiers()))
.append("\n"));
md.opens().stream()
.sorted(Comparator.comparing(Opens::source))
.filter(Opens::isQualified)
.forEach(o -> sb.append("qualified opens ").append(o.source())
.append(toLowerCaseString(o.modifiers()))
.append(" to").append(toLowerCaseString(o.targets()))
.append("\n"));
Set<String> concealed = new TreeSet<>(md.packages());
md.exports().stream().map(Exports::source).forEach(concealed::remove);
md.opens().stream().map(Opens::source).forEach(concealed::remove);
concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));
md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));
if (target != null) {
String targetPlatform = target.targetPlatform();
if (!targetPlatform.isEmpty())
sb.append("platform ").append(targetPlatform).append("\n");
}
if (hashes != null) {
hashes.names().stream().sorted().forEach(
mod -> sb.append("hashes ").append(mod).append(" ")
.append(hashes.algorithm()).append(" ")
.append(toHex(hashes.hashFor(mod)))
.append("\n"));
}
output(sb.toString());
}
private static String toHex(byte[] ba) {
StringBuilder sb = new StringBuilder(ba.length << 1);
for (byte b: ba) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
static String toBinaryName(String classname) {
return (classname.replace('.', '/')) + ".class";
}
private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries)
throws IOException
{
boolean ok = true;
if (moduleInfoBytes != null) {
try {
ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes));
if (md.provides().stream().map(Provides::providers).flatMap(List::stream)
.filter(p -> !entries.contains(toBinaryName(p)))
.peek(p -> fatalError(formatMsg("error.missing.provider", p)))
.count() != 0) {
ok = false;
}
} catch (InvalidModuleDescriptorException x) {
fatalError(x.getMessage());
ok = false;
}
}
return ok;
}
private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos,
Set<String> packages)
throws IOException
{
for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) {
ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue()));
e.setValue(extendedInfoBytes(md, e.getValue(), packages));
}
}
static boolean isModuleInfoEntry(String name) {
if (name.endsWith(MODULE_INFO)) {
int end = name.length() - MODULE_INFO.length();
if (end == 0)
return true;
if (name.startsWith(VERSIONS_DIR)) {
int off = VERSIONS_DIR_LENGTH;
if (off == end)
return false;
while (off < end - 1) {
char c = name.charAt(off++);
if (c < '0' || c > '9')
return false;
}
return name.charAt(off) == '/';
}
}
return false;
}
private byte[] extendedInfoBytes(ModuleDescriptor md,
byte[] miBytes,
Set<String> packages)
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new ByteArrayInputStream(miBytes);
ModuleInfoExtender extender = ModuleInfoExtender.newExtender(is);
extender.packages(packages);
if (ename != null)
extender.mainClass(ename);
if (moduleVersion != null)
extender.version(moduleVersion);
if (modulesToHash != null) {
String mn = md.name();
Hasher hasher = new Hasher(md, fname);
ModuleHashes moduleHashes = hasher.computeHashes(mn);
if (moduleHashes != null) {
extender.hashes(moduleHashes);
} else {
warn("warning: no module is recorded in hash in " + mn);
}
}
if (moduleResolution.value() != 0) {
extender.moduleResolution(moduleResolution);
}
extender.write(baos);
return baos.toByteArray();
}
private class Hasher {
final ModuleHashesBuilder hashesBuilder;
final ModuleFinder finder;
final Set<String> modules;
Hasher(ModuleDescriptor descriptor, String fname) throws IOException {
URI uri = Paths.get(fname).toUri();
ModuleReference mref = new ModuleReference(descriptor, uri) {
@Override
public ModuleReader open() {
throw new UnsupportedOperationException("should not reach here");
}
};
this.finder = ModuleFinder.compose(moduleFinder,
new ModuleFinder() {
@Override
public Optional<ModuleReference> find(String name) {
if (descriptor.name().equals(name))
return Optional.of(mref);
else
return Optional.empty();
}
@Override
public Set<ModuleReference> findAll() {
return Collections.singleton(mref);
}
});
Set<String> roots = finder.findAll().stream()
.map(ref -> ref.descriptor().name())
.filter(mn -> modulesToHash.matcher(mn).find())
.collect(Collectors.toSet());
ModuleFinder system;
String name = descriptor.name();
if (name != null && ModuleFinder.ofSystem().find(name).isPresent()) {
system = ModuleFinder.of();
} else {
system = ModuleFinder.ofSystem();
}
Configuration config =
Configuration.empty().resolve(system, finder, roots);
this.modules = config.modules().stream()
.map(ResolvedModule::name)
.filter(mn -> roots.contains(mn) && !system.find(mn).isPresent())
.collect(Collectors.toSet());
this.hashesBuilder = new ModuleHashesBuilder(config, modules);
}
ModuleHashes computeHashes(String name) {
if (hashesBuilder == null)
return null;
return hashesBuilder.computeHashes(Set.of(name)).get(name);
}
}
static Comparator<String> ENTRYNAME_COMPARATOR = (s1, s2) -> {
if (s1.equals(s2)) return 0;
boolean b1 = s1.startsWith(VERSIONS_DIR);
boolean b2 = s2.startsWith(VERSIONS_DIR);
if (b1 && !b2) return 1;
if (!b1 && b2) return -1;
int n = 0;
if (b1 && b2) {
n = VERSIONS_DIR.length();
int i1 = s1.indexOf('/', n);
int i2 = s2.indexOf('/', n);
if (i1 == -1) throw new Validator.InvalidJarException(s1);
if (i2 == -1) throw new Validator.InvalidJarException(s2);
if (i1 != i2) return i1 - i2;
}
int l1 = s1.length();
int l2 = s2.length();
int lim = Math.min(l1, l2);
for (int k = n; k < lim; k++) {
char c1 = s1.charAt(k);
char c2 = s2.charAt(k);
if (c1 != c2) {
if (c1 == '$' && c2 == '.') return 1;
if (c1 == '.' && c2 == '$') return -1;
return c1 - c2;
}
}
return l1 - l2;
};
static Comparator<ZipEntry> ENTRY_COMPARATOR =
Comparator.comparing(ZipEntry::getName, ENTRYNAME_COMPARATOR);
}