Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8251989: Hex formatting and parsing utility #482

Closed
wants to merge 33 commits into from
Closed
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c1bb451
8251989: Hex formatting and parsing utility
RogerRiggs Sep 2, 2020
3170b49
Various code review comments, rename UpperCase and LowerCase methods …
RogerRiggs Oct 13, 2020
e7edf07
temp updates
RogerRiggs Oct 14, 2020
1892eca
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Oct 14, 2020
ec334db
Added assertions to testVariableLength and samples
RogerRiggs Oct 14, 2020
59bdb07
Cleanup of javadoc markup
RogerRiggs Oct 15, 2020
880cebb
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Oct 15, 2020
bad80cb
Expanded test coverage and fixed related bugs; Added static imports f…
RogerRiggs Oct 16, 2020
450bfaf
Test enhancements from Chris Hegarty
RogerRiggs Oct 16, 2020
d9f2b6e
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Oct 16, 2020
1d2c26f
Correct length of StringBuilder in formatHex;
RogerRiggs Oct 19, 2020
0a47544
Review comment updates, in the example code, and to describe the char…
RogerRiggs Oct 21, 2020
2b493d3
Review comment updates to class javadoc
RogerRiggs Oct 22, 2020
94e6061
- Added @see and @link references to Integer.toHexString and Long.toH…
RogerRiggs Oct 26, 2020
2aeab7d
The HexFormat API indexing model for array and string ranges is changed
RogerRiggs Oct 27, 2020
80a7118
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Oct 27, 2020
552d308
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Nov 2, 2020
1d36cfb
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Nov 24, 2020
b19d282
Clarified that suffix() and prefix() methods do not return null, inst…
RogerRiggs Nov 25, 2020
cad6a70
Addressed review comments on use of formatted hexadecimal strings, up…
RogerRiggs Nov 27, 2020
29f9bf7
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Nov 27, 2020
a1ce9d7
Clarified hexadecimal characters used in converting from characters t…
RogerRiggs Nov 30, 2020
bbcb213
Review comment updates:
RogerRiggs Dec 1, 2020
892d08e
Increased memory to 4G for the test and add diagnostic info for OOME
RogerRiggs Dec 1, 2020
a137740
Clarify that the fromHexDigits does not use the uppercase parameter.
RogerRiggs Dec 2, 2020
848c7a6
Clarify that the fromHexDigit method does not use the prefix, suffix,…
RogerRiggs Dec 2, 2020
0a8088b
Add class level clarification of use of uppercase for primitive conve…
RogerRiggs Dec 2, 2020
9ddda60
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Dec 4, 2020
af7b1fa
Added @apiNote to fromHexDigits methods to link and compare to simila…
RogerRiggs Dec 5, 2020
044d463
Clarified parsing of hex as unsigned in fromHexDigits methods.
RogerRiggs Dec 9, 2020
8a6d379
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Dec 9, 2020
b9e288f
Merge branch 'master' into 8251989-hex-formatter
RogerRiggs Dec 15, 2020
4866a0a
Correct typos in fromHexDigits methods javadoc
RogerRiggs Dec 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/java.base/share/classes/java/lang/Integer.java
Original file line number Diff line number Diff line change
@@ -257,10 +257,18 @@ public static String toUnsignedString(int i, int radix) {
* <blockquote>
* {@code Integer.toHexString(n).toUpperCase()}
* </blockquote>
* <p>
Copy link

@Marcono1234 Marcono1234 Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Marcono1234, thanks for making a comment in an OpenJDK project!

All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user Marcono1234 for the summary.

If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.

Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use.

Sorry, something went wrong.

* @apiNote
* {@link java.util.HexFormat} provides formatting and parsing
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
* 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
8 changes: 8 additions & 0 deletions src/java.base/share/classes/java/lang/Long.java
Original file line number Diff line number Diff line change
@@ -292,11 +292,19 @@ private static BigInteger toUnsignedBigInteger(long i) {
* <blockquote>
* {@code Long.toHexString(n).toUpperCase()}
* </blockquote>
* <p>
Copy link

@Marcono1234 Marcono1234 Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Marcono1234, thanks for making a comment in an OpenJDK project!

All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user Marcono1234 for the summary.

If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.

Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use.

Sorry, something went wrong.

* @apiNote
* {@link java.util.HexFormat} provides formatting and parsing
* of byte arrays and primitives to return a string or adding to an {@link Appendable}.
* 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
311 changes: 156 additions & 155 deletions src/java.base/share/classes/java/util/HexFormat.java

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions test/jdk/java/util/HexFormat/HexFormatTest.java
Original file line number Diff line number Diff line change
@@ -94,24 +94,24 @@ Object[][] HexStringsThrowing() {
@DataProvider(name = "BadBytesThrowing")
Object[][] badBytesThrowing() {
return new Object[][]{
{new byte[1], 0, 2}, // bad length
{new byte[1], 1, 1}, // bad offset + length
{new byte[1], -1, 2}, // bad length
{new byte[1], -1, 1}, // bad offset + length
{new byte[1], 0, -1}, // bad length
{new byte[1], 1, -1}, // bad offset + length
{new byte[1], 0, 2}, // bad toIndex
{new byte[1], 1, 2}, // bad fromIndex + toIndex
{new byte[1], -1, 2}, // bad fromIndex
{new byte[1], -1, 1}, // bad fromIndex
{new byte[1], 0, -1}, // bad toIndex
{new byte[1], 1, -1}, // bad toIndex
};
}

@DataProvider(name = "BadParseHexThrowing")
Object[][] badParseHexThrowing() {
return new Object[][]{
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad length
{"b", 1, 1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad length
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad length
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad fromIndex
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad fromIndex
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"76543210", 0, 7, IllegalArgumentException.class}, // odd number of digits
{"zz00", 0, 4, IllegalArgumentException.class}, // non-hex digits
{"00zz", 0, 4, IllegalArgumentException.class}, // non-hex digits
@@ -121,12 +121,12 @@ Object[][] badParseHexThrowing() {
@DataProvider(name = "BadFromHexDigitsThrowing")
Object[][] badHexDigitsThrowing() {
return new Object[][]{
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad length
{"b", 1, 1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad length
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad length
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad offset + length
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, 2, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
};
}

@@ -309,12 +309,12 @@ static void badParseHex(String string, int offset, int length,
}

@Test(dataProvider = "BadFromHexDigitsThrowing")
static void badFromHexDigits(String string, int offset, int length,
static void badFromHexDigits(String string, int fromIndex, int toIndex,
Class<? extends Throwable> exClass) {
assertThrows(exClass,
() -> HexFormat.of().fromHexDigits(string, offset, length));
() -> HexFormat.of().fromHexDigits(string, fromIndex, toIndex));
assertThrows(exClass,
() -> HexFormat.of().fromHexDigitsToLong(string, offset, length));
() -> HexFormat.of().fromHexDigitsToLong(string, fromIndex, toIndex));
}

// Verify IAE for strings that are too long for the target primitive type
@@ -392,7 +392,7 @@ static void testParseHexStringRange(String delimiter, String prefix, String suff
System.out.println(" formatted subrange: " +
s.substring(low * stride, high * stride - delimiter.length()));
byte[] actual = hex.parseHex(s, low * stride,
high * stride - delimiter.length() - low * stride);
high * stride - delimiter.length());
System.out.println(" parsed as: " + Arrays.toString(actual));

assertEquals(actual.length, (high - low), "array length");
@@ -417,7 +417,7 @@ static void testFormatHexRangeString(String unused1, String unused2, String unus
byte[] expected = genBytes('A', 15);
int low = 1;
int high = expected.length - 2;
String s = hex.formatHex(expected, low, high - low);
String s = hex.formatHex(expected, low, high);
System.out.println(" formatted: " + s);

byte[] actual = hex.parseHex(s);
@@ -448,7 +448,7 @@ static void testFormatHexRangeAppendable(String unused1, String unused2, String
int low = 1;
int high = expected.length - 2;
StringBuilder sb = new StringBuilder();
StringBuilder s = hex.formatHex(sb, expected, low, high - low);
StringBuilder s = hex.formatHex(sb, expected, low, high);
assertEquals(s, sb, "formatHex returned unknown StringBuilder");
System.out.println(" formatted: " + s);

@@ -493,7 +493,7 @@ static void testFormatHexCharArrayIndexed(String delimiter, String prefix, Strin
s.substring(low * stride, high * stride - delimiter.length()));
char[] chars = s.toCharArray();
byte[] actual = hex.parseHex(chars, low * stride,
(high - low ) * stride - delimiter.length());
high * stride - delimiter.length());
System.out.println(" parsed as: " + Arrays.toString(actual));

assertEquals(actual.length, (high - low), "array length");