mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 10:34:38 +02:00
8251989: Hex formatting and parsing utility
Reviewed-by: tvaleev, chegar, naoto, darcy
This commit is contained in:
parent
efd61c6f53
commit
aa9c136d67
14 changed files with 1921 additions and 167 deletions
|
@ -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 16).
|
||||
* @see java.util.HexFormat
|
||||
* @see #parseUnsignedInt(String, int)
|
||||
* @see #toUnsignedString(int, int)
|
||||
* @since 1.0.2
|
||||
|
|
|
@ -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 16).
|
||||
* @see java.util.HexFormat
|
||||
* @see #parseUnsignedLong(String, int)
|
||||
* @see #toUnsignedString(long, int)
|
||||
* @since 1.0.2
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
1108
src/java.base/share/classes/java/util/HexFormat.java
Normal file
1108
src/java.base/share/classes/java/util/HexFormat.java
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -26,6 +26,7 @@
|
|||
package javax.net.ssl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/**
|
||||
* Instances of this class represent a server name in a Server Name
|
||||
|
@ -52,9 +53,6 @@ public abstract class SNIServerName {
|
|||
// the encoded value of the server name
|
||||
private final byte[] encoded;
|
||||
|
||||
// the hex digitals
|
||||
private static final char[] HEXES = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
/**
|
||||
* Creates an {@code SNIServerName} using the specified name type and
|
||||
* encoded value.
|
||||
|
@ -192,22 +190,7 @@ public abstract class SNIServerName {
|
|||
if (bytes.length == 0) {
|
||||
return "(empty)";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 3 - 1);
|
||||
boolean isInitial = true;
|
||||
for (byte b : bytes) {
|
||||
if (isInitial) {
|
||||
isInitial = false;
|
||||
} else {
|
||||
sb.append(':');
|
||||
}
|
||||
|
||||
int k = b & 0xFF;
|
||||
sb.append(HEXES[k >>> 4]);
|
||||
sb.append(HEXES[k & 0xF]);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
return HexFormat.ofDelimiter(":").withUpperCase().formatHex(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ import java.nio.charset.CharsetDecoder;
|
|||
import java.nio.charset.CharsetEncoder;
|
||||
import java.nio.charset.CoderResult;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import sun.nio.cs.UTF_8;
|
||||
|
||||
|
@ -47,6 +48,8 @@ import sun.nio.cs.UTF_8;
|
|||
|
||||
public final class ParseUtil {
|
||||
|
||||
private static final HexFormat HEX_UPPERCASE = HexFormat.of().withUpperCase();
|
||||
|
||||
private ParseUtil() {}
|
||||
|
||||
/**
|
||||
|
@ -515,15 +518,9 @@ public final class ParseUtil {
|
|||
}
|
||||
}
|
||||
|
||||
private static final char[] hexDigits = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
private static void appendEscape(StringBuilder sb, byte b) {
|
||||
sb.append('%');
|
||||
sb.append(hexDigits[(b >> 4) & 0x0f]);
|
||||
sb.append(hexDigits[(b >> 0) & 0x0f]);
|
||||
HEX_UPPERCASE.toHexDigits(sb, b);
|
||||
}
|
||||
|
||||
// Tell whether the given character is permitted by the given mask pair
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue