1 17 package org.alfresco.filesys.util; 18 19 24 public class DataBuffer 25 { 26 27 29 private static final int DefaultBufferSize = 256; 30 31 33 private byte[] m_data; 34 private int m_pos; 35 private int m_endpos; 36 private int m_offset; 37 38 41 public DataBuffer() 42 { 43 m_data = new byte[DefaultBufferSize]; 44 m_pos = 0; 45 m_offset = 0; 46 } 47 48 53 public DataBuffer(int siz) 54 { 55 m_data = new byte[siz]; 56 m_pos = 0; 57 m_offset = 0; 58 } 59 60 67 public DataBuffer(byte[] buf, int off, int len) 68 { 69 m_data = buf; 70 m_offset = off; 71 m_pos = off; 72 m_endpos = off + len; 73 } 74 75 80 public final byte[] getBuffer() 81 { 82 return m_data; 83 } 84 85 90 public final int getLength() 91 { 92 if (m_endpos != 0) 93 return m_endpos - m_offset; 94 return m_pos - m_offset; 95 } 96 97 102 public final int getLengthInWords() 103 { 104 return getLength() / 2; 105 } 106 107 112 public final int getAvailableLength() 113 { 114 if (m_endpos == 0) 115 return -1; 116 return m_endpos - m_pos; 117 } 118 119 124 public final int getDisplacement() 125 { 126 return m_pos - m_offset; 127 } 128 129 134 public final int getOffset() 135 { 136 return m_offset; 137 } 138 139 144 public final int getByte() 145 { 146 147 149 if (m_data.length - m_pos < 1) 150 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 151 152 154 int bval = (int) (m_data[m_pos] & 0xFF); 155 m_pos++; 156 return bval; 157 } 158 159 164 public final int getShort() 165 { 166 167 169 if (m_data.length - m_pos < 2) 170 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 171 172 174 int sval = (int) DataPacker.getIntelShort(m_data, m_pos); 175 m_pos += 2; 176 return sval; 177 } 178 179 184 public final int getInt() 185 { 186 187 189 if (m_data.length - m_pos < 4) 190 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 191 192 194 int ival = DataPacker.getIntelInt(m_data, m_pos); 195 m_pos += 4; 196 return ival; 197 } 198 199 204 public final long getLong() 205 { 206 207 209 if (m_data.length - m_pos < 8) 210 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 211 212 214 long lval = DataPacker.getIntelLong(m_data, m_pos); 215 m_pos += 8; 216 return lval; 217 } 218 219 225 public final String getString(boolean uni) 226 { 227 return getString(255, uni); 228 } 229 230 237 public final String getString(int maxlen, boolean uni) 238 { 239 240 242 String ret = null; 243 int availLen = -1; 244 245 if (uni) 246 { 247 248 251 m_pos = DataPacker.wordAlign(m_pos); 252 availLen = (m_endpos - m_pos) / 2; 253 if (availLen < maxlen) 254 maxlen = availLen; 255 256 ret = DataPacker.getUnicodeString(m_data, m_pos, maxlen); 257 if (ret != null) { 258 if ( ret.length() < maxlen) 259 m_pos += (ret.length() * 2) + 2; 260 else 261 m_pos += maxlen * 2; 262 } 263 } 264 else 265 { 266 267 269 availLen = m_endpos - m_pos; 270 if (availLen < maxlen) 271 maxlen = availLen; 272 273 275 ret = DataPacker.getString(m_data, m_pos, maxlen); 276 if (ret != null) { 277 if ( ret.length() < maxlen) 278 m_pos += ret.length() + 1; 279 else 280 m_pos += maxlen; 281 } 282 } 283 284 286 return ret; 287 } 288 289 295 public final int getShortAt(int idx) 296 { 297 298 300 int pos = m_offset + (idx * 2); 301 if (m_data.length - pos < 2) 302 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 303 304 306 int sval = (int) DataPacker.getIntelShort(m_data, pos) & 0xFFFF; 307 return sval; 308 } 309 310 316 public final int getIntAt(int idx) 317 { 318 319 321 int pos = m_offset + (idx * 2); 322 if (m_data.length - pos < 4) 323 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 324 325 327 int ival = DataPacker.getIntelInt(m_data, pos); 328 return ival; 329 } 330 331 337 public final long getLongAt(int idx) 338 { 339 340 342 int pos = m_offset + (idx * 2); 343 if (m_data.length - pos < 8) 344 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 345 346 348 long lval = DataPacker.getIntelLong(m_data, pos); 349 return lval; 350 } 351 352 357 public final void skipBytes(int cnt) 358 { 359 360 362 if (m_data.length - m_pos < cnt) 363 throw new ArrayIndexOutOfBoundsException ("End of data buffer"); 364 365 367 m_pos += cnt; 368 } 369 370 375 public final int getPosition() 376 { 377 return m_pos; 378 } 379 380 385 public final void setPosition(int pos) 386 { 387 m_pos = pos; 388 } 389 390 393 public final void setEndOfBuffer() 394 { 395 m_endpos = m_pos; 396 m_pos = m_offset; 397 } 398 399 404 public final void setLength(int len) 405 { 406 m_pos = m_offset + len; 407 } 408 409 414 public final void putByte(int bval) 415 { 416 417 419 if (m_data.length - m_pos < 1) 420 extendBuffer(); 421 422 424 m_data[m_pos++] = (byte) (bval & 0xFF); 425 } 426 427 432 public final void putShort(int sval) 433 { 434 435 437 if (m_data.length - m_pos < 2) 438 extendBuffer(); 439 440 442 DataPacker.putIntelShort(sval, m_data, m_pos); 443 m_pos += 2; 444 } 445 446 451 public final void putInt(int ival) 452 { 453 454 456 if (m_data.length - m_pos < 4) 457 extendBuffer(); 458 459 461 DataPacker.putIntelInt(ival, m_data, m_pos); 462 m_pos += 4; 463 } 464 465 470 public final void putLong(long lval) 471 { 472 473 475 if (m_data.length - m_pos < 8) 476 extendBuffer(); 477 478 480 DataPacker.putIntelLong(lval, m_data, m_pos); 481 m_pos += 8; 482 } 483 484 490 public final void putShortAt(int idx, int sval) 491 { 492 493 495 int pos = m_offset + (idx * 2); 496 if (m_data.length - pos < 2) 497 extendBuffer(); 498 499 501 DataPacker.putIntelShort(sval, m_data, pos); 502 } 503 504 510 public final void putIntAt(int idx, int ival) 511 { 512 513 515 int pos = m_offset = (idx * 2); 516 if (m_data.length - pos < 4) 517 extendBuffer(); 518 519 521 DataPacker.putIntelInt(ival, m_data, pos); 522 } 523 524 530 public final void putLongAt(int idx, int lval) 531 { 532 533 535 int pos = m_offset = (idx * 2); 536 if (m_data.length - pos < 8) 537 extendBuffer(); 538 539 541 DataPacker.putIntelLong(lval, m_data, pos); 542 } 543 544 550 public final void putString(String str, boolean uni) 551 { 552 putString(str, uni, true); 553 } 554 555 562 public final void putString(String str, boolean uni, boolean nulTerm) 563 { 564 565 567 if (uni) 568 { 569 570 572 int bytLen = str.length() * 2; 573 if (m_data.length - m_pos < bytLen) 574 extendBuffer(bytLen + 4); 575 576 578 m_pos = DataPacker.wordAlign(m_pos); 579 DataPacker.putUnicodeString(str, m_data, m_pos, nulTerm); 580 m_pos += (str.length() * 2); 581 if (nulTerm) 582 m_pos += 2; 583 } 584 else 585 { 586 587 589 if (m_data.length - m_pos < str.length()) 590 extendBuffer(str.length() + 2); 591 592 594 DataPacker.putString(str, m_data, m_pos, nulTerm); 595 m_pos += str.length(); 596 if (nulTerm) 597 m_pos++; 598 } 599 } 600 601 607 public final void putFixedString(String str, int len) 608 { 609 610 612 if (m_data.length - m_pos < str.length()) 613 extendBuffer(str.length() + 2); 614 615 617 DataPacker.putString(str, len, m_data, m_pos); 618 m_pos += len; 619 } 620 621 630 public final int putStringAt(String str, int pos, boolean uni, boolean nulTerm) 631 { 632 633 635 int retPos = -1; 636 637 if (uni) 638 { 639 640 642 int bytLen = str.length() * 2; 643 if (m_data.length - pos < bytLen) 644 extendBuffer(bytLen + 4); 645 646 648 pos = DataPacker.wordAlign(pos); 649 retPos = DataPacker.putUnicodeString(str, m_data, pos, nulTerm); 650 } 651 else 652 { 653 654 656 if (m_data.length - pos < str.length()) 657 extendBuffer(str.length() + 2); 658 659 661 retPos = DataPacker.putString(str, m_data, pos, nulTerm); 662 } 663 664 666 return retPos; 667 } 668 669 677 public final int putFixedStringAt(String str, int len, int pos) 678 { 679 680 682 if (m_data.length - pos < str.length()) 683 extendBuffer(str.length() + 2); 684 685 687 return DataPacker.putString(str, len, m_data, pos); 688 } 689 690 695 public final void putStringPointer(int off) 696 { 697 698 701 DataPacker.putIntelInt(off - m_offset, m_data, m_pos); 702 m_pos += 4; 703 } 704 705 710 public final void putZeros(int cnt) 711 { 712 713 715 if (m_data.length - m_pos < cnt) 716 extendBuffer(cnt); 717 718 720 for (int i = 0; i < cnt; i++) 721 m_data[m_pos++] = 0; 722 } 723 724 727 public final void wordAlign() 728 { 729 m_pos = DataPacker.wordAlign(m_pos); 730 } 731 732 735 public final void longwordAlign() 736 { 737 m_pos = DataPacker.longwordAlign(m_pos); 738 } 739 740 747 public final void appendData(byte[] buf, int off, int len) 748 { 749 750 752 if (m_data.length - m_pos < len) 753 extendBuffer(len); 754 755 757 System.arraycopy(buf, off, m_data, m_pos, len); 758 m_pos += len; 759 } 760 761 768 public final int copyData(byte[] buf, int off) 769 { 770 return copyData(buf, off, getLength()); 771 } 772 773 781 public final int copyData(byte[] buf, int off, int cnt) 782 { 783 784 786 if (m_pos == m_endpos) 787 return 0; 788 789 791 int siz = m_endpos - m_pos; 792 if (siz > cnt) 793 siz = cnt; 794 795 797 System.arraycopy(m_data, m_pos, buf, off, siz); 798 m_pos += siz; 799 800 802 return siz; 803 } 804 805 810 private final void extendBuffer(int ext) 811 { 812 813 815 byte[] newBuf = new byte[m_data.length + ext]; 816 817 819 System.arraycopy(m_data, 0, newBuf, 0, m_data.length); 820 821 823 m_data = newBuf; 824 } 825 826 829 private final void extendBuffer() 830 { 831 extendBuffer(m_data.length * 2); 832 } 833 834 839 public String toString() 840 { 841 StringBuffer str = new StringBuffer (); 842 843 str.append("[data="); 844 str.append(m_data); 845 str.append(","); 846 str.append(m_pos); 847 str.append("/"); 848 str.append(m_offset); 849 str.append("/"); 850 str.append(getLength()); 851 str.append("]"); 852 853 return str.toString(); 854 } 855 } 856 | Popular Tags |