Use HexFormat in ContentDisposition
Closes gh-34797 Signed-off-by: Patrick Strawderman <pstrawderman@netflix.com>
This commit is contained in:
committed by
Brian Clozel
parent
7c82a62bfb
commit
542eb6f305
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.BitSet;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -60,6 +61,8 @@ public final class ContentDisposition {
|
||||
|
||||
private static final BitSet PRINTABLE = new BitSet(256);
|
||||
|
||||
private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
|
||||
|
||||
|
||||
static {
|
||||
// RFC 2045, Section 6.7, and RFC 2047, Section 4.2
|
||||
@@ -385,9 +388,10 @@ public final class ContentDisposition {
|
||||
index++;
|
||||
}
|
||||
else if (b == '%' && index < value.length - 2) {
|
||||
char[] array = new char[]{(char) value[index + 1], (char) value[index + 2]};
|
||||
try {
|
||||
baos.write(Integer.parseInt(String.valueOf(array), 16));
|
||||
int high = HexFormat.fromHexDigit(value[index + 1]);
|
||||
int low = HexFormat.fromHexDigit(value[index + 2]);
|
||||
baos.write(high << 4 | low);
|
||||
}
|
||||
catch (NumberFormatException ex) {
|
||||
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT, ex);
|
||||
@@ -428,11 +432,8 @@ public final class ContentDisposition {
|
||||
index++;
|
||||
}
|
||||
else if (b == '=' && index < value.length - 2) {
|
||||
int i1 = Character.digit((char) value[index + 1], 16);
|
||||
int i2 = Character.digit((char) value[index + 2], 16);
|
||||
if (i1 == -1 || i2 == -1) {
|
||||
throw new IllegalArgumentException("Not a valid hex sequence: " + filename.substring(index));
|
||||
}
|
||||
int i1 = HexFormat.fromHexDigit(value[index + 1]);
|
||||
int i2 = HexFormat.fromHexDigit(value[index + 2]);
|
||||
baos.write((i1 << 4) | i2);
|
||||
index += 3;
|
||||
}
|
||||
@@ -469,10 +470,7 @@ public final class ContentDisposition {
|
||||
}
|
||||
else {
|
||||
sb.append('=');
|
||||
char ch1 = hexDigit(b >> 4);
|
||||
char ch2 = hexDigit(b);
|
||||
sb.append(ch1);
|
||||
sb.append(ch2);
|
||||
HEX_FORMAT.toHexDigits(sb, b);
|
||||
}
|
||||
}
|
||||
sb.append("?=");
|
||||
@@ -546,21 +544,12 @@ public final class ContentDisposition {
|
||||
}
|
||||
else {
|
||||
sb.append('%');
|
||||
char hex1 = hexDigit(b >> 4);
|
||||
char hex2 = hexDigit(b);
|
||||
sb.append(hex1);
|
||||
sb.append(hex2);
|
||||
HEX_FORMAT.toHexDigits(sb, b);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static char hexDigit(int b) {
|
||||
return Character.toUpperCase(Character.forDigit(b & 0xF, 16));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A mutable builder for {@code ContentDisposition}.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user