Repository: mina Updated Branches: refs/heads/trunk 602273832 -> e422ec88d
Fix for DIRMINA-1008: refactor fromHexString to be less tolerant Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/e422ec88 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/e422ec88 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/e422ec88 Branch: refs/heads/trunk Commit: e422ec88defa5da1bd47f01bb952835e2e841b30 Parents: 6022738 Author: Jeff MAURY <[email protected]> Authored: Sun Mar 22 22:08:44 2015 +0100 Committer: Jeff MAURY <[email protected]> Committed: Sun Mar 22 22:10:25 2015 +0100 ---------------------------------------------------------------------- .../org/apache/mina/util/ByteBufferDumper.java | 5 +++- .../apache/mina/util/ByteBufferDumperTest.java | 27 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/e422ec88/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java b/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java index e351a13..25f79e7 100644 --- a/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java +++ b/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java @@ -143,7 +143,10 @@ public class ByteBufferDumper { ByteBuffer res = ByteBuffer.allocate(size); for (int i = 0; i < size; i++) { - int b = ((Character.digit(hex.charAt(i * 2), 16) << 4) | (Character.digit(hex.charAt(i * 2 + 1), 16))); + int b = Integer.parseInt(hex.substring(i, i + 2), 16); + if (Integer.highestOneBit(b) == 128) { + b = b - 256; + } res.put((byte) b); } http://git-wip-us.apache.org/repos/asf/mina/blob/e422ec88/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java b/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java index e319521..00985bd 100644 --- a/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java +++ b/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java @@ -92,4 +92,31 @@ public class ByteBufferDumperTest { bb.flip(); assertEquals("000102FE", ByteBufferDumper.toHex(bb)); } + + @Test + public void checkFromHexStringEmptyStringReturnsEmptyByteArray() { + ByteBuffer buffer = ByteBufferDumper.fromHexString(""); + assertEquals(0, buffer.remaining()); + } + + @Test + public void checkFromHexStringNormalStringReturnsByteArray() { + ByteBuffer buffer = ByteBufferDumper.fromHexString("ff"); + assertEquals(1, buffer.remaining()); + assertEquals(-1, buffer.get()); + } + + @Test + public void checkFromHexStringNormalStringUppercaseReturnsByteArray() { + ByteBuffer buffer = ByteBufferDumper.fromHexString("FF"); + assertEquals(1, buffer.remaining()); + assertEquals(-1, buffer.get()); + } + + @Test(expected=NumberFormatException.class) + public void checkFromHexStringInvalidStringReturnsException() { + ByteBuffer buffer = ByteBufferDumper.fromHexString("non-hexastring"); + assertEquals(1, buffer.remaining()); + assertEquals(-1, buffer.get()); + } }
