8340885: Desugar ZipCoder.Comparison
Reviewed-by: lancea, eirbjo
This commit is contained in:
parent
120463dc90
commit
d8790aa048
@ -56,28 +56,27 @@ class ZipCoder {
|
||||
}
|
||||
|
||||
/**
|
||||
* This enum represents the three possible return values for
|
||||
* Constants representing the three possible return values for
|
||||
* {@link #compare(String, byte[], int, int, boolean)} when
|
||||
* this method compares a lookup name to a string encoded in the
|
||||
* CEN byte array.
|
||||
*/
|
||||
enum Comparison {
|
||||
/**
|
||||
static final byte
|
||||
/*
|
||||
* The lookup string is exactly equal
|
||||
* to the encoded string.
|
||||
*/
|
||||
EXACT_MATCH,
|
||||
/**
|
||||
*/
|
||||
EXACT_MATCH = 0,
|
||||
/*
|
||||
* The lookup string and the encoded string differs only
|
||||
* by the encoded string having a trailing '/' character.
|
||||
*/
|
||||
DIRECTORY_MATCH,
|
||||
/**
|
||||
DIRECTORY_MATCH = 1,
|
||||
/*
|
||||
* The lookup string and the encoded string do not match.
|
||||
* (They are neither an exact match or a directory match.)
|
||||
*/
|
||||
NO_MATCH
|
||||
}
|
||||
NO_MATCH = 2;
|
||||
|
||||
String toString(byte[] ba, int off, int length) {
|
||||
try {
|
||||
@ -197,13 +196,13 @@ class ZipCoder {
|
||||
* The return values of this method are as follows:
|
||||
*
|
||||
* If the lookup name is exactly equal to the encoded string, return
|
||||
* {@link Comparison#EXACT_MATCH}.
|
||||
* {@link EXACT_MATCH}.
|
||||
*
|
||||
* If the parameter {@code matchDirectory} is {@code true} and the
|
||||
* two strings differ only by the encoded string having an extra
|
||||
* trailing '/' character, then return {@link Comparison#DIRECTORY_MATCH}.
|
||||
* trailing '/' character, then return {@link DIRECTORY_MATCH}.
|
||||
*
|
||||
* Otherwise, return {@link Comparison#NO_MATCH}
|
||||
* Otherwise, return {@link NO_MATCH}
|
||||
*
|
||||
* While a general implementation will need to decode bytes into a
|
||||
* String for comparison, this can be avoided if the String coder
|
||||
@ -217,18 +216,18 @@ class ZipCoder {
|
||||
* a directory match will also be tested
|
||||
*
|
||||
*/
|
||||
Comparison compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
|
||||
byte compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
|
||||
String decoded = toString(b, off, len);
|
||||
if (decoded.startsWith(str)) {
|
||||
if (decoded.length() == str.length()) {
|
||||
return Comparison.EXACT_MATCH;
|
||||
return EXACT_MATCH;
|
||||
} else if (matchDirectory
|
||||
&& decoded.length() == str.length() + 1
|
||||
&& decoded.endsWith("/") ) {
|
||||
return Comparison.DIRECTORY_MATCH;
|
||||
return DIRECTORY_MATCH;
|
||||
}
|
||||
}
|
||||
return Comparison.NO_MATCH;
|
||||
return NO_MATCH;
|
||||
}
|
||||
static final class UTF8ZipCoder extends ZipCoder {
|
||||
|
||||
@ -278,19 +277,19 @@ class ZipCoder {
|
||||
}
|
||||
|
||||
@Override
|
||||
Comparison compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
|
||||
byte compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
|
||||
try {
|
||||
byte[] encoded = JLA.getBytesNoRepl(str, UTF_8.INSTANCE);
|
||||
int mismatch = Arrays.mismatch(encoded, 0, encoded.length, b, off, off+len);
|
||||
if (mismatch == -1) {
|
||||
return Comparison.EXACT_MATCH;
|
||||
return EXACT_MATCH;
|
||||
} else if (matchDirectory && len == mismatch + 1 && hasTrailingSlash(b, off + len)) {
|
||||
return Comparison.DIRECTORY_MATCH;
|
||||
return DIRECTORY_MATCH;
|
||||
} else {
|
||||
return Comparison.NO_MATCH;
|
||||
return NO_MATCH;
|
||||
}
|
||||
} catch (CharacterCodingException e) {
|
||||
return Comparison.NO_MATCH;
|
||||
return NO_MATCH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1869,15 +1869,15 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
|
||||
// Compare the lookup name with the name encoded in the CEN
|
||||
switch (zc.compare(name, cen, noff, nlen, addSlash)) {
|
||||
case EXACT_MATCH:
|
||||
case ZipCoder.EXACT_MATCH:
|
||||
// We found an exact match for "name"
|
||||
return new EntryPos(name, pos);
|
||||
case DIRECTORY_MATCH:
|
||||
case ZipCoder.DIRECTORY_MATCH:
|
||||
// We found the directory "name/"
|
||||
// Track its position, then continue the search for "name"
|
||||
dirPos = pos;
|
||||
break;
|
||||
case NO_MATCH:
|
||||
case ZipCoder.NO_MATCH:
|
||||
// Hash collision, continue searching
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user