001: /*
002: * FastStringBuffer.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: FastStringBuffer.java,v 1.1.1.1 2002/09/24 16:09:19 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class FastStringBuffer {
025: private static final int SPARE_CAPACITY = 128;
026:
027: private char[] buffer;
028: private int used;
029:
030: public FastStringBuffer() {
031: buffer = new char[SPARE_CAPACITY];
032: }
033:
034: public FastStringBuffer(String s) {
035: used = s.length();
036: buffer = new char[used + SPARE_CAPACITY];
037: s.getChars(0, used, buffer, 0);
038: }
039:
040: public FastStringBuffer(char c) {
041: used = 1;
042: buffer = new char[1 + SPARE_CAPACITY];
043: buffer[0] = c;
044: }
045:
046: public FastStringBuffer(int length)
047: throws NegativeArraySizeException {
048: if (length < 0)
049: throw new NegativeArraySizeException();
050:
051: buffer = new char[length];
052: }
053:
054: public final int length() {
055: return used;
056: }
057:
058: public final int capacity() {
059: return buffer.length;
060: }
061:
062: public final char charAt(int index) {
063: if (index >= used)
064: throw new StringIndexOutOfBoundsException();
065: return buffer[index];
066: }
067:
068: public void getChars(int srcBegin, int srcEnd, char dst[],
069: int dstBegin) {
070: if (srcBegin < 0)
071: throw new StringIndexOutOfBoundsException();
072: if (srcBegin > srcEnd)
073: throw new StringIndexOutOfBoundsException();
074: if (srcEnd > used)
075: throw new StringIndexOutOfBoundsException();
076: System.arraycopy(buffer, srcBegin, dst, dstBegin, srcEnd
077: - srcBegin);
078: }
079:
080: public void setCharAt(int index, char c) {
081: if (index < 0 || index >= used)
082: throw new StringIndexOutOfBoundsException(index);
083: buffer[index] = c;
084: }
085:
086: public void ensureCapacity(int minimumCapacity) {
087: if (minimumCapacity <= 0 || buffer.length >= minimumCapacity)
088: return;
089: int newCapacity = buffer.length * 2 + 2;
090: if (newCapacity < minimumCapacity)
091: newCapacity = minimumCapacity;
092: char newBuffer[] = new char[newCapacity];
093: System.arraycopy(buffer, 0, newBuffer, 0, used);
094: buffer = newBuffer;
095: }
096:
097: public void setText(String s) {
098: used = 0;
099: append(s);
100: }
101:
102: public FastStringBuffer append(String s) {
103: if (s == null)
104: s = "null";
105: int addedLength = s.length();
106: int combinedLength = used + addedLength;
107: ensureCapacity(combinedLength);
108: s.getChars(0, addedLength, buffer, used);
109: used = combinedLength;
110: return this ;
111: }
112:
113: public FastStringBuffer append(char[] chars) {
114: if (used + chars.length > buffer.length)
115: ensureCapacity(used + chars.length);
116: System.arraycopy(chars, 0, buffer, used, chars.length);
117: used += chars.length;
118: return this ;
119: }
120:
121: public FastStringBuffer append(char[] chars, int offset, int len) {
122: if (offset < 0 || len < 0 || offset + len > chars.length)
123: throw new StringIndexOutOfBoundsException();
124: if (used + len > buffer.length)
125: ensureCapacity(used + len);
126: System.arraycopy(chars, offset, buffer, used, len);
127: used += len;
128: return this ;
129: }
130:
131: public FastStringBuffer append(Object object) {
132: return append(String.valueOf(object));
133: }
134:
135: public FastStringBuffer append(char c) {
136: if (used + 1 > buffer.length)
137: ensureCapacity(used + 1);
138: buffer[used++] = c;
139: return this ;
140: }
141:
142: public final FastStringBuffer append(int n) {
143: return append(String.valueOf(n));
144: }
145:
146: public final FastStringBuffer append(long n) {
147: return append(String.valueOf(n));
148: }
149:
150: public void setLength(int newLength)
151: throws IndexOutOfBoundsException {
152: if (newLength < 0)
153: throw new StringIndexOutOfBoundsException(newLength);
154: ensureCapacity(newLength);
155: used = newLength;
156: }
157:
158: public FastStringBuffer reverse() {
159: final int limit = used / 2;
160: for (int i = 0; i < limit; ++i) {
161: char c = buffer[i];
162: buffer[i] = buffer[used - i - 1];
163: buffer[used - i - 1] = c;
164: }
165: return this ;
166: }
167:
168: public final String toString() {
169: return new String(buffer, 0, used);
170: }
171: }
|