Skip to content
Permalink
Browse files
8242842: Avoid reallocating name when checking for trailing slash in …
…ZipFile.getEntryPos

Co-authored-by: Eirik Bjørsnøs <eirbjo@gmail.com>
Reviewed-by: alanb, lancea
  • Loading branch information
cl4es and eirbjo committed Apr 15, 2020
1 parent d983fa6 commit 941bb3bcb8546af51cbfe8b5aff7caa5ace749e9
Showing 1 changed file with 16 additions and 9 deletions.
@@ -1273,7 +1273,7 @@ private static final int hashN(byte[] a, int off, int len) {
return h;
}

private static final int hash_append(int hash, byte b) {
private static final int hashAppend(int hash, byte b) {
return hash * 31 + b;
}

@@ -1492,11 +1492,13 @@ private int getEntryPos(byte[] name, boolean addSlash) {
}
int hsh = hashN(name, 0, name.length);
int idx = table[(hsh & 0x7fffffff) % tablelen];
boolean appendSlash = false;
/*
* This while loop is an optimization where a double lookup
* for name and name+/ is being performed. The name char
* array has enough room at the end to try again with a
* slash appended if the first table lookup does not succeed.
* for name and name+/ is being performed. The name byte
* array will be updated with an added slash only if the first
* table lookup fails and there is a matching hash value for
* name+/.
*/
while (true) {
/*
@@ -1505,6 +1507,11 @@ private int getEntryPos(byte[] name, boolean addSlash) {
*/
while (idx != ZIP_ENDCHAIN) {
if (getEntryHash(idx) == hsh) {
if (appendSlash) {
name = Arrays.copyOf(name, name.length + 1);
name[name.length - 1] = '/';
appendSlash = false;
}
// The CEN name must match the specfied one
int pos = getEntryPos(idx);
if (name.length == CENNAM(cen, pos)) {
@@ -1527,11 +1534,11 @@ private int getEntryPos(byte[] name, boolean addSlash) {
if (!addSlash || name.length == 0 || name[name.length - 1] == '/') {
return -1;
}
/* Add slash and try once more */
name = Arrays.copyOf(name, name.length + 1);
name[name.length - 1] = '/';
hsh = hash_append(hsh, (byte)'/');
//idx = table[hsh % tablelen];
// Add a slash to the hash code
hsh = hashAppend(hsh, (byte)'/');
// If we find a match on the new hash code, we need to append a
// slash when comparing
appendSlash = true;
idx = table[(hsh & 0x7fffffff) % tablelen];
addSlash = false;
}

0 comments on commit 941bb3b

Please sign in to comment.