8251989: Hex formatting and parsing utility

Reviewed-by: tvaleev, chegar, naoto, darcy
This commit is contained in:
Roger Riggs 2020-12-16 20:29:49 +00:00
parent efd61c6f53
commit aa9c136d67
14 changed files with 1921 additions and 167 deletions

View file

@ -30,6 +30,7 @@ import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HexFormat;
/**
* Unix specific Path <--> URI conversion
@ -102,14 +103,14 @@ class UnixUriUtils {
byte[] path = up.toAbsolutePath().asByteArray();
StringBuilder sb = new StringBuilder("file:///");
assert path[0] == '/';
HexFormat hex = HexFormat.of().withUpperCase();
for (int i=1; i<path.length; i++) {
char c = (char)(path[i] & 0xff);
if (match(c, L_PATH, H_PATH)) {
sb.append(c);
} else {
sb.append('%');
sb.append(hexDigits[(c >> 4) & 0x0f]);
sb.append(hexDigits[(c) & 0x0f]);
hex.toHexDigits(sb, (byte)c);
}
}
@ -242,9 +243,4 @@ class UnixUriUtils {
// All valid path characters
private static final long L_PATH = L_PCHAR | lowMask(";/");
private static final long H_PATH = H_PCHAR | highMask(";/");
private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
}