Skip to content

Commit aa9c136

Browse files
author
Roger Riggs
committed
8251989: Hex formatting and parsing utility
Reviewed-by: tvaleev, chegar, naoto, darcy
1 parent efd61c6 commit aa9c136

File tree

14 files changed

+1921
-167
lines changed

14 files changed

+1921
-167
lines changed

src/java.base/share/classes/java/lang/Integer.java

+8
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,18 @@ public static String toUnsignedString(int i, int radix) {
264264
* <blockquote>
265265
* {@code Integer.toHexString(n).toUpperCase()}
266266
* </blockquote>
267+
* <p>
268+
* @apiNote
269+
* The {@link java.util.HexFormat} class provides formatting and parsing
270+
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
271+
* {@code HexFormat} formats and parses uppercase or lowercase hexadecimal characters,
272+
* with leading zeros and for byte arrays includes for each byte
273+
* a delimiter, prefix, and suffix.
267274
*
268275
* @param i an integer to be converted to a string.
269276
* @return the string representation of the unsigned integer value
270277
* represented by the argument in hexadecimal (base&nbsp;16).
278+
* @see java.util.HexFormat
271279
* @see #parseUnsignedInt(String, int)
272280
* @see #toUnsignedString(int, int)
273281
* @since 1.0.2

src/java.base/share/classes/java/lang/Long.java

+8
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,19 @@ private static BigInteger toUnsignedBigInteger(long i) {
299299
* <blockquote>
300300
* {@code Long.toHexString(n).toUpperCase()}
301301
* </blockquote>
302+
* <p>
303+
* @apiNote
304+
* The {@link java.util.HexFormat} class provides formatting and parsing
305+
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
306+
* {@code HexFormat} formats and parses uppercase or lowercase hexadecimal characters,
307+
* with leading zeros and for byte arrays includes for each byte
308+
* a delimiter, prefix, and suffix.
302309
*
303310
* @param i a {@code long} to be converted to a string.
304311
* @return the string representation of the unsigned {@code long}
305312
* value represented by the argument in hexadecimal
306313
* (base&nbsp;16).
314+
* @see java.util.HexFormat
307315
* @see #parseUnsignedLong(String, int)
308316
* @see #toUnsignedString(long, int)
309317
* @since 1.0.2

src/java.base/share/classes/java/lang/module/Resolver.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Deque;
3636
import java.util.HashMap;
3737
import java.util.HashSet;
38+
import java.util.HexFormat;
3839
import java.util.LinkedHashSet;
3940
import java.util.List;
4041
import java.util.Map;
@@ -475,25 +476,17 @@ private void checkHashes() {
475476
if (actualHash == null)
476477
findFail("Unable to compute the hash of module %s", dn);
477478
if (!Arrays.equals(recordedHash, actualHash)) {
479+
HexFormat hex = HexFormat.of();
478480
findFail("Hash of %s (%s) differs to expected hash (%s)" +
479-
" recorded in %s", dn, toHexString(actualHash),
480-
toHexString(recordedHash), descriptor.name());
481+
" recorded in %s", dn, hex.formatHex(actualHash),
482+
hex.formatHex(recordedHash), descriptor.name());
481483
}
482484
}
483485
}
484486

485487
}
486488
}
487489

488-
private static String toHexString(byte[] ba) {
489-
StringBuilder sb = new StringBuilder(ba.length * 2);
490-
for (byte b: ba) {
491-
sb.append(String.format("%02x", b & 0xff));
492-
}
493-
return sb.toString();
494-
}
495-
496-
497490
/**
498491
* Computes the readability graph for the modules in the given Configuration.
499492
*

0 commit comments

Comments
 (0)