1 28 29 package com.caucho.util; 30 31 import java.io.InputStream ; 32 33 36 public final class CharBuffer extends CharSegment { 37 private static final int MIN_CAPACITY = 64; 38 39 42 public CharBuffer() 43 { 44 _buffer = new char[MIN_CAPACITY]; 45 _length = 0; 46 } 47 48 53 public CharBuffer(int capacity) 54 { 55 if (capacity < 0) 56 throw new IllegalArgumentException (); 57 if (capacity < MIN_CAPACITY) 58 capacity = MIN_CAPACITY; 59 60 _buffer = new char[capacity]; 61 _length = 0; 62 } 63 64 69 public CharBuffer(String string) 70 { 71 int length = string.length(); 72 int capacity = length + MIN_CAPACITY; 73 74 _buffer = new char[capacity]; 75 _length = length; 76 string.getChars(0, length, _buffer, 0); 77 } 78 79 84 public CharBuffer(String string, int offset, int length) 85 { 86 int capacity = length; 87 if (capacity < MIN_CAPACITY) 88 capacity = MIN_CAPACITY; 89 90 _buffer = new char[capacity]; 91 _length = length; 92 string.getChars(offset, length, _buffer, 0); 93 } 94 95 public static CharBuffer allocate() 96 { 97 return new CharBuffer(); 98 } 99 100 public void free() 101 { 102 } 103 104 107 public int length() 108 { 109 return _length; 110 } 111 112 115 public int getLength() 116 { 117 return _length; 118 } 119 120 124 public int capacity() 125 { 126 return _buffer.length; 127 } 128 129 public int getCapacity() 130 { 131 return _buffer.length; 132 } 133 134 137 public final void ensureCapacity(int minimumCapacity) 138 { 139 if (minimumCapacity <= _buffer.length) { 140 return; 141 } 142 143 expandCapacity(minimumCapacity); 144 } 145 146 149 private final void expandCapacity(int minimumCapacity) 150 { 151 int oldCapacity = _buffer.length; 152 int newCapacity = oldCapacity * 2; 153 154 if (newCapacity < 0) 155 newCapacity = Integer.MAX_VALUE; 156 else if (newCapacity < minimumCapacity) 157 newCapacity = minimumCapacity; 158 159 char []chars = new char[newCapacity]; 160 161 System.arraycopy(_buffer, 0, chars, 0, oldCapacity); 162 163 _buffer = chars; 164 } 165 166 169 public final void clear() 170 { 171 _length = 0; 172 } 173 174 177 public final void setLength(int newLength) 178 { 179 if (newLength < 0) 180 throw new IndexOutOfBoundsException ("illegal argument"); 181 else if (_buffer.length < newLength) 182 expandCapacity(newLength); 183 184 _length = newLength; 185 } 186 187 190 public char charAt(int i) 191 { 192 if (i < 0 || _length <= i) 193 throw new IndexOutOfBoundsException (); 194 195 return _buffer[i]; 196 } 197 198 203 public char getLastChar() 204 { 205 if (_length == 0) 206 throw new IndexOutOfBoundsException (); 207 208 return _buffer[_length - 1]; 209 } 210 211 214 public final char []getBuffer() 215 { 216 return _buffer; 217 } 218 219 222 public void getChars(int srcBegin, int srcEnd, char []dst, int dstBegin) 223 { 224 char []buffer = _buffer; 225 while (srcBegin < srcEnd) 226 dst[dstBegin++] = buffer[srcBegin++]; 227 } 228 229 232 public void setCharAt(int index, char ch) 233 { 234 if (index < 0 || _length <= index) 235 throw new IndexOutOfBoundsException (); 236 237 _buffer[index] = ch; 238 } 239 240 243 public CharBuffer append(Object obj) 244 { 245 return append(String.valueOf(obj)); 246 } 247 248 251 public CharBuffer append(CharBuffer cb) 252 { 253 return append(cb._buffer, 0, cb._length); 254 } 255 256 259 public CharBuffer append(String string) 260 { 261 if (string == null) 262 string = "null"; 263 264 int len = string.length(); 265 int newLength = _length + len; 266 int length = _length; 267 if (_buffer.length <= newLength) 268 expandCapacity(newLength); 269 270 string.getChars(0, len, _buffer, length); 271 272 _length = newLength; 273 274 return this; 275 } 276 277 public CharBuffer append(String string, int offset, int len) 278 { 279 if (_buffer.length <= len + _length) 280 expandCapacity(len + _length); 281 282 string.getChars(offset, offset + len, _buffer, _length); 283 284 _length += len; 285 286 return this; 287 } 288 289 292 public CharBuffer append(char []buffer) 293 { 294 return append(buffer, 0, buffer.length); 295 } 296 297 300 public CharBuffer append(char []buffer, int offset, int length) 301 { 302 if (_buffer.length < _length + length) 303 expandCapacity(_length + length); 304 305 System.arraycopy(buffer, offset, _buffer, _length, length); 306 307 _length += length; 308 309 return this; 310 } 311 312 315 public final CharBuffer append(boolean b) 316 { 317 return append(String.valueOf(b)); 318 } 319 320 323 public final CharBuffer append(char ch) 324 { 325 if (_buffer.length <= _length) 326 expandCapacity(_length + 1); 327 328 _buffer[_length++] = ch; 329 330 return this; 331 } 332 333 336 public CharBuffer append(int i) 337 { 338 if (i == 0x80000000) { 339 return append("-2147483648"); 340 } 341 342 int length = _length; 343 344 if (_buffer.length <= length + 16) 345 expandCapacity(length + 16); 346 347 char []buffer = _buffer; 348 349 if (i < 0) { 350 buffer[length++] = '-'; 351 i = -i; 352 } 353 else if (i == 0) { 354 buffer[_length++] = '0'; 355 return this; 356 } 357 358 int start = length; 359 while (i > 0) { 360 buffer[length++] = (char) ((i % 10) + '0'); 361 i /= 10; 362 } 363 364 for (int j = (length - start) / 2; j > 0; j--) { 365 char temp = buffer[length - j]; 366 buffer[length - j] = buffer[start + j - 1]; 367 buffer[start + j - 1] = temp; 368 } 369 370 _length = length; 371 372 return this; 373 } 374 375 378 public CharBuffer append(long i) 379 { 380 if (i == 0x8000000000000000L) { 381 return append("-9223372036854775808"); 382 } 383 384 int length = _length; 385 386 if (_buffer.length < length + 32) 387 expandCapacity(length + 32); 388 389 char []buffer = _buffer; 390 391 if (i < 0) { 392 buffer[length++] = '-'; 393 i = -i; 394 } 395 else if (i == 0) { 396 buffer[_length++] = '0'; 397 return this; 398 } 399 400 int start = length; 401 while (i > 0) { 402 buffer[length++] = (char) ((i % 10) + '0'); 403 i /= 10; 404 } 405 406 for (int j = (length - start) / 2; j > 0; j--) { 407 char temp = buffer[length - j]; 408 buffer[length - j] = buffer[start + j - 1]; 409 buffer[start + j - 1] = temp; 410 } 411 412 _length = length; 413 414 return this; 415 } 416 417 420 public CharBuffer append(float f) 421 { 422 return append(String.valueOf(f)); 423 } 424 425 428 public CharBuffer append(double d) 429 { 430 return append(String.valueOf(d)); 431 } 432 433 436 public final CharBuffer append(byte []buf, int offset, int len) 437 { 438 int length = _length; 439 if (_buffer.length < _length + len) 440 expandCapacity(_length + len); 441 442 char []buffer = _buffer; 443 for (; len > 0; len--) 444 buffer[length++] = (char) buf[offset++]; 445 446 _length = length; 447 448 return this; 449 } 450 451 454 public CharBuffer delete(int start, int end) 455 { 456 if (start < 0 || end < start || _length < start) 457 throw new StringIndexOutOfBoundsException (); 458 459 if (_length < end) 460 end = _length; 461 462 int tail = _length - end; 463 char []buffer = _buffer; 464 465 for (int i = 0; i < tail; i++) 466 buffer[start + i] = buffer[end + i]; 467 468 _length -= end - start; 469 470 return this; 471 } 472 473 476 public CharBuffer deleteCharAt(int index) 477 { 478 if (index < 0 || _length < index) 479 throw new StringIndexOutOfBoundsException (); 480 481 if (index == _length) 482 return this; 483 484 int tail = _length - index + 1; 485 char []buffer = _buffer; 486 487 for (int i = 0; i < tail; i++) 488 buffer[index + i] = buffer[index + i + 1]; 489 490 _length--; 491 492 return this; 493 } 494 495 498 public CharBuffer replace(int start, int end, String string) 499 { 500 if (start < 0 || end < start || _length < start) 501 throw new StringIndexOutOfBoundsException (); 502 503 int len = string.length(); 504 int length = _length; 505 506 if (_buffer.length < len + length - (end - start)) 507 expandCapacity(len + length - (end - start)); 508 509 char []buffer = _buffer; 510 511 if (len < end - start) { 512 int tail = length - end; 513 for (int i = 0; i < tail; i++) 514 buffer[start + len + i] = buffer[end + i]; 515 } 516 else { 517 int tail = length - end; 518 for (int i = tail - 1; i >= 0; i--) 519 buffer[end + i] = buffer[start + len + i]; 520 } 521 522 string.getChars(0, len, buffer, start); 523 524 _length = length + len - (end - start); 525 526 return this; 527 } 528 529 532 public CharBuffer replace(int start, int end, 533 char []buffer, int offset, int len) 534 { 535 if (start < 0 || end < start || _length < start) 536 throw new StringIndexOutOfBoundsException (); 537 538 if (_buffer.length < len + _length - (end - start)) 539 expandCapacity(len + _length - (end - start)); 540 541 char []thisBuffer = _buffer; 542 543 if (len < end - start) { 544 int tail = _length - end; 545 for (int i = 0; i < tail; i++) 546 thisBuffer[start + len + i] = thisBuffer[end + i]; 547 } 548 else { 549 int tail = _length - end; 550 for (int i = tail - 1; i >= 0; i--) 551 thisBuffer[end + i] = thisBuffer[start + len + i]; 552 } 553 554 System.arraycopy(buffer, offset, thisBuffer, start, len); 555 556 _length += len - (end - start); 557 558 return this; 559 } 560 561 564 public String substring(int start) 565 { 566 if (_length < start || start < 0) 567 throw new StringIndexOutOfBoundsException (); 568 569 return new String (_buffer, start, _length - start); 570 } 571 572 575 public String substring(int start, int end) 576 { 577 if (_length < start || start < 0 || end < start) 578 throw new StringIndexOutOfBoundsException (); 579 580 return new String (_buffer, start, end - start); 581 } 582 585 public CharBuffer insert(int index, String string) 586 { 587 if (string == null) 588 string = "null"; 589 590 if (index < 0 || _length < index) 591 throw new StringIndexOutOfBoundsException (); 592 593 int len = string.length(); 594 595 if (_buffer.length < _length + len) 596 expandCapacity(len + _length); 597 598 int tail = _length - index; 599 char []buffer = _buffer; 600 601 for (int i = tail - 1; i >= 0; i--) 602 buffer[index + len + i] = buffer[index + i]; 603 604 string.getChars(0, len, buffer, index); 605 _length += len; 606 607 return this; 608 } 609 610 613 public CharBuffer insert(int index, char []buffer, int offset, int len) 614 { 615 if (index < 0 || _length < index) 616 throw new StringIndexOutOfBoundsException (); 617 618 if (_buffer.length < len + _length) 619 expandCapacity(len + _length); 620 621 int tail = _length - index; 622 char []thisBuffer = _buffer; 623 for (int i = tail - 1; i >= 0; i--) 624 buffer[index + len + i] = thisBuffer[index + i]; 625 626 System.arraycopy(buffer, offset, thisBuffer, index, len); 627 _length += len; 628 629 return this; 630 } 631 632 635 public CharBuffer insert(int offset, Object o) 636 { 637 return insert(offset, String.valueOf(o)); 638 } 639 640 643 public CharBuffer insert(int offset, char ch) 644 { 645 return insert(offset, String.valueOf(ch)); 646 } 647 648 651 public CharBuffer insert(int offset, int i) 652 { 653 return insert(offset, String.valueOf(i)); 654 } 655 656 659 public CharBuffer insert(int offset, long l) 660 { 661 return insert(offset, String.valueOf(l)); 662 } 663 664 667 public CharBuffer insert(int offset, float f) 668 { 669 return insert(offset, String.valueOf(f)); 670 } 671 672 675 public CharBuffer insert(int offset, double d) 676 { 677 return insert(offset, String.valueOf(d)); 678 } 679 680 public int indexOf(char ch) 681 { 682 return indexOf(ch, 0); 683 } 684 685 688 public Object clone() 689 { 690 CharBuffer newBuffer = new CharBuffer(); 691 692 newBuffer.setLength(_length); 693 694 System.arraycopy(_buffer, 0, newBuffer._buffer, 0, _length); 695 696 return newBuffer; 697 } 698 699 702 public String toString() 703 { 704 return new String (_buffer, 0, _length); 705 } 706 707 public String close() 708 { 709 String string = new String (_buffer, 0, _length); 710 free(); 711 return string; 712 } 713 714 class CBInputStream extends InputStream { 715 int _index = 0; 716 717 public int read() 718 { 719 if (_length <= _index) 720 return -1; 721 722 return _buffer[_index++]; 723 } 724 } 725 726 public InputStream getInputStream() 727 { 728 return new CBInputStream(); 729 } 730 } 731 | Popular Tags |