diff --git a/agrona/src/main/java/org/agrona/AsciiEncoding.java b/agrona/src/main/java/org/agrona/AsciiEncoding.java index 55dc6c721..6545a7819 100644 --- a/agrona/src/main/java/org/agrona/AsciiEncoding.java +++ b/agrona/src/main/java/org/agrona/AsciiEncoding.java @@ -80,13 +80,13 @@ public static int endOffset(final long value) * @param index within the string the value is encoded. * @param value of the encoding in ASCII. * @return the digit value of the encoded ASCII. - * @throws NumberFormatException if the value is not a digit. + * @throws AsciiNumberFormatException if the value is not a digit. */ public static int getDigit(final int index, final byte value) { if (value < 0x30 || value > 0x39) { - throw new NumberFormatException("'" + ((char)value) + "' is not a valid digit @ " + index); + throw new AsciiNumberFormatException("'" + ((char)value) + "' is not a valid digit @ " + index); } return value - 0x30; @@ -98,13 +98,13 @@ public static int getDigit(final int index, final byte value) * @param index within the string the value is encoded. * @param value of the encoding in ASCII. * @return the digit value of the encoded ASCII. - * @throws NumberFormatException if the value is not a digit. + * @throws AsciiNumberFormatException if the value is not a digit. */ public static int getDigit(final int index, final char value) { if (value < 0x30 || value > 0x39) { - throw new NumberFormatException("'" + value + "' is not a valid digit @ " + index); + throw new AsciiNumberFormatException("'" + value + "' is not a valid digit @ " + index); } return value - 0x30; diff --git a/agrona/src/main/java/org/agrona/AsciiNumberFormatException.java b/agrona/src/main/java/org/agrona/AsciiNumberFormatException.java new file mode 100644 index 000000000..b60a17bf2 --- /dev/null +++ b/agrona/src/main/java/org/agrona/AsciiNumberFormatException.java @@ -0,0 +1,9 @@ +package org.agrona; + +public class AsciiNumberFormatException extends NumberFormatException +{ + public AsciiNumberFormatException(final String message) + { + super(message); + } +} diff --git a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java index 51e26791d..04028dda8 100644 --- a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java +++ b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java @@ -18,7 +18,7 @@ import org.junit.Test; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertThat; public class AsciiEncodingTest { @@ -41,4 +41,16 @@ public void shouldParseLong() assertThat(AsciiEncoding.parseLongAscii("-7", 0, 2), is(-7L)); assertThat(AsciiEncoding.parseLongAscii("3333", 1, 2), is(33L)); } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenDecodingCharNonAsciiValue() + { + AsciiEncoding.getDigit(0, 'a'); + } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenDecodingByteNonAsciiValue() + { + AsciiEncoding.getDigit(0, (byte)'a'); + } } \ No newline at end of file