Commit MetaInfo
Log Message
Optimize byte[]->String/String->byte[]
Change Summary
Incremental Difference
| | @@ -29,6 +29,12 @@ import jp.bitmeister.asn1.exception.ASN1IllegalArgument; | 29 | 29 | public class HexString implements StringItem { | 30 | 30 | | 31 | 31 | /** | | 32 | + * Hexadecimal digit characters. | | 33 | + */ | | 34 | + private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', | | 35 | + '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; | | 36 | + | | 37 | + /** | 32 | 38 | * The hexadecimal string. | 33 | 39 | */ | 34 | 40 | private String string; |
| | @@ -49,7 +55,7 @@ public class HexString implements StringItem { | 49 | 55 | throw ex; | 50 | 56 | } | 51 | 57 | StringBuilder builder = new StringBuilder(); | 52 | | - for (char c: string.toCharArray()) { | | 58 | + for (char c : string.toCharArray()) { | 53 | 59 | if (Character.digit(c, 16) >= 0) { | 54 | 60 | builder.append(c); | 55 | 61 | } |
| | @@ -67,7 +73,8 @@ public class HexString implements StringItem { | 67 | 73 | public HexString(byte... array) { | 68 | 74 | StringBuilder builder = new StringBuilder(); | 69 | 75 | for (Byte b : array) { | 70 | | - builder.append(String.format("%02X", b)); | | 76 | + builder.append(HEX_DIGITS[(b & 0xf0) >> 4]); | | 77 | + builder.append(HEX_DIGITS[b & 0x0f]); | 71 | 78 | } | 72 | 79 | string = builder.toString(); | 73 | 80 | } |
| | @@ -89,10 +96,10 @@ public class HexString implements StringItem { | 89 | 96 | public boolean[] toBinArray() { | 90 | 97 | boolean[] array = new boolean[string.length() * 4]; | 91 | 98 | int index = 0, mask = 0; | 92 | | - byte octet = 0; | | 99 | + int octet = 0; | 93 | 100 | for (int i = 0; i < array.length; i++) { | 94 | 101 | if (mask == 0) { | 95 | | - octet = Byte.parseByte(string.substring(index++, index), 16); | | 102 | + octet = charToInt(string.charAt(index++)); | 96 | 103 | mask = 8; | 97 | 104 | } | 98 | 105 | array[i] = (octet & mask) > 0; |
| | @@ -107,15 +114,14 @@ public class HexString implements StringItem { | 107 | 114 | * @return An array of {@code byte}. | 108 | 115 | */ | 109 | 116 | public byte[] toByteArray() { | 110 | | - byte[] array = new byte[string.length() / 2 + string.length() % 2]; | 111 | | - byte octet = 0; | 112 | | - for (int i = 0; i < string.length(); i++) { | 113 | | - octet = Integer.valueOf(string.substring(i, i + 1), 16).byteValue(); | 114 | | - if (i % 2 == 0) { | 115 | | - array[i / 2] = (byte) (octet << 4); | 116 | | - } else { | 117 | | - array[i / 2] = (byte) (array[i / 2] + octet); | 118 | | - } | | 117 | + byte[] array = new byte[(string.length() + 1) / 2]; | | 118 | + int ci = string.length() - 1, bi = array.length - 1; | | 119 | + if (string.length() % 2 != 0) { | | 120 | + array[bi--] = (byte) (charToInt(string.charAt(ci--)) << 4); | | 121 | + } | | 122 | + for (; ci > 0; bi--) { | | 123 | + array[bi] = (byte) charToInt(string.charAt(ci--)); | | 124 | + array[bi] += (byte) (charToInt(string.charAt(ci--)) << 4); | 119 | 125 | } | 120 | 126 | return array; | 121 | 127 | } |
| | @@ -130,4 +136,13 @@ public class HexString implements StringItem { | 130 | 136 | return '\'' + string + "\'H"; | 131 | 137 | } | 132 | 138 | | | 139 | + /** | | 140 | + * Converts a hexadecimal {@code char} to {@code int} value. | | 141 | + * | | 142 | + * @param c A hexadecimal character. | | 143 | + * @return The integer value. | | 144 | + */ | | 145 | + private int charToInt(char c) { | | 146 | + return c <= '9' ? c - '0' : c + (10 - 'A'); | | 147 | + } | 133 | 148 | } |
Show on old repository browser
|