1 21 22 package org.armedbear.j; 23 24 public final class FastStringBuffer 25 { 26 private static final int SPARE_CAPACITY = 128; 27 28 private char[] buffer; 29 private int used; 30 31 public FastStringBuffer() 32 { 33 buffer = new char[SPARE_CAPACITY]; 34 } 35 36 public FastStringBuffer(String s) 37 { 38 used = s.length(); 39 buffer = new char[used + SPARE_CAPACITY]; 40 s.getChars(0, used, buffer, 0); 41 } 42 43 public FastStringBuffer(char c) 44 { 45 used = 1; 46 buffer = new char[1 + SPARE_CAPACITY]; 47 buffer[0] = c; 48 } 49 50 public FastStringBuffer(int length) throws NegativeArraySizeException 51 { 52 if (length < 0) 53 throw new NegativeArraySizeException (); 54 55 buffer = new char[length]; 56 } 57 58 public final int length() 59 { 60 return used; 61 } 62 63 public final int capacity() 64 { 65 return buffer.length; 66 } 67 68 public final char charAt(int index) 69 { 70 if (index >= used) 71 throw new StringIndexOutOfBoundsException (); 72 return buffer[index]; 73 } 74 75 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 76 { 77 if (srcBegin < 0) 78 throw new StringIndexOutOfBoundsException (); 79 if (srcBegin > srcEnd) 80 throw new StringIndexOutOfBoundsException (); 81 if (srcEnd > used) 82 throw new StringIndexOutOfBoundsException (); 83 System.arraycopy(buffer, srcBegin, dst, dstBegin, srcEnd - srcBegin); 84 } 85 86 public void setCharAt(int index, char c) 87 { 88 if (index < 0 || index >= used) 89 throw new StringIndexOutOfBoundsException (index); 90 buffer[index] = c; 91 } 92 93 public void ensureCapacity(int minimumCapacity) 94 { 95 if (minimumCapacity <= 0 || buffer.length >= minimumCapacity) 96 return; 97 int newCapacity = buffer.length * 2 + 2; 98 if (newCapacity < minimumCapacity) 99 newCapacity = minimumCapacity; 100 char newBuffer[] = new char[newCapacity]; 101 System.arraycopy(buffer, 0, newBuffer, 0, used); 102 buffer = newBuffer; 103 } 104 105 public void setText(String s) 106 { 107 used = 0; 108 append(s); 109 } 110 111 public FastStringBuffer append(String s) 112 { 113 if (s == null) 114 s = "null"; 115 int addedLength = s.length(); 116 int combinedLength = used + addedLength; 117 ensureCapacity(combinedLength); 118 s.getChars(0, addedLength, buffer, used); 119 used = combinedLength; 120 return this; 121 } 122 123 public FastStringBuffer append(char[] chars) 124 { 125 if (used + chars.length > buffer.length) 126 ensureCapacity(used + chars.length); 127 System.arraycopy(chars, 0, buffer, used, chars.length); 128 used += chars.length; 129 return this; 130 } 131 132 public FastStringBuffer append(char[] chars, int offset, int len) 133 { 134 if (offset < 0 || len < 0 || offset + len > chars.length) 135 throw new StringIndexOutOfBoundsException (); 136 if (used + len > buffer.length) 137 ensureCapacity(used + len); 138 System.arraycopy(chars, offset, buffer, used, len); 139 used += len; 140 return this; 141 } 142 143 public FastStringBuffer append(Object object) 144 { 145 return append(String.valueOf(object)); 146 } 147 148 public FastStringBuffer append(char c) 149 { 150 if (used + 1 > buffer.length) 151 ensureCapacity(used + 1); 152 buffer[used++] = c; 153 return this; 154 } 155 156 public final FastStringBuffer append(int n) 157 { 158 return append(String.valueOf(n)); 159 } 160 161 public final FastStringBuffer append(long n) 162 { 163 return append(String.valueOf(n)); 164 } 165 166 public void setLength(int newLength) throws IndexOutOfBoundsException 167 { 168 if (newLength < 0) 169 throw new StringIndexOutOfBoundsException (newLength); 170 ensureCapacity(newLength); 171 used = newLength; 172 } 173 174 public FastStringBuffer reverse() 175 { 176 final int limit = used / 2; 177 for (int i = 0; i < limit; ++i) { 178 char c = buffer[i]; 179 buffer[i] = buffer[used-i-1]; 180 buffer[used-i-1] = c; 181 } 182 return this; 183 } 184 185 public final String toString() 186 { 187 return new String (buffer, 0, used); 188 } 189 } 190 | Popular Tags |