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

@ -264,10 +264,18 @@ public final class Integer extends Number
* <blockquote>
* {@code Integer.toHexString(n).toUpperCase()}
* </blockquote>
* <p>
* @apiNote
* The {@link java.util.HexFormat} class provides formatting and parsing
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
* {@code HexFormat} formats and parses uppercase or lowercase hexadecimal characters,
* with leading zeros and for byte arrays includes for each byte
* a delimiter, prefix, and suffix.
*
* @param i an integer to be converted to a string.
* @return the string representation of the unsigned integer value
* represented by the argument in hexadecimal (base&nbsp;16).
* @see java.util.HexFormat
* @see #parseUnsignedInt(String, int)
* @see #toUnsignedString(int, int)
* @since 1.0.2

View file

@ -299,11 +299,19 @@ public final class Long extends Number
* <blockquote>
* {@code Long.toHexString(n).toUpperCase()}
* </blockquote>
* <p>
* @apiNote
* The {@link java.util.HexFormat} class provides formatting and parsing
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
* {@code HexFormat} formats and parses uppercase or lowercase hexadecimal characters,
* with leading zeros and for byte arrays includes for each byte
* a delimiter, prefix, and suffix.
*
* @param i a {@code long} to be converted to a string.
* @return the string representation of the unsigned {@code long}
* value represented by the argument in hexadecimal
* (base&nbsp;16).
* @see java.util.HexFormat
* @see #parseUnsignedLong(String, int)
* @see #toUnsignedString(long, int)
* @since 1.0.2

View file

@ -35,6 +35,7 @@ import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@ -475,9 +476,10 @@ final class Resolver {
if (actualHash == null)
findFail("Unable to compute the hash of module %s", dn);
if (!Arrays.equals(recordedHash, actualHash)) {
HexFormat hex = HexFormat.of();
findFail("Hash of %s (%s) differs to expected hash (%s)" +
" recorded in %s", dn, toHexString(actualHash),
toHexString(recordedHash), descriptor.name());
" recorded in %s", dn, hex.formatHex(actualHash),
hex.formatHex(recordedHash), descriptor.name());
}
}
}
@ -485,15 +487,6 @@ final class Resolver {
}
}
private static String toHexString(byte[] ba) {
StringBuilder sb = new StringBuilder(ba.length * 2);
for (byte b: ba) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
/**
* Computes the readability graph for the modules in the given Configuration.
*

File diff suppressed because it is too large Load diff

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