|
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Base64;
|
24 | 24 | import java.util.BitSet;
|
| 25 | +import java.util.HexFormat; |
25 | 26 | import java.util.List;
|
26 | 27 | import java.util.Locale;
|
27 | 28 | import java.util.regex.Matcher;
|
@@ -60,6 +61,8 @@ public final class ContentDisposition {
|
60 | 61 |
|
61 | 62 | private static final BitSet PRINTABLE = new BitSet(256);
|
62 | 63 |
|
| 64 | + private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase(); |
| 65 | + |
63 | 66 |
|
64 | 67 | static {
|
65 | 68 | // RFC 2045, Section 6.7, and RFC 2047, Section 4.2
|
@@ -385,9 +388,10 @@ private static String decodeRfc5987Filename(String filename, Charset charset) {
|
385 | 388 | index++;
|
386 | 389 | }
|
387 | 390 | else if (b == '%' && index < value.length - 2) {
|
388 |
| - char[] array = new char[]{(char) value[index + 1], (char) value[index + 2]}; |
389 | 391 | 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); |
391 | 395 | }
|
392 | 396 | catch (NumberFormatException ex) {
|
393 | 397 | throw new IllegalArgumentException(INVALID_HEADER_FIELD_PARAMETER_FORMAT, ex);
|
@@ -428,8 +432,8 @@ private static String decodeQuotedPrintableFilename(String filename, Charset cha
|
428 | 432 | index++;
|
429 | 433 | }
|
430 | 434 | 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]); |
433 | 437 | if (i1 == -1 || i2 == -1) {
|
434 | 438 | throw new IllegalArgumentException("Not a valid hex sequence: " + filename.substring(index));
|
435 | 439 | }
|
@@ -469,10 +473,8 @@ else if (isPrintable(b)) {
|
469 | 473 | }
|
470 | 474 | else {
|
471 | 475 | 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)); |
476 | 478 | }
|
477 | 479 | }
|
478 | 480 | sb.append("?=");
|
@@ -546,21 +548,13 @@ private static String encodeRfc5987Filename(String input, Charset charset) {
|
546 | 548 | }
|
547 | 549 | else {
|
548 | 550 | 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)); |
553 | 553 | }
|
554 | 554 | }
|
555 | 555 | return sb.toString();
|
556 | 556 | }
|
557 | 557 |
|
558 |
| - private static char hexDigit(int b) { |
559 |
| - return Character.toUpperCase(Character.forDigit(b & 0xF, 16)); |
560 |
| - } |
561 |
| - |
562 |
| - |
563 |
| - |
564 | 558 | /**
|
565 | 559 | * A mutable builder for {@code ContentDisposition}.
|
566 | 560 | */
|
|
0 commit comments