8242596: Improve JarFile.getEntry performance for multi-release jar files

Co-authored-by: Eirik Bjørsnøs <eirbjo@gmail.com>
Reviewed-by: lancea, redestad
This commit is contained in:
Claes Redestad 2020-04-17 11:46:59 +02:00
parent 07156dd25b
commit aeb2f9492e
3 changed files with 123 additions and 30 deletions

View file

@ -431,10 +431,6 @@ public class JarFile extends ZipFile {
return man;
}
private String[] getMetaInfEntryNames() {
return JUZFA.getMetaInfEntryNames((ZipFile)this);
}
/**
* Returns the {@code JarEntry} for the given base entry name or
* {@code null} if not found.
@ -504,11 +500,15 @@ public class JarFile extends ZipFile {
* </div>
*/
public ZipEntry getEntry(String name) {
JarFileEntry je = getEntry0(name);
if (isMultiRelease()) {
return getVersionedEntry(name, je);
JarEntry je = getVersionedEntry(name, null);
if (je == null) {
je = getEntry0(name);
}
return je;
} else {
return getEntry0(name);
}
return je;
}
/**
@ -598,21 +598,29 @@ public class JarFile extends ZipFile {
return name;
}
private JarEntry getVersionedEntry(String name, JarEntry je) {
if (BASE_VERSION_FEATURE < versionFeature) {
if (!name.startsWith(META_INF)) {
private JarEntry getVersionedEntry(String name, JarEntry defaultEntry) {
if (!name.startsWith(META_INF)) {
int[] versions = JUZFA.getMetaInfVersions(this);
if (BASE_VERSION_FEATURE < versionFeature && versions.length > 0) {
// search for versioned entry
int v = versionFeature;
while (v > BASE_VERSION_FEATURE) {
JarFileEntry vje = getEntry0(META_INF_VERSIONS + v + "/" + name);
for (int i = versions.length - 1; i >= 0; i--) {
int version = versions[i];
// skip versions above versionFeature
if (version > versionFeature) {
continue;
}
// skip versions below base version
if (version < BASE_VERSION_FEATURE) {
break;
}
JarFileEntry vje = getEntry0(META_INF_VERSIONS + version + "/" + name);
if (vje != null) {
return vje.withBasename(name);
}
v--;
}
}
}
return je;
return defaultEntry;
}
// placeholder for now
@ -707,7 +715,7 @@ public class JarFile extends ZipFile {
}
if (verify) {
String[] names = getMetaInfEntryNames();
String[] names = JUZFA.getMetaInfEntryNames(this);
if (names != null) {
for (String nameLower : names) {
String name = nameLower.toUpperCase(Locale.ENGLISH);
@ -738,7 +746,7 @@ public class JarFile extends ZipFile {
// Verify "META-INF/" entries...
try {
String[] names = getMetaInfEntryNames();
String[] names = JUZFA.getMetaInfEntryNames(this);
if (names != null) {
for (String name : names) {
String uname = name.toUpperCase(Locale.ENGLISH);
@ -932,7 +940,7 @@ public class JarFile extends ZipFile {
if (manEntry == null) {
// If not found, then iterate through all the "META-INF/"
// entries to find a match.
String[] names = getMetaInfEntryNames();
String[] names = JUZFA.getMetaInfEntryNames(this);
if (names != null) {
for (String name : names) {
if (MANIFEST_NAME.equals(name.toUpperCase(Locale.ENGLISH))) {
@ -1016,7 +1024,7 @@ public class JarFile extends ZipFile {
byte[] lbuf = new byte[512];
Attributes attr = new Attributes();
attr.read(new Manifest.FastInputStream(
new ByteArrayInputStream(b)), lbuf);
new ByteArrayInputStream(b)), lbuf);
isMultiRelease = Boolean.parseBoolean(
attr.getValue(Attributes.Name.MULTI_RELEASE));
}
@ -1068,7 +1076,7 @@ public class JarFile extends ZipFile {
*/
JarEntry newEntry(String name) {
if (isMultiRelease()) {
JarEntry vje = getVersionedEntry(name, (JarEntry)null);
JarEntry vje = getVersionedEntry(name, null);
if (vje != null) {
return vje;
}