Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Invalid
-
None
-
None
-
None
-
Mina Sprint 2.2.0
Description
I came across this by accident in class org.apache.mina.util.ByteBufferDumper:
fromHexString happily converts Strings like "ff, fa, f2" and "not a hex string" to bytes.
Here is a suggestion for curing the problem:
public static ByteBuffer fromHexString(final String hex) { if (null == hex) { throw new IllegalArgumentException( "null argument not permitted"); } else if (hex.length() % 2 != 0) { throw new IllegalArgumentException( "the hexadecimal string length cannot be odd"); } int size = hex.length() / 2; ByteBuffer res = ByteBuffer.allocate(size); for (int i = 0; i < hex.length(); i += 2) { int b = Integer.parseInt(hex.substring(i, i + 2), 16); if (Integer.highestOneBit(b) == 128) { b = b - 256; } res.put((byte) b); } res.flip(); return res; }