Skip to content

Use HexFormat in ContentDisposition #34797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -385,9 +388,10 @@ private static String decodeRfc5987Filename(String filename, Charset charset) {
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);
Expand Down Expand Up @@ -428,11 +432,8 @@ private static String decodeQuotedPrintableFilename(String filename, Charset cha
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;
}
Expand Down Expand Up @@ -469,10 +470,7 @@ else if (isPrintable(b)) {
}
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("?=");
Expand Down Expand Up @@ -546,21 +544,12 @@ private static String encodeRfc5987Filename(String input, Charset charset) {
}
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}.
*/
Expand Down