| 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 | |
package com.healthmarketscience.jackcess; |
| 29 | |
|
| 30 | |
import java.io.FileWriter; |
| 31 | |
import java.io.IOException; |
| 32 | |
import java.io.PrintWriter; |
| 33 | |
import java.nio.ByteBuffer; |
| 34 | |
import java.nio.ByteOrder; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public final class ByteUtil { |
| 41 | |
|
| 42 | 1 | private static final String[] HEX_CHARS = new String[] { |
| 43 | |
"0", "1", "2", "3", "4", "5", "6", "7", |
| 44 | |
"8", "9", "A", "B", "C", "D", "E", "F"}; |
| 45 | |
|
| 46 | 0 | private ByteUtil() {} |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static void put3ByteInt(ByteBuffer buffer, int val) |
| 55 | |
{ |
| 56 | 1358 | put3ByteInt(buffer, val, buffer.order()); |
| 57 | 1358 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public static void put3ByteInt(ByteBuffer buffer, int val, ByteOrder order) |
| 67 | |
{ |
| 68 | 46874 | int pos = buffer.position(); |
| 69 | 46874 | put3ByteInt(buffer, val, pos, order); |
| 70 | 46874 | buffer.position(pos + 3); |
| 71 | 46874 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public static void put3ByteInt(ByteBuffer buffer, int val, int offset, |
| 82 | |
ByteOrder order) { |
| 83 | |
|
| 84 | 46874 | int offInc = 1; |
| 85 | 46874 | if(order == ByteOrder.BIG_ENDIAN) { |
| 86 | 45516 | offInc = -1; |
| 87 | 45516 | offset += 2; |
| 88 | |
} |
| 89 | |
|
| 90 | 46874 | buffer.put(offset, (byte) (val & 0xFF)); |
| 91 | 46874 | buffer.put(offset + (1 * offInc), (byte) ((val >>> 8) & 0xFF)); |
| 92 | 46874 | buffer.put(offset + (2 * offInc), (byte) ((val >>> 16) & 0xFF)); |
| 93 | 46874 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public static int get3ByteInt(ByteBuffer buffer) { |
| 101 | 1180 | return get3ByteInt(buffer, buffer.order()); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public static int get3ByteInt(ByteBuffer buffer, ByteOrder order) { |
| 111 | 9010 | int pos = buffer.position(); |
| 112 | 9010 | int rtn = get3ByteInt(buffer, pos, order); |
| 113 | 9010 | buffer.position(pos + 3); |
| 114 | 9010 | return rtn; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public static int get3ByteInt(ByteBuffer buffer, int offset) { |
| 124 | 814 | return get3ByteInt(buffer, offset, buffer.order()); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public static int get3ByteInt(ByteBuffer buffer, int offset, |
| 135 | |
ByteOrder order) { |
| 136 | |
|
| 137 | 9824 | int offInc = 1; |
| 138 | 9824 | if(order == ByteOrder.BIG_ENDIAN) { |
| 139 | 7830 | offInc = -1; |
| 140 | 7830 | offset += 2; |
| 141 | |
} |
| 142 | |
|
| 143 | 9824 | int rtn = getUnsignedByte(buffer, offset); |
| 144 | 9824 | rtn += (getUnsignedByte(buffer, offset + (1 * offInc)) << 8); |
| 145 | 9824 | rtn += (getUnsignedByte(buffer, offset + (2 * offInc)) << 16); |
| 146 | 9824 | return rtn; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public static int getUnsignedByte(ByteBuffer buffer) { |
| 155 | 8409 | int pos = buffer.position(); |
| 156 | 8409 | int rtn = getUnsignedByte(buffer, pos); |
| 157 | 8409 | buffer.position(pos + 1); |
| 158 | 8409 | return rtn; |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public static int getUnsignedByte(ByteBuffer buffer, int offset) { |
| 168 | 38428 | return asUnsignedByte(buffer.get(offset)); |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public static int getUnsignedShort(ByteBuffer buffer) { |
| 177 | 2565 | int pos = buffer.position(); |
| 178 | 2565 | int rtn = getUnsignedShort(buffer, pos); |
| 179 | 2565 | buffer.position(pos + 2); |
| 180 | 2565 | return rtn; |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
public static int getUnsignedShort(ByteBuffer buffer, int offset) { |
| 190 | 2994 | return asUnsignedShort(buffer.getShort(offset)); |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public static int getInt(ByteBuffer buffer, ByteOrder order) { |
| 201 | 209 | int offset = buffer.position(); |
| 202 | 209 | int rtn = getInt(buffer, offset, order); |
| 203 | 209 | buffer.position(offset + 4); |
| 204 | 209 | return rtn; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public static int getInt(ByteBuffer buffer, int offset, ByteOrder order) { |
| 215 | 209 | ByteOrder origOrder = buffer.order(); |
| 216 | |
try { |
| 217 | 209 | return buffer.order(order).getInt(offset); |
| 218 | |
} finally { |
| 219 | 209 | buffer.order(origOrder); |
| 220 | |
} |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
public static void putInt(ByteBuffer buffer, int val, ByteOrder order) { |
| 231 | 4945 | int offset = buffer.position(); |
| 232 | 4945 | putInt(buffer, val, offset, order); |
| 233 | 4945 | buffer.position(offset + 4); |
| 234 | 4945 | } |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public static void putInt(ByteBuffer buffer, int val, int offset, |
| 245 | |
ByteOrder order) |
| 246 | |
{ |
| 247 | 4945 | ByteOrder origOrder = buffer.order(); |
| 248 | |
try { |
| 249 | 4945 | buffer.order(order).putInt(offset, val); |
| 250 | |
} finally { |
| 251 | 4945 | buffer.order(origOrder); |
| 252 | 4945 | } |
| 253 | 4945 | } |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public static void clearRange(ByteBuffer buffer, int start, |
| 259 | |
int end) |
| 260 | |
{ |
| 261 | 5 | putRange(buffer, start, end, (byte)0x00); |
| 262 | 5 | } |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public static void fillRange(ByteBuffer buffer, int start, |
| 268 | |
int end) |
| 269 | |
{ |
| 270 | 0 | putRange(buffer, start, end, (byte)0xff); |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
public static void putRange(ByteBuffer buffer, int start, |
| 277 | |
int end, byte b) |
| 278 | |
{ |
| 279 | 345 | for(int i = start; i < end; ++i) { |
| 280 | 340 | buffer.put(i, b); |
| 281 | |
} |
| 282 | 5 | } |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
public static boolean matchesRange(ByteBuffer buffer, int start, |
| 289 | |
byte[] pattern) |
| 290 | |
{ |
| 291 | 0 | for(int i = 0; i < pattern.length; ++i) { |
| 292 | 0 | if(pattern[i] != buffer.get(start + i)) { |
| 293 | 0 | return false; |
| 294 | |
} |
| 295 | |
} |
| 296 | 0 | return true; |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
public static String toHexString(ByteBuffer buffer, int size) { |
| 306 | 3608 | return toHexString(buffer, 0, size); |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
public static String toHexString(byte[] array) { |
| 315 | 0 | return toHexString(ByteBuffer.wrap(array), 0, array.length); |
| 316 | |
} |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
public static String toHexString(ByteBuffer buffer, int offset, int size) { |
| 326 | 3608 | return toHexString(buffer, offset, size, true); |
| 327 | |
} |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public static String toHexString(ByteBuffer buffer, |
| 338 | |
int offset, int size, boolean formatted) { |
| 339 | |
|
| 340 | 3633 | StringBuilder rtn = new StringBuilder(); |
| 341 | 3633 | int position = buffer.position(); |
| 342 | 3633 | buffer.position(offset); |
| 343 | |
|
| 344 | 39821 | for (int i = 0; i < size; i++) { |
| 345 | 36188 | byte b = buffer.get(); |
| 346 | 36188 | byte h = (byte) (b & 0xF0); |
| 347 | 36188 | h = (byte) (h >>> 4); |
| 348 | 36188 | h = (byte) (h & 0x0F); |
| 349 | 36188 | rtn.append(HEX_CHARS[h]); |
| 350 | 36188 | h = (byte) (b & 0x0F); |
| 351 | 36188 | rtn.append(HEX_CHARS[h]); |
| 352 | |
|
| 353 | 36188 | if (formatted == true) |
| 354 | |
{ |
| 355 | 36108 | rtn.append(" "); |
| 356 | |
|
| 357 | 36108 | if ((i + 1) % 4 == 0) { |
| 358 | 7834 | rtn.append(" "); |
| 359 | |
} |
| 360 | 36108 | if ((i + 1) % 24 == 0) { |
| 361 | 148 | rtn.append("\n"); |
| 362 | |
} |
| 363 | |
} |
| 364 | |
} |
| 365 | |
|
| 366 | 3633 | buffer.position(position); |
| 367 | 3633 | return rtn.toString(); |
| 368 | |
} |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
public static void writeHexString(ByteBuffer buffer, |
| 375 | |
String hexStr) |
| 376 | |
throws IOException |
| 377 | |
{ |
| 378 | 25 | char[] hexChars = hexStr.toCharArray(); |
| 379 | 25 | if((hexChars.length % 2) != 0) { |
| 380 | 0 | throw new IOException("Hex string length must be even"); |
| 381 | |
} |
| 382 | 105 | for(int i = 0; i < hexChars.length; i += 2) { |
| 383 | 80 | String tmpStr = new String(hexChars, i, 2); |
| 384 | 80 | buffer.put((byte)Long.parseLong(tmpStr, 16)); |
| 385 | |
} |
| 386 | 25 | } |
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
public static void toHexFile( |
| 392 | |
String fileName, |
| 393 | |
ByteBuffer buffer, |
| 394 | |
int offset, int size) |
| 395 | |
throws IOException |
| 396 | |
{ |
| 397 | 0 | PrintWriter writer = new PrintWriter( |
| 398 | |
new FileWriter(fileName)); |
| 399 | |
try { |
| 400 | 0 | writer.println(toHexString(buffer, offset, size)); |
| 401 | |
} finally { |
| 402 | 0 | writer.close(); |
| 403 | 0 | } |
| 404 | 0 | } |
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
public static int asUnsignedByte(byte b) { |
| 410 | 290846 | return b & 0xFF; |
| 411 | |
} |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
public static int asUnsignedShort(short s) { |
| 417 | 2994 | return s & 0xFFFF; |
| 418 | |
} |
| 419 | |
|
| 420 | |
} |