Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-1008

fromHexString converts non-hex Strings to ByteBuffer

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Sprint:
      Mina Sprint 1

      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;
       }
      

        Attachments

          Activity

            People

            • Assignee:
              johnnyv Jonathan Valliere
              Reporter:
              nullpointer7 nullpointer
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: