1 17 package org.alfresco.filesys.util; 18 19 23 public final class DataPacker 24 { 25 26 29 private static boolean bigEndian = false; 30 31 36 public final static boolean isBigEndian() 37 { 38 return bigEndian; 39 } 40 41 51 public final static String getDataString(char typ, byte[] bytarray, int pos, int maxlen, boolean uni) 52 { 53 54 56 if (bytarray[pos++] == (byte) typ) 57 { 58 59 61 if (uni == true) 62 return getUnicodeString(bytarray, wordAlign(pos), maxlen / 2); 63 else 64 return getString(bytarray, pos, maxlen - 1); 65 } 66 67 69 return null; 70 } 71 72 80 public final static int getInt(byte[] buf, int pos) throws java.lang.IndexOutOfBoundsException 81 { 82 83 85 if (buf.length < pos + 3) 86 throw new java.lang.IndexOutOfBoundsException (); 87 88 90 int i1 = (int) buf[pos] & 0xFF; 91 int i2 = (int) buf[pos + 1] & 0xFF; 92 int i3 = (int) buf[pos + 2] & 0xFF; 93 int i4 = (int) buf[pos + 3] & 0xFF; 94 95 int iVal = (i1 << 24) + (i2 << 16) + (i3 << 8) + i4; 96 97 99 return iVal; 100 } 101 102 110 public final static int getIntelInt(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 111 { 112 113 115 if (bytarray.length < pos + 3) 116 throw new java.lang.IndexOutOfBoundsException (); 117 118 120 int iVal = 0; 121 122 124 int i1 = (int) bytarray[pos + 3] & 0xFF; 125 int i2 = (int) bytarray[pos + 2] & 0xFF; 126 int i3 = (int) bytarray[pos + 1] & 0xFF; 127 int i4 = (int) bytarray[pos] & 0xFF; 128 129 iVal = (i1 << 24) + (i2 << 16) + (i3 << 8) + i4; 130 131 133 return iVal; 134 } 135 136 144 public final static long getLong(byte[] buf, int pos) throws java.lang.IndexOutOfBoundsException 145 { 146 147 149 if (buf.length < pos + 7) 150 throw new java.lang.IndexOutOfBoundsException (); 151 152 154 long lVal = 0L; 155 156 for (int i = 0; i < 8; i++) 157 { 158 159 161 long curVal = (long) buf[pos + i] & 0xFF; 162 curVal = curVal << ((7 - i) * 8); 163 lVal += curVal; 164 } 165 166 168 return lVal; 169 } 170 171 179 public final static long getIntelLong(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 180 { 181 182 184 if (bytarray.length < pos + 7) 185 throw new java.lang.IndexOutOfBoundsException (); 186 187 189 long lVal = 0L; 190 191 for (int i = 0; i < 8; i++) 192 { 193 194 196 long curVal = (long) bytarray[pos + i] & 0xFF; 197 curVal = curVal << (i * 8); 198 lVal += curVal; 199 } 200 201 203 return lVal; 204 } 205 206 214 public final static int getIntelShort(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 215 { 216 217 219 if (bytarray.length < pos) 220 throw new java.lang.IndexOutOfBoundsException (); 221 222 224 int sVal = (((int) bytarray[pos + 1] << 8) + ((int) bytarray[pos] & 0xFF)); 225 226 228 return sVal & 0xFFFF; 229 } 230 231 239 public final static int getShort(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 240 { 241 242 244 if (bytarray.length < pos) 245 throw new java.lang.IndexOutOfBoundsException (); 246 247 249 int sVal = 0; 250 251 if (bigEndian == true) 252 { 253 254 256 sVal = ((((int) bytarray[pos + 1]) << 8) + ((int) bytarray[pos] & 0xFF)); 257 } 258 else 259 { 260 261 263 sVal = ((((int) bytarray[pos]) << 8) + ((int) bytarray[pos + 1] & 0xFF)); 264 } 265 266 268 return sVal & 0xFFFF; 269 } 270 271 279 public final static String getString(byte[] bytarray, int pos, int maxlen) 280 { 281 282 284 int maxpos = pos + maxlen; 285 int endpos = pos; 286 287 while (bytarray[endpos] != 0x00 && endpos < maxpos) 288 endpos++; 289 290 292 if (endpos <= maxpos) 293 return new String (bytarray, pos, endpos - pos); 294 return null; 295 } 296 297 306 public final static String getString(byte[] bytarray, int pos, int maxlen, boolean isUni) 307 { 308 309 311 String str = null; 312 313 if (isUni) 314 str = getUnicodeString(bytarray, pos, maxlen); 315 else 316 str = getString(bytarray, pos, maxlen); 317 318 320 return str; 321 } 322 323 331 public final static String getUnicodeString(byte[] byt, int pos, int maxlen) 332 { 333 334 336 if (maxlen == 0) 337 return ""; 338 339 341 int maxpos = pos + (maxlen * 2); 342 int endpos = pos; 343 char[] chars = new char[maxlen]; 344 int cpos = 0; 345 char curChar; 346 347 do 348 { 349 350 352 curChar = (char) (((byt[endpos + 1] & 0xFF) << 8) + (byt[endpos] & 0xFF)); 353 354 356 chars[cpos++] = curChar; 357 358 360 endpos += 2; 361 362 } while (curChar != 0 && endpos < maxpos); 363 364 366 if (endpos <= maxpos) 367 { 368 if (curChar == 0) 369 cpos--; 370 return new String (chars, 0, cpos); 371 } 372 return null; 373 } 374 375 383 public final static void putInt(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 384 { 385 386 388 if (bytarray.length < pos + 3) 389 throw new java.lang.IndexOutOfBoundsException (); 390 391 393 bytarray[pos] = (byte) ((val >> 24) & 0xFF); 394 bytarray[pos + 1] = (byte) ((val >> 16) & 0xFF); 395 bytarray[pos + 2] = (byte) ((val >> 8) & 0xFF); 396 bytarray[pos + 3] = (byte) (val & 0xFF); 397 } 398 399 407 public final static void putIntelInt(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 408 { 409 410 412 if (bytarray.length < pos + 3) 413 throw new java.lang.IndexOutOfBoundsException (); 414 415 417 bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF); 418 bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF); 419 bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF); 420 bytarray[pos] = (byte) (val & 0xFF); 421 } 422 423 431 public final static void putLong(long val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 432 { 433 434 436 if (bytarray.length < pos + 7) 437 throw new java.lang.IndexOutOfBoundsException (); 438 439 441 bytarray[pos] = (byte) ((val >> 56) & 0xFF); 442 bytarray[pos + 1] = (byte) ((val >> 48) & 0xFF); 443 bytarray[pos + 2] = (byte) ((val >> 40) & 0xFF); 444 bytarray[pos + 3] = (byte) ((val >> 32) & 0xFF); 445 bytarray[pos + 4] = (byte) ((val >> 24) & 0xFF); 446 bytarray[pos + 5] = (byte) ((val >> 16) & 0xFF); 447 bytarray[pos + 6] = (byte) ((val >> 8) & 0xFF); 448 bytarray[pos + 7] = (byte) (val & 0xFF); 449 } 450 451 459 public final static void putIntelLong(long val, byte[] bytarray, int pos) 460 throws java.lang.IndexOutOfBoundsException 461 { 462 463 465 if (bytarray.length < pos + 7) 466 throw new java.lang.IndexOutOfBoundsException (); 467 468 470 bytarray[pos + 7] = (byte) ((val >> 56) & 0xFF); 471 bytarray[pos + 6] = (byte) ((val >> 48) & 0xFF); 472 bytarray[pos + 5] = (byte) ((val >> 40) & 0xFF); 473 bytarray[pos + 4] = (byte) ((val >> 32) & 0xFF); 474 bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF); 475 bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF); 476 bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF); 477 bytarray[pos] = (byte) (val & 0xFF); 478 } 479 480 488 public final static void putIntelLong(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 489 { 490 491 493 if (bytarray.length < pos + 7) 494 throw new java.lang.IndexOutOfBoundsException (); 495 496 498 bytarray[pos + 7] = (byte) 0; 499 bytarray[pos + 6] = (byte) 0; 500 bytarray[pos + 5] = (byte) 0; 501 bytarray[pos + 4] = (byte) 0; 502 bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF); 503 bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF); 504 bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF); 505 bytarray[pos] = (byte) (val & 0xFF); 506 } 507 508 516 public final static void putIntelShort(int val, byte[] bytarray, int pos) 517 throws java.lang.IndexOutOfBoundsException 518 { 519 520 522 if (bytarray.length < pos) 523 throw new java.lang.IndexOutOfBoundsException (); 524 525 527 bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF); 528 bytarray[pos] = (byte) (val & 0xFF); 529 } 530 531 539 public final static void putShort(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException 540 { 541 542 544 if (bytarray.length < pos) 545 throw new java.lang.IndexOutOfBoundsException (); 546 547 549 bytarray[pos] = (byte) ((val >> 8) & 0xFF); 550 bytarray[pos + 1] = (byte) (val & 0xFF); 551 } 552 553 562 public final static int putString(String str, byte[] bytarray, int pos, boolean nullterm) 563 { 564 565 567 byte[] byts = str.getBytes(); 568 569 571 int bufpos = pos; 572 573 for (int i = 0; i < byts.length; i++) 574 bytarray[bufpos++] = byts[i]; 575 576 578 if (nullterm == true) 579 bytarray[bufpos++] = 0; 580 581 583 return bufpos; 584 } 585 586 595 public final static int putString(String str, int fldLen, byte[] bytarray, int pos) 596 { 597 598 600 byte[] byts = str.getBytes(); 601 602 604 int bufpos = pos; 605 int idx = 0; 606 607 while (idx < fldLen) 608 { 609 if (idx < byts.length) 610 bytarray[bufpos++] = byts[idx]; 611 else 612 bytarray[bufpos++] = (byte) 0; 613 idx++; 614 } 615 616 618 return bufpos; 619 } 620 621 631 public final static int putString(String str, byte[] bytarray, int pos, boolean nullterm, boolean isUni) 632 { 633 634 636 int newpos = -1; 637 638 if (isUni) 639 newpos = putUnicodeString(str, bytarray, pos, nullterm); 640 else 641 newpos = putString(str, bytarray, pos, nullterm); 642 643 645 return newpos; 646 } 647 648 657 public final static int putUnicodeString(String str, byte[] bytarray, int pos, boolean nullterm) 658 { 659 660 662 int bufpos = pos; 663 664 for (int i = 0; i < str.length(); i++) 665 { 666 667 669 char ch = str.charAt(i); 670 671 673 bytarray[bufpos++] = (byte) (ch & 0xFF); 674 bytarray[bufpos++] = (byte) ((ch & 0xFF00) >> 8); 675 } 676 677 679 if (nullterm == true) 680 { 681 bytarray[bufpos++] = 0; 682 bytarray[bufpos++] = 0; 683 } 684 685 687 return bufpos; 688 } 689 690 698 public final static void putZeros(byte[] buf, int pos, int cnt) throws java.lang.ArrayIndexOutOfBoundsException 699 { 700 701 703 if (buf.length < (pos + cnt)) 704 throw new java.lang.ArrayIndexOutOfBoundsException (); 705 706 708 for (int i = 0; i < cnt; i++) 709 buf[pos + i] = 0; 710 } 711 712 718 public final static int wordAlign(int pos) 719 { 720 return (pos + 1) & 0xFFFFFFFE; 721 } 722 723 729 public final static int longwordAlign(int pos) 730 { 731 return (pos + 3) & 0xFFFFFFFC; 732 } 733 734 742 public final static int getStringLength(String str, boolean uni, boolean nul) 743 { 744 745 747 int len = str.length(); 748 if (nul) 749 len += 1; 750 if (uni) 751 len *= 2; 752 753 return len; 754 } 755 756 765 public final static int getBufferPosition(int pos, String str, boolean uni, boolean nul) 766 { 767 768 770 int len = str.length(); 771 if (nul) 772 len += 1; 773 if (uni) 774 len *= 2; 775 776 return pos + len; 777 } 778 } | Popular Tags |