Skip to content

Commit ee4844a

Browse files
committed
Use HexFormat in ContentDisposition
1 parent e7402bc commit ee4844a

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

spring-web/src/main/java/org/springframework/http/ContentDisposition.java

+12-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Base64;
2424
import java.util.BitSet;
25+
import java.util.HexFormat;
2526
import java.util.List;
2627
import java.util.Locale;
2728
import java.util.regex.Matcher;
@@ -60,6 +61,8 @@ public final class ContentDisposition {
6061

6162
private static final BitSet PRINTABLE = new BitSet(256);
6263

64+
private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
65+
6366

6467
static {
6568
// RFC 2045, Section 6.7, and RFC 2047, Section 4.2
@@ -385,9 +388,10 @@ private static String decodeRfc5987Filename(String filename, Charset charset) {
385388
index++;
386389
}
387390
else if (b == '%' && index < value.length - 2) {
388-
char[] array = new char[]{(char) value[index + 1], (char) value[index + 2]};
389391
try {
390-
baos.write(Integer.parseInt(String.valueOf(array), 16));
392+
int high = HexFormat.fromHexDigit(value[index + 1]);
393+
int low = HexFormat.fromHexDigit(value[index + 2]);
394+
baos.write(high << 4 | low);
391395
}
392396
catch (NumberFormatException ex) {
393397
throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT, ex);
@@ -428,8 +432,8 @@ private static String decodeQuotedPrintableFilename(String filename, Charset cha
428432
index++;
429433
}
430434
else if (b == '=' && index < value.length - 2) {
431-
int i1 = Character.digit((char) value[index + 1], 16);
432-
int i2 = Character.digit((char) value[index + 2], 16);
435+
int i1 = HexFormat.fromHexDigit(value[index + 1]);
436+
int i2 = HexFormat.fromHexDigit(value[index + 2]);
433437
if (i1 == -1 || i2 == -1) {
434438
throw new IllegalArgumentException("Not a valid hex sequence: " + filename.substring(index));
435439
}
@@ -469,10 +473,8 @@ else if (isPrintable(b)) {
469473
}
470474
else {
471475
sb.append('=');
472-
char ch1 = hexDigit(b >> 4);
473-
char ch2 = hexDigit(b);
474-
sb.append(ch1);
475-
sb.append(ch2);
476+
sb.append(HEX_FORMAT.toHighHexDigit(b));
477+
sb.append(HEX_FORMAT.toLowHexDigit(b));
476478
}
477479
}
478480
sb.append("?=");
@@ -546,21 +548,13 @@ private static String encodeRfc5987Filename(String input, Charset charset) {
546548
}
547549
else {
548550
sb.append('%');
549-
char hex1 = hexDigit(b >> 4);
550-
char hex2 = hexDigit(b);
551-
sb.append(hex1);
552-
sb.append(hex2);
551+
sb.append(HEX_FORMAT.toHighHexDigit(b));
552+
sb.append(HEX_FORMAT.toLowHexDigit(b));
553553
}
554554
}
555555
return sb.toString();
556556
}
557557

558-
private static char hexDigit(int b) {
559-
return Character.toUpperCase(Character.forDigit(b & 0xF, 16));
560-
}
561-
562-
563-
564558
/**
565559
* A mutable builder for {@code ContentDisposition}.
566560
*/

0 commit comments

Comments
 (0)