1 package net.sf.saxon.om; 2 3 import net.sf.saxon.tinytree.CharSlice; 4 5 import java.io.Writer ; 6 import java.io.Serializable ; 7 8 14 15 public final class FastStringBuffer implements CharSequence , Serializable { 16 17 private char[] array; 18 private int used = 0; 19 20 public FastStringBuffer(int initialSize) { 21 array = new char[initialSize]; 22 } 23 24 28 29 public void append(String s) { 30 int len = s.length(); 31 ensureCapacity(len); 32 s.getChars(0, len, array, used); 33 used += len; 34 } 35 36 40 41 public void append(CharSlice s) { 42 int len = s.length(); 43 ensureCapacity(len); 44 s.copyTo(array, used); 45 used += len; 46 } 47 48 52 53 public void append(FastStringBuffer s) { 54 int len = s.length(); 55 ensureCapacity(len); 56 s.getChars(0, len, array, used); 57 used += len; 58 } 59 60 64 65 public void append(StringBuffer s) { 66 int len = s.length(); 67 ensureCapacity(len); 68 s.getChars(0, len, array, used); 69 used += len; 70 } 71 72 76 77 public void append(CharSequence s) { 78 final int len = s.length(); 82 ensureCapacity(len); 83 if (s instanceof CharSlice) { 84 ((CharSlice)s).copyTo(array, used); 85 } else if (s instanceof String ) { 86 ((String )s).getChars(0, len, array, used); 87 } else if (s instanceof FastStringBuffer) { 88 ((FastStringBuffer)s).getChars(0, len, array, used); 89 } else { 90 s.toString().getChars(0, len, array, used); 91 } 92 used += len; 93 } 94 95 101 102 public void append(char[] srcArray, int start, int length) { 103 ensureCapacity(length); 104 System.arraycopy(srcArray, start, array, used, length); 105 used += length; 106 } 107 108 112 113 public void append(char ch) { 114 ensureCapacity(1); 115 array[used++] = ch; 116 } 117 118 124 public int length() { 125 return used; 126 } 127 128 143 public char charAt(int index) { 144 if (index >= used) { 145 throw new IndexOutOfBoundsException ("" + index); 146 } 147 return array[index]; 148 } 149 150 165 public CharSequence subSequence(int start, int end) { 166 return new CharSlice(array, start, end - start); 167 } 168 169 199 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 200 if (srcBegin < 0) { 201 throw new StringIndexOutOfBoundsException (srcBegin); 202 } 203 if (srcEnd > used) { 204 throw new StringIndexOutOfBoundsException (srcEnd); 205 } 206 if (srcBegin > srcEnd) { 207 throw new StringIndexOutOfBoundsException (srcEnd - srcBegin); 208 } 209 System.arraycopy(array, srcBegin, dst, dstBegin, srcEnd - srcBegin); 210 } 211 212 215 216 public String toString() { 217 condense(); 218 return new String (array, 0, used); 219 } 220 221 227 228 public void setCharAt(int index, char ch) { 229 if (index<0 || index>used) { 230 throw new IndexOutOfBoundsException (""+index); 231 } 232 array[index] = ch; 233 } 234 235 241 242 public void setLength(int length) { 243 if (length < 0 || length > used) { 244 return; 245 } 246 used = length; 247 } 248 249 252 253 public void ensureCapacity(int extra) { 254 if (used + extra > array.length) { 255 int newlen = array.length * 2; 256 if (newlen < used + extra) { 257 newlen = used + extra*2; 258 } 259 char[] array2 = new char[newlen]; 260 System.arraycopy(array, 0, array2, 0, used); 261 array = array2; 262 } 263 } 264 265 271 272 public CharSequence condense() { 273 if (array.length - used > 256 || array.length > used * 2) { 274 char[] array2 = new char[used]; 275 System.arraycopy(array, 0, array2, 0, used); 276 array = array2; 277 } 278 return this; 279 } 280 281 284 285 public void write(Writer writer) throws java.io.IOException { 286 writer.write(array, 0, used); 287 } 288 } 289 290 308 | Popular Tags |