1 64 65 package com.jcorporate.expresso.kernel.util; 66 67 import org.apache.commons.pool.BasePoolableObjectFactory; 68 import org.apache.commons.pool.ObjectPool; 69 import org.apache.commons.pool.impl.StackObjectPool; 70 71 103 public final class FastStringBuffer 104 implements java.io.Serializable { 105 106 109 private char[] value; 110 111 114 private int count; 115 116 119 private boolean shared; 120 121 static FastStringBuffer pointerInstance = null; 122 123 126 private static FastStringBufferObjectFactory factory = null; 127 128 129 132 private static ObjectPool thePool = null; 133 134 135 136 137 142 public FastStringBuffer() { 143 this(16); 144 } 145 146 154 public FastStringBuffer(int length) { 155 synchronized (FastStringBuffer.class) { 156 if (factory == null) { 157 factory = new FastStringBuffer.FastStringBufferObjectFactory(); 158 } 159 } 160 value = new char[length]; 161 shared = false; 162 } 163 164 172 public FastStringBuffer(String str) { 173 this(str.length() + 16); 174 append(str); 175 176 synchronized (FastStringBuffer.class) { 177 if (factory == null) { 178 factory = new FastStringBuffer.FastStringBufferObjectFactory(); 179 } 180 } 181 182 } 183 184 189 public int length() { 190 return count; 191 } 192 193 200 public int capacity() { 201 return value.length; 202 } 203 204 207 public void clear() { 208 copyWhenShared(); 209 count = 0; 210 } 211 212 215 private final void copyWhenShared() { 216 if (shared) { 217 char[] newValue = new char[value.length]; 218 System.arraycopy(value, 0, newValue, 0, count); 219 value = newValue; 220 shared = false; 221 } 222 } 223 224 239 public void ensureCapacity(int minimumCapacity) { 240 int maxCapacity = value.length; 241 242 if (minimumCapacity > maxCapacity) { 243 int newCapacity = maxCapacity + maxCapacity + 2; 244 245 if (minimumCapacity > newCapacity) { 246 newCapacity = minimumCapacity; 247 } 248 249 char[] newValue = new char[newCapacity]; 250 System.arraycopy(value, 0, newValue, 0, count); 251 value = newValue; 252 shared = false; 253 } 254 } 255 256 274 public void setLength(int newLength) { 275 ensureCapacity(newLength); 276 277 if (count < newLength) { 278 copyWhenShared(); 279 280 } 284 285 count = newLength; 286 } 287 288 302 public char charAt(int index) { 303 return value[index]; 304 } 305 306 326 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { 327 System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - 328 srcBegin); 329 } 330 331 344 public void setCharAt(int index, char ch) { 345 copyWhenShared(); 346 value[index] = ch; 347 } 348 349 362 public FastStringBuffer append(Object obj) { 363 return append(String.valueOf(obj)); 364 } 365 366 376 public FastStringBuffer append(String str) { 377 if (str == null) { 378 str = String.valueOf(str); 379 } 380 381 int len = str.length(); 382 ensureCapacity(count + len); 383 copyWhenShared(); 384 str.getChars(0, len, value, count); 385 count += len; 386 387 return this; 388 } 389 390 401 public FastStringBuffer append(char[] str) { 402 int len = str.length; 403 ensureCapacity(count + len); 404 copyWhenShared(); 405 System.arraycopy(str, 0, value, count, len); 406 count += len; 407 408 return this; 409 } 410 411 425 public FastStringBuffer append(char[] str, int offset, int len) { 426 ensureCapacity(count + len); 427 copyWhenShared(); 428 System.arraycopy(str, offset, value, count, len); 429 count += len; 430 431 return this; 432 } 433 434 441 public FastStringBuffer append(FastStringBuffer str) { 442 int len = str.length(); 443 ensureCapacity(count + str.count); 444 copyWhenShared(); 445 System.arraycopy(str.getValue(), 0, value, count, len); 446 count += len; 447 448 return this; 449 } 450 451 464 public FastStringBuffer append(boolean b) { 465 return append(String.valueOf(b)); 466 } 467 468 478 public FastStringBuffer append(char c) { 479 ensureCapacity(count + 1); 480 copyWhenShared(); 481 value[count++] = c; 482 483 return this; 484 } 485 486 499 public FastStringBuffer append(int i) { 500 return append(String.valueOf(i)); 501 } 502 503 516 public FastStringBuffer append(long l) { 517 return append(String.valueOf(l)); 518 } 519 520 533 public FastStringBuffer append(float f) { 534 return append(String.valueOf(f)); 535 } 536 537 550 public FastStringBuffer append(double d) { 551 return append(String.valueOf(d)); 552 } 553 554 560 public FastStringBuffer reverse() { 561 copyWhenShared(); 562 563 int n = count - 1; 564 565 for (int j = (n - 1) >> 1; j >= 0; --j) { 566 char temp = value[j]; 567 value[j] = value[n - j]; 568 value[n - j] = temp; 569 } 570 571 return this; 572 } 573 574 584 public String toString() { 585 return new String (value, 0, count); 586 } 587 588 final void setShared() { 593 shared = true; 594 } 595 596 final char[] getValue() { 597 return value; 598 } 599 600 601 608 public synchronized static FastStringBuffer getInstance() { 609 synchronized (FastStringBuffer.class) { 610 if (FastStringBuffer.pointerInstance == null) { 611 pointerInstance = new FastStringBuffer(1); 612 thePool = new StackObjectPool(factory); 613 } 614 615 try { 616 return (FastStringBuffer) thePool.borrowObject(); 617 618 } catch (Exception ex) { 619 System.err.println(ex.getMessage()); 620 ex.printStackTrace(); 621 return null; 622 } 623 } 624 } 625 626 630 public void release() { 631 synchronized (FastStringBuffer.class) { 632 try { 633 thePool.returnObject(this); 634 } catch (Exception ex) { 635 } 636 } 637 } 638 639 640 646 class FastStringBufferObjectFactory extends BasePoolableObjectFactory { 647 public Object makeObject() { 649 return new FastStringBuffer(1024); 650 } 651 652 public void passivateObject(Object obj) { 655 FastStringBuffer buf = (FastStringBuffer) obj; 656 if (buf.value.length > 1024) { 657 buf.value = new char[1024]; 658 buf.clear(); 659 } else { 660 buf.clear(); 661 } 662 } 663 664 668 } 669 } | Popular Tags |