| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.util.Arrays; |
| 36 | | | |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | @author |
| 42 | | | |
| 43 | | | public final class HexUtil |
| 44 | | | { |
| 45 | | | private static final int BYTE_UNSIGNED_MAX = 255; |
| 46 | | | private static final int HALF_BYTE = 4; |
| 47 | | | private static final byte LOW_HALF_MASK = (byte) 0x0F; |
| 48 | | | private static final byte HIGH_HALF_MASK = (byte) 0xF0; |
| 49 | | | private static final int BYTE_MASK = 0xFF; |
| 50 | | | private static final int CHARS_PER_BYTE = 2; |
| 51 | | | private static final int DECIMAL_OFFSET = 10; |
| 52 | | | |
| 53 | | | |
| 54 | 100 | | private static final char[] HEX_CHARS = { |
| 55 | | | '0', '1', '2', '3', '4', '5', '6', '7', |
| 56 | | | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 57 | | | }; |
| 58 | | | |
| 59 | 100 | | private static final String[] BYTE_AS_HEX |
| 60 | | | = new String[BYTE_UNSIGNED_MAX + 1]; |
| 61 | | | |
| 62 | 100 | | private static final int[] CHAR_TO_NIBBLE_LOW |
| 63 | | | = new int[BYTE_UNSIGNED_MAX + 1]; |
| 64 | 100 | | private static final int[] CHAR_TO_NIBBLE_HIGH |
| 65 | | | = new int[BYTE_UNSIGNED_MAX + 1]; |
| 66 | | | |
| 67 | | | private static final int DUMP_BYTES_PER_LINE = 16; |
| 68 | | | private static final int DUMP_BYTES_PER_COLUMN = 8; |
| 69 | | | private static final int DUMP_ADDR_LEN = 8; |
| 70 | | | private static final int DUMP_BUFFER_SIZE = DUMP_ADDR_LEN |
| 71 | | | + DUMP_BYTES_PER_LINE * CHARS_PER_BYTE |
| 72 | | | + DUMP_BYTES_PER_LINE |
| 73 | | | + 1 + 1; |
| 74 | | | |
| 75 | | | static |
| 76 | | | { |
| 77 | | | |
| 78 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 79 | 100 | | for (int i = 0; i < BYTE_AS_HEX.length; i++) |
| 80 | | | { |
| 81 | 100 | | sb.setLength(0); |
| 82 | 100 | | final int lowerFourBits = i & LOW_HALF_MASK; |
| 83 | 100 | | final int highFourBits = (i & HIGH_HALF_MASK) >> HALF_BYTE; |
| 84 | 100 | | sb.append(HEX_CHARS[highFourBits]); |
| 85 | 100 | | sb.append(HEX_CHARS[lowerFourBits]); |
| 86 | 100 | | BYTE_AS_HEX[i] = sb.toString(); |
| 87 | | | } |
| 88 | | | |
| 89 | | | |
| 90 | 100 | | Arrays.fill(CHAR_TO_NIBBLE_LOW, -1); |
| 91 | 100 | | Arrays.fill(CHAR_TO_NIBBLE_HIGH, -1); |
| 92 | 100 | | for (int i = '0'; i <= '9'; i++) |
| 93 | | | { |
| 94 | 100 | | CHAR_TO_NIBBLE_LOW[i] = i - '0'; |
| 95 | 100 | | CHAR_TO_NIBBLE_HIGH[i] = (i - '0') << HALF_BYTE; |
| 96 | | | } |
| 97 | 100 | | for (int i = 'a'; i <= 'f'; i++) |
| 98 | | | { |
| 99 | 100 | | CHAR_TO_NIBBLE_LOW[i] = i - 'a' + DECIMAL_OFFSET; |
| 100 | 100 | | CHAR_TO_NIBBLE_HIGH[i] = (i - 'a' + DECIMAL_OFFSET) << HALF_BYTE; |
| 101 | | | } |
| 102 | 100 | | for (int i = 'A'; i <= 'F'; i++) |
| 103 | | | { |
| 104 | 100 | | CHAR_TO_NIBBLE_LOW[i] = i - 'A' + DECIMAL_OFFSET; |
| 105 | 100 | | CHAR_TO_NIBBLE_HIGH[i] = (i - 'A' + DECIMAL_OFFSET) << HALF_BYTE; |
| 106 | | | } |
| 107 | 100 | | } |
| 108 | | | |
| 109 | | | private HexUtil () |
| 110 | 0 | | { |
| 111 | | | |
| 112 | 0 | | } |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | @param |
| 118 | | | @param |
| 119 | | | @param |
| 120 | | | @return |
| 121 | | | |
| 122 | | | @throws |
| 123 | | | |
| 124 | | | public static String bytesToHex ( |
| 125 | | | final byte[] data, final int offset, final int length) |
| 126 | | | throws IndexOutOfBoundsException |
| 127 | | | { |
| 128 | | | final String result; |
| 129 | 100 | | if (data == null) |
| 130 | | | { |
| 131 | 100 | | result = null; |
| 132 | | | } |
| 133 | | | else |
| 134 | | | { |
| 135 | 100 | | final StringBuffer sbuf = new StringBuffer(); |
| 136 | 100 | | for (int i = offset; i < offset + length; i++) |
| 137 | | | { |
| 138 | 100 | | sbuf.append(BYTE_AS_HEX[data[i] & BYTE_MASK]); |
| 139 | | | } |
| 140 | 100 | | result = sbuf.toString(); |
| 141 | | | } |
| 142 | 100 | | return result; |
| 143 | | | } |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | @param |
| 149 | | | @return |
| 150 | | | |
| 151 | | | |
| 152 | | | public static String bytesToHex (final byte[] data) |
| 153 | | | { |
| 154 | | | final String result; |
| 155 | 100 | | if (data != null) |
| 156 | | | { |
| 157 | 100 | | result = bytesToHex(data, 0, data.length); |
| 158 | | | } |
| 159 | | | else |
| 160 | | | { |
| 161 | 0 | | result = null; |
| 162 | | | } |
| 163 | 100 | | return result; |
| 164 | | | } |
| 165 | | | |
| 166 | | | |
| 167 | | | |
| 168 | | | @param |
| 169 | | | @return |
| 170 | | | |
| 171 | | | @throws |
| 172 | | | |
| 173 | | | |
| 174 | | | public static byte[] stringToBytes (final String s) |
| 175 | | | throws IllegalArgumentException |
| 176 | | | { |
| 177 | | | final byte[] result; |
| 178 | 100 | | if (s == null) |
| 179 | | | { |
| 180 | 0 | | result = null; |
| 181 | | | } |
| 182 | 100 | | else if (s.length() == 0) |
| 183 | | | { |
| 184 | 0 | | result = new byte[0]; |
| 185 | | | } |
| 186 | | | else |
| 187 | | | { |
| 188 | | | |
| 189 | 100 | | if (s.length() % CHARS_PER_BYTE != 0) |
| 190 | | | { |
| 191 | 100 | | throw new IllegalArgumentException( |
| 192 | | | "The length of a hex string must be a multiple of 2 (was " |
| 193 | | | + s.length() + ")"); |
| 194 | | | } |
| 195 | 100 | | int count = 0; |
| 196 | 100 | | result = new byte[s.length() / CHARS_PER_BYTE]; |
| 197 | | | try |
| 198 | | | { |
| 199 | 100 | | for (int i = 0; i < s.length(); i++) |
| 200 | | | { |
| 201 | 100 | | final char c1 = s.charAt(i); |
| 202 | 100 | | final char c2 = s.charAt(++i); |
| 203 | 100 | | final int b = CHAR_TO_NIBBLE_HIGH[c1] | CHAR_TO_NIBBLE_LOW[c2]; |
| 204 | 100 | | if (b == -1) |
| 205 | | | { |
| 206 | 100 | | throw new IllegalArgumentException( |
| 207 | | | "'" + c1 + c2 |
| 208 | | | + "' is not a valid hex representation of a byte"); |
| 209 | | | } |
| 210 | 100 | | result[count] = (byte) b; |
| 211 | 100 | | ++count; |
| 212 | | | } |
| 213 | | | } |
| 214 | 0 | | catch (IndexOutOfBoundsException ex) |
| 215 | | | { |
| 216 | 0 | | final char c1 = s.charAt(count * CHARS_PER_BYTE); |
| 217 | 0 | | final char c2 = s.charAt(count * CHARS_PER_BYTE + 1); |
| 218 | 0 | | final IllegalArgumentException e = new IllegalArgumentException( |
| 219 | | | "'" + c1 + c2 |
| 220 | | | + "' is not a valid hex representation of a byte"); |
| 221 | 0 | | e.initCause(ex); |
| 222 | 0 | | throw e; |
| 223 | 100 | | } |
| 224 | | | } |
| 225 | 100 | | return result; |
| 226 | | | } |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | | | |
| 232 | | | |
| 233 | | | |
| 234 | | | |
| 235 | | | |
| 236 | | | @param |
| 237 | | | @return |
| 238 | | | |
| 239 | | | public static String dump (final byte[] data) |
| 240 | | | { |
| 241 | | | final String result; |
| 242 | 100 | | if (data == null) |
| 243 | | | { |
| 244 | 100 | | result = null; |
| 245 | | | } |
| 246 | | | else |
| 247 | | | { |
| 248 | 100 | | int offset = 0; |
| 249 | 100 | | final StringBuffer dumpBuffer = new StringBuffer(); |
| 250 | 100 | | final StringBuffer lineBuffer = new StringBuffer(); |
| 251 | 100 | | final StringBuffer charBuffer = new StringBuffer(); |
| 252 | 100 | | while (offset < data.length) |
| 253 | | | { |
| 254 | 100 | | lineBuffer.setLength(0); |
| 255 | 100 | | charBuffer.setLength(0); |
| 256 | 100 | | charBuffer.append('|'); |
| 257 | 100 | | lineBuffer.append(offsetToHex(offset)); |
| 258 | 100 | | lineBuffer.append(' '); |
| 259 | 100 | | final int end = (offset + DUMP_BYTES_PER_LINE < data.length |
| 260 | | | ? offset + DUMP_BYTES_PER_LINE |
| 261 | | | : data.length); |
| 262 | 100 | | for (int i = offset; i < end; i++) |
| 263 | | | { |
| 264 | 100 | | final byte b = data[i]; |
| 265 | 100 | | lineBuffer.append(bytesToHex(new byte[] {b})); |
| 266 | 100 | | lineBuffer.append(' '); |
| 267 | | | |
| 268 | 100 | | if (i - offset == DUMP_BYTES_PER_COLUMN - 1) |
| 269 | | | { |
| 270 | 100 | | lineBuffer.append(' '); |
| 271 | | | } |
| 272 | 100 | | final char c = (char) b; |
| 273 | 100 | | if ((! Character.isISOControl(c)) |
| 274 | | | && StringUtil.isAscii(c)) |
| 275 | | | { |
| 276 | 100 | | charBuffer.append(c); |
| 277 | | | } |
| 278 | | | else |
| 279 | | | { |
| 280 | 100 | | charBuffer.append('.'); |
| 281 | | | } |
| 282 | | | } |
| 283 | 100 | | padBuffer(lineBuffer); |
| 284 | 100 | | charBuffer.append('|'); |
| 285 | 100 | | dumpBuffer.append(lineBuffer); |
| 286 | 100 | | dumpBuffer.append(charBuffer); |
| 287 | 100 | | dumpBuffer.append(Constants.LINE_SEPARATOR); |
| 288 | 100 | | offset += DUMP_BYTES_PER_LINE; |
| 289 | 100 | | } |
| 290 | 100 | | result = dumpBuffer.toString(); |
| 291 | | | } |
| 292 | 100 | | return result; |
| 293 | | | } |
| 294 | | | |
| 295 | | | private static void padBuffer (final StringBuffer sbuf) |
| 296 | | | { |
| 297 | 100 | | while (sbuf.length() < DUMP_BUFFER_SIZE) |
| 298 | | | { |
| 299 | 100 | | sbuf.append(' '); |
| 300 | | | } |
| 301 | 100 | | } |
| 302 | | | |
| 303 | | | private static String offsetToHex (int offset) |
| 304 | | | { |
| 305 | 100 | | String s = Integer.toHexString(offset); |
| 306 | 100 | | while (s.length() < DUMP_ADDR_LEN) |
| 307 | | | { |
| 308 | 100 | (1) | s = "0" + s; |
| 309 | | | } |
| 310 | 100 | | return s; |
| 311 | | | } |
| 312 | | | } |