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

@ -701,7 +701,7 @@ public class Properties extends Hashtable<Object,Object> {
bufLen = Integer.MAX_VALUE;
}
StringBuilder outBuffer = new StringBuilder(bufLen);
HexFormat hex = HexFormat.of().withUpperCase();
for(int x=0; x<len; x++) {
char aChar = theString.charAt(x);
// Handle common case first, selecting largest block that
@ -736,12 +736,8 @@ public class Properties extends Hashtable<Object,Object> {
break;
default:
if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
outBuffer.append('\\');
outBuffer.append('u');
outBuffer.append(toHex((aChar >> 12) & 0xF));
outBuffer.append(toHex((aChar >> 8) & 0xF));
outBuffer.append(toHex((aChar >> 4) & 0xF));
outBuffer.append(toHex( aChar & 0xF));
outBuffer.append("\\u");
outBuffer.append(hex.toHexDigits(aChar));
} else {
outBuffer.append(aChar);
}
@ -752,24 +748,19 @@ public class Properties extends Hashtable<Object,Object> {
private static void writeComments(BufferedWriter bw, String comments)
throws IOException {
HexFormat hex = HexFormat.of().withUpperCase();
bw.write("#");
int len = comments.length();
int current = 0;
int last = 0;
char[] uu = new char[6];
uu[0] = '\\';
uu[1] = 'u';
while (current < len) {
char c = comments.charAt(current);
if (c > '\u00ff' || c == '\n' || c == '\r') {
if (last != current)
bw.write(comments.substring(last, current));
if (c > '\u00ff') {
uu[2] = toHex((c >> 12) & 0xf);
uu[3] = toHex((c >> 8) & 0xf);
uu[4] = toHex((c >> 4) & 0xf);
uu[5] = toHex( c & 0xf);
bw.write(new String(uu));
bw.write("\\u");
bw.write(hex.toHexDigits(c));
} else {
bw.newLine();
if (c == '\r' &&
@ -1271,19 +1262,6 @@ public class Properties extends Hashtable<Object,Object> {
}
}
/**
* Convert a nibble to a hex character
* @param nibble the nibble to convert.
*/
private static char toHex(int nibble) {
return hexDigit[(nibble & 0xF)];
}
/** A table of hex digits */
private static final char[] hexDigit = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
//
// Hashtable methods overridden and delegated to a ConcurrentHashMap instance