KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FastStringBuffer


1 /*
2  * FastStringBuffer.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: FastStringBuffer.java,v 1.1.1.1 2002/09/24 16:09:19 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

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 JavaDoc 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 JavaDoc
51     {
52         if (length < 0)
53             throw new NegativeArraySizeException JavaDoc();
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 JavaDoc();
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 JavaDoc();
79         if (srcBegin > srcEnd)
80             throw new StringIndexOutOfBoundsException JavaDoc();
81         if (srcEnd > used)
82             throw new StringIndexOutOfBoundsException JavaDoc();
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 JavaDoc(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 JavaDoc s)
106     {
107         used = 0;
108         append(s);
109     }
110
111     public FastStringBuffer append(String JavaDoc 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 JavaDoc();
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 JavaDoc 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 JavaDoc
167     {
168         if (newLength < 0)
169             throw new StringIndexOutOfBoundsException JavaDoc(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 JavaDoc toString()
186     {
187         return new String JavaDoc(buffer, 0, used);
188     }
189 }
190
Popular Tags