1 | /* StringBuffer.java -- Growable strings
|
---|
2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.lang;
|
---|
39 | import java.io.Serializable;
|
---|
40 |
|
---|
41 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
---|
42 | * Updated using online JDK 1.2 docs.
|
---|
43 | * Believed complete and correct to JDK 1.2.
|
---|
44 | * Merged with Classpath.
|
---|
45 | */
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <code>StringBuffer</code> represents a changeable <code>String</code>.
|
---|
49 | * It provides the operations required to modify the
|
---|
50 | * <code>StringBuffer</code> including insert, replace, delete, append,
|
---|
51 | * and reverse.
|
---|
52 | * <P>
|
---|
53 | *
|
---|
54 | * <code>StringBuffer</code>s are variable-length in nature, so even if
|
---|
55 | * you initialize them to a certain size, they can still grow larger than
|
---|
56 | * that. <EM>Capacity</EM> indicates the number of characters the
|
---|
57 | * <code>StringBuffer</code> can have in it before it has to grow (growing
|
---|
58 | * the char array is an expensive operation involving <code>new</code>).
|
---|
59 | * <P>
|
---|
60 | *
|
---|
61 | * Incidentally, the String operator "+" actually is turned into a
|
---|
62 | * <code>StringBuffer</code> operation:
|
---|
63 | * <BR>
|
---|
64 | * <code>a + b</code>
|
---|
65 | * <BR>
|
---|
66 | * is the same as
|
---|
67 | * <BR>
|
---|
68 | * <code>new StringBuffer(a).append(b).toString()</code>.
|
---|
69 | *
|
---|
70 | * @implnote Classpath's StringBuffer is capable of sharing memory with
|
---|
71 | * Strings for efficiency. This will help in two instances:
|
---|
72 | * first, when a StringBuffer is created from a String but is
|
---|
73 | * never changed, and second, when a StringBuffer is converted
|
---|
74 | * to a String and the StringBuffer is not changed after that.
|
---|
75 | *
|
---|
76 | * @since JDK1.0
|
---|
77 | * @author Paul Fisher
|
---|
78 | * @author John Keiser
|
---|
79 | * @author Tom Tromey
|
---|
80 | * @see java.lang.String
|
---|
81 | */
|
---|
82 | public final class StringBuffer implements Serializable, CharSequence
|
---|
83 | {
|
---|
84 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
85 | * Uses <code>String.valueOf()</code> to convert to
|
---|
86 | * <code>String</code>.
|
---|
87 | * @param bool the <code>boolean</code> to convert and append.
|
---|
88 | * @return this <code>StringBuffer</code>.
|
---|
89 | * @see java.lang.String#valueOf(boolean)
|
---|
90 | */
|
---|
91 | public StringBuffer append (boolean bool)
|
---|
92 | {
|
---|
93 | return append (String.valueOf(bool));
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Append the <code>char</code> to this <code>StringBuffer</code>.
|
---|
97 | * @param c the <code>char</code> to append.
|
---|
98 | * @return this <code>StringBuffer</code>.
|
---|
99 | */
|
---|
100 | public synchronized StringBuffer append (char ch)
|
---|
101 | {
|
---|
102 | ensureCapacity_unsynchronized (count + 1);
|
---|
103 | value[count++] = ch;
|
---|
104 | return this;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
108 | * Uses <code>String.valueOf()</code> to convert to
|
---|
109 | * <code>String</code>.
|
---|
110 | * @param inum the <code>int</code> to convert and append.
|
---|
111 | * @return this <code>StringBuffer</code>.
|
---|
112 | * @see java.lang.String#valueOf(int)
|
---|
113 | */
|
---|
114 | public native StringBuffer append (int inum);
|
---|
115 |
|
---|
116 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
117 | * Uses <code>String.valueOf()</code> to convert to
|
---|
118 | * <code>String</code>.
|
---|
119 | * @param lnum the <code>long</code> to convert and append.
|
---|
120 | * @return this <code>StringBuffer</code>.
|
---|
121 | * @see java.lang.String#valueOf(long)
|
---|
122 | */
|
---|
123 | public StringBuffer append (long lnum)
|
---|
124 | {
|
---|
125 | return append (String.valueOf(lnum));
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
129 | * Uses <code>String.valueOf()</code> to convert to
|
---|
130 | * <code>String</code>.
|
---|
131 | * @param fnum the <code>float</code> to convert and append.
|
---|
132 | * @return this <code>StringBuffer</code>.
|
---|
133 | * @see java.lang.String#valueOf(float)
|
---|
134 | */
|
---|
135 | public StringBuffer append (float fnum)
|
---|
136 | {
|
---|
137 | return append (String.valueOf(fnum));
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
141 | * Uses <code>String.valueOf()</code> to convert to
|
---|
142 | * <code>String</code>.
|
---|
143 | * @param dnum the <code>double</code> to convert and append.
|
---|
144 | * @return this <code>StringBuffer</code>.
|
---|
145 | * @see java.lang.String#valueOf(double)
|
---|
146 | */
|
---|
147 | public StringBuffer append (double dnum)
|
---|
148 | {
|
---|
149 | return append (String.valueOf(dnum));
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
---|
153 | * Uses <code>String.valueOf()</code> to convert to
|
---|
154 | * <code>String</code>.
|
---|
155 | * @param obj the <code>Object</code> to convert and append.
|
---|
156 | * @return this <code>StringBuffer</code>.
|
---|
157 | * @see java.lang.String#valueOf(java.lang.Object)
|
---|
158 | */
|
---|
159 | public StringBuffer append (Object obj)
|
---|
160 | {
|
---|
161 | return append (String.valueOf(obj));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Append the <code>String</code> to this <code>StringBuffer</code>.
|
---|
165 | * @param str the <code>String</code> to append.
|
---|
166 | * @return this <code>StringBuffer</code>.
|
---|
167 | */
|
---|
168 | public synchronized StringBuffer append (String str)
|
---|
169 | {
|
---|
170 | if (str == null)
|
---|
171 | str = "null";
|
---|
172 | int len = str.length();
|
---|
173 | ensureCapacity_unsynchronized (count + len);
|
---|
174 | str.getChars(0, len, value, count);
|
---|
175 | count += len;
|
---|
176 | return this;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Append the <code>char</code> array to this <code>StringBuffer</code>.
|
---|
180 | * @param data the <code>char[]</code> to append.
|
---|
181 | * @return this <code>StringBuffer</code>.
|
---|
182 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
---|
183 | */
|
---|
184 | public StringBuffer append (char[] data)
|
---|
185 | {
|
---|
186 | return append (data, 0, data.length);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Append the <code>char</code> array to this <code>StringBuffer</code>.
|
---|
190 | * @param data the <code>char[]</code> to append.
|
---|
191 | * @param offset the place to start grabbing characters from
|
---|
192 | * <code>str</code>.
|
---|
193 | * @param count the number of characters to get from <code>str</code>.
|
---|
194 | * @return this <code>StringBuffer</code>.
|
---|
195 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
---|
196 | * @exception IndexOutOfBoundsException if <code>offset</code> or
|
---|
197 | * <code>offset+len</code> is out of range.
|
---|
198 | */
|
---|
199 | public synchronized StringBuffer append (char[] data, int offset, int count)
|
---|
200 | {
|
---|
201 | ensureCapacity_unsynchronized (this.count + count);
|
---|
202 | System.arraycopy(data, offset, value, this.count, count);
|
---|
203 | this.count += count;
|
---|
204 | return this;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Get the total number of characters this <code>StringBuffer</code>
|
---|
208 | * can support before it must be grown. Not to be confused with
|
---|
209 | * <em>length</em>.
|
---|
210 | * @return the capacity of this <code>StringBuffer</code>
|
---|
211 | * @see #length()
|
---|
212 | * @see #ensureCapacity(int)
|
---|
213 | */
|
---|
214 | public int capacity ()
|
---|
215 | {
|
---|
216 | return value.length;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Get the character at the specified index.
|
---|
220 | * @param index the index of the character to get, starting at 0.
|
---|
221 | * @return the character at the specified index.
|
---|
222 | * @exception IndexOutOfBoundsException if the desired character index
|
---|
223 | * is negative or greater then length() - 1.
|
---|
224 | */
|
---|
225 | public synchronized char charAt (int index)
|
---|
226 | {
|
---|
227 | if (index >= count)
|
---|
228 | throw new StringIndexOutOfBoundsException (index);
|
---|
229 | return value[index];
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Delete characters from this <code>StringBuffer</code>.
|
---|
233 | * <code>delete(10, 12)</code> will delete 10 and 11, but not 12.
|
---|
234 | * @param start the first character to delete.
|
---|
235 | * @param end the index after the last character to delete.
|
---|
236 | * @return this <code>StringBuffer</code>.
|
---|
237 | * @exception StringIndexOutOfBoundsException if <code>start</code>
|
---|
238 | * or <code>end-1</code> are out of bounds, or if
|
---|
239 | * <code>start > end</code>.
|
---|
240 | */
|
---|
241 | public synchronized StringBuffer delete (int start, int end)
|
---|
242 | {
|
---|
243 | if (start < 0 || start > count || start > end)
|
---|
244 | throw new StringIndexOutOfBoundsException (start);
|
---|
245 | if (end > count)
|
---|
246 | end = count;
|
---|
247 | // This will unshare if required.
|
---|
248 | ensureCapacity_unsynchronized (count);
|
---|
249 | if (count - end != 0)
|
---|
250 | System.arraycopy (value, end, value, start, count - end);
|
---|
251 | count -= (end - start);
|
---|
252 | return this;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Delete a character from this <code>StringBuffer</code>.
|
---|
256 | * @param index the index of the character to delete.
|
---|
257 | * @return this <code>StringBuffer</code>.
|
---|
258 | * @exception StringIndexOutOfBoundsException if <code>index</code>
|
---|
259 | * is out of bounds.
|
---|
260 | */
|
---|
261 | public StringBuffer deleteCharAt(int index)
|
---|
262 | {
|
---|
263 | return delete (index, index + 1);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** Increase the capacity of this <code>StringBuffer</code>.
|
---|
267 | * This will ensure that an expensive growing operation will not occur
|
---|
268 | * until <code>minimumCapacity</code> is reached.
|
---|
269 | * If the capacity is actually already greater than <code>minimumCapacity</code>
|
---|
270 | * @param minimumCapacity the new capacity.
|
---|
271 | * @see #capacity()
|
---|
272 | */
|
---|
273 | public synchronized void ensureCapacity (int minimumCapacity)
|
---|
274 | {
|
---|
275 | if (shared || minimumCapacity > value.length)
|
---|
276 | {
|
---|
277 | // We don't want to make a larger vector when `shared' is
|
---|
278 | // set. If we do, then setLength becomes very inefficient
|
---|
279 | // when repeatedly reusing a StringBuffer in a loop.
|
---|
280 | int max = (minimumCapacity > value.length
|
---|
281 | ? value.length*2+2
|
---|
282 | : value.length);
|
---|
283 | minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
|
---|
284 | char[] nb = new char[minimumCapacity];
|
---|
285 | System.arraycopy(value, 0, nb, 0, count);
|
---|
286 | value = nb;
|
---|
287 | shared = false;
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | // ensureCapacity is used by several synchronized methods in StringBuffer.
|
---|
292 | // There's no need to synchronize again.
|
---|
293 | private void ensureCapacity_unsynchronized (int minimumCapacity)
|
---|
294 | {
|
---|
295 | if (shared || minimumCapacity > value.length)
|
---|
296 | {
|
---|
297 | // We don't want to make a larger vector when `shared' is
|
---|
298 | // set. If we do, then setLength becomes very inefficient
|
---|
299 | // when repeatedly reusing a StringBuffer in a loop.
|
---|
300 | int max = (minimumCapacity > value.length
|
---|
301 | ? value.length*2+2
|
---|
302 | : value.length);
|
---|
303 | minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
|
---|
304 | char[] nb = new char[minimumCapacity];
|
---|
305 | System.arraycopy(value, 0, nb, 0, count);
|
---|
306 | value = nb;
|
---|
307 | shared = false;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Get the specified array of characters. <code>srcOffset - srcEnd</code>
|
---|
313 | * characters will be copied into the array you pass in.
|
---|
314 | *
|
---|
315 | * @param srcOffset the index to start copying from (inclusive)
|
---|
316 | * @param srcEnd the index to stop copying from (exclusive)
|
---|
317 | * @param dst the array to copy into
|
---|
318 | * @param dstOffset the index to start copying into
|
---|
319 | * @throws NullPointerException if dst is null
|
---|
320 | * @throws IndexOutOfBoundsException if any source or target indices are
|
---|
321 | * out of range (while unspecified, source problems cause a
|
---|
322 | * StringIndexOutOfBoundsException, and dest problems cause an
|
---|
323 | * ArrayIndexOutOfBoundsException)
|
---|
324 | * @see System#arraycopy(Object, int, Object, int, int)
|
---|
325 | */
|
---|
326 | public synchronized void getChars(int srcOffset, int srcEnd,
|
---|
327 | char[] dst, int dstOffset)
|
---|
328 | {
|
---|
329 | int todo = srcEnd - srcOffset;
|
---|
330 | if (srcOffset < 0 || srcEnd > count || todo < 0)
|
---|
331 | throw new StringIndexOutOfBoundsException();
|
---|
332 | System.arraycopy(value, srcOffset, dst, dstOffset, todo);
|
---|
333 | }
|
---|
334 |
|
---|
335 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
336 | * Uses <code>String.valueOf()</code> to convert to
|
---|
337 | * <code>String</code>.
|
---|
338 | * @param offset the place to insert.
|
---|
339 | * @param bool the <code>boolean</code> to convert and insert.
|
---|
340 | * @return this <code>StringBuffer</code>.
|
---|
341 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
342 | * of range for this <code>StringBuffer</code>.
|
---|
343 | * @see java.lang.String#valueOf(boolean)
|
---|
344 | */
|
---|
345 | public StringBuffer insert (int offset, boolean bool)
|
---|
346 | {
|
---|
347 | return insert (offset, bool ? "true" : "false");
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** Insert the <code>char</code> argument into this <code>StringBuffer</code>.
|
---|
351 | * @param offset the place to insert.
|
---|
352 | * @param ch the <code>char</code> to insert.
|
---|
353 | * @return this <code>StringBuffer</code>.
|
---|
354 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
355 | * of range for this <code>StringBuffer</code>.
|
---|
356 | */
|
---|
357 | public synchronized StringBuffer insert (int offset, char ch)
|
---|
358 | {
|
---|
359 | if (offset < 0 || offset > count)
|
---|
360 | throw new StringIndexOutOfBoundsException (offset);
|
---|
361 | ensureCapacity_unsynchronized (count+1);
|
---|
362 | System.arraycopy(value, offset, value, offset+1, count-offset);
|
---|
363 | value[offset] = ch;
|
---|
364 | count++;
|
---|
365 | return this;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
369 | * Uses <code>String.valueOf()</code> to convert to
|
---|
370 | * <code>String</code>.
|
---|
371 | * @param offset the place to insert.
|
---|
372 | * @param inum the <code>int</code> to convert and insert.
|
---|
373 | * @return this <code>StringBuffer</code>.
|
---|
374 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
375 | * of range for this <code>StringBuffer</code>.
|
---|
376 | * @see java.lang.String#valueOf(int)
|
---|
377 | */
|
---|
378 | public StringBuffer insert (int offset, int inum)
|
---|
379 | {
|
---|
380 | return insert (offset, String.valueOf(inum));
|
---|
381 | }
|
---|
382 |
|
---|
383 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
384 | * Uses <code>String.valueOf()</code> to convert to
|
---|
385 | * <code>String</code>.
|
---|
386 | * @param offset the place to insert.
|
---|
387 | * @param lnum the <code>long</code> to convert and insert.
|
---|
388 | * @return this <code>StringBuffer</code>.
|
---|
389 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
390 | * of range for this <code>StringBuffer</code>.
|
---|
391 | * @see java.lang.String#valueOf(long)
|
---|
392 | */
|
---|
393 | public StringBuffer insert (int offset, long lnum)
|
---|
394 | {
|
---|
395 | return insert (offset, String.valueOf(lnum));
|
---|
396 | }
|
---|
397 |
|
---|
398 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
399 | * Uses <code>String.valueOf()</code> to convert to
|
---|
400 | * <code>String</code>.
|
---|
401 | * @param offset the place to insert.
|
---|
402 | * @param fnum the <code>float</code> to convert and insert.
|
---|
403 | * @return this <code>StringBuffer</code>.
|
---|
404 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
405 | * of range for this <code>StringBuffer</code>.
|
---|
406 | * @see java.lang.String#valueOf(float)
|
---|
407 | */
|
---|
408 | public StringBuffer insert (int offset, float fnum)
|
---|
409 | {
|
---|
410 | return insert (offset, String.valueOf(fnum));
|
---|
411 | }
|
---|
412 |
|
---|
413 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
414 | * Uses <code>String.valueOf()</code> to convert to
|
---|
415 | * <code>String</code>.
|
---|
416 | * @param offset the place to insert.
|
---|
417 | * @param dnum the <code>double</code> to convert and insert.
|
---|
418 | * @return this <code>StringBuffer</code>.
|
---|
419 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
420 | * of range for this <code>StringBuffer</code>.
|
---|
421 | * @see java.lang.String#valueOf(double)
|
---|
422 | */
|
---|
423 | public StringBuffer insert (int offset, double dnum)
|
---|
424 | {
|
---|
425 | return insert (offset, String.valueOf(dnum));
|
---|
426 | }
|
---|
427 |
|
---|
428 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
---|
429 | * Uses <code>String.valueOf()</code> to convert to
|
---|
430 | * <code>String</code>.
|
---|
431 | * @param offset the place to insert.
|
---|
432 | * @param obj the <code>Object</code> to convert and insert.
|
---|
433 | * @return this <code>StringBuffer</code>.
|
---|
434 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
435 | * of range for this <code>StringBuffer</code>.
|
---|
436 | * @see java.lang.String#valueOf(java.lang.Object)
|
---|
437 | */
|
---|
438 | public StringBuffer insert (int offset, Object obj)
|
---|
439 | {
|
---|
440 | return insert (offset, String.valueOf(obj));
|
---|
441 | }
|
---|
442 |
|
---|
443 | /** Insert the <code>String</code> argument into this <code>StringBuffer</code>.
|
---|
444 | * @param offset the place to insert.
|
---|
445 | * @param str the <code>String</code> to insert.
|
---|
446 | * @return this <code>StringBuffer</code>.
|
---|
447 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
448 | * of range for this <code>StringBuffer</code>.
|
---|
449 | */
|
---|
450 | public synchronized StringBuffer insert (int offset, String str)
|
---|
451 | {
|
---|
452 | if (offset < 0 || offset > count)
|
---|
453 | throw new StringIndexOutOfBoundsException (offset);
|
---|
454 | // Note that using `null' is from JDK 1.2.
|
---|
455 | if (str == null)
|
---|
456 | str = "null";
|
---|
457 | int len = str.length();
|
---|
458 | ensureCapacity_unsynchronized (count+len);
|
---|
459 | System.arraycopy(value, offset, value, offset+len, count-offset);
|
---|
460 | str.getChars(0, len, value, offset);
|
---|
461 | count += len;
|
---|
462 | return this;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /** Insert the <code>char[]</code> argument into this
|
---|
466 | * <code>StringBuffer</code>.
|
---|
467 | * @param offset the place to insert.
|
---|
468 | * @param data the <code>char[]</code> to insert.
|
---|
469 | * @return this <code>StringBuffer</code>.
|
---|
470 | * @exception NullPointerException if <code>data</code> is
|
---|
471 | * <code>null</code>.
|
---|
472 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
473 | * of range for this <code>StringBuffer</code>.
|
---|
474 | */
|
---|
475 | public StringBuffer insert (int offset, char[] data)
|
---|
476 | {
|
---|
477 | // One could check if offset is invalid here instead of making sure that
|
---|
478 | // data isn't null before dereferencing, but this works just as well.
|
---|
479 | return insert (offset, data, 0, data == null ? 0 : data.length);
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** Insert the <code>char[]</code> argument into this
|
---|
483 | * <code>StringBuffer</code>.
|
---|
484 | * @param offset the place to insert.
|
---|
485 | * @param str the <code>char[]</code> to insert.
|
---|
486 | * @param str_offset the index in <code>str</code> to start inserting
|
---|
487 | * from.
|
---|
488 | * @param len the number of characters to insert.
|
---|
489 | * @return this <code>StringBuffer</code>.
|
---|
490 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
---|
491 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
---|
492 | * of range, for this <code>StringBuffer</code>, or if
|
---|
493 | * <code>str_offset</code> or <code>str_offset+len</code>
|
---|
494 | * are out of range for <code>str</code>.
|
---|
495 | */
|
---|
496 | public synchronized StringBuffer insert(int offset, char[] str,
|
---|
497 | int str_offset, int len)
|
---|
498 | {
|
---|
499 | if (offset < 0 || offset > count)
|
---|
500 | throw new StringIndexOutOfBoundsException (offset);
|
---|
501 | if (len < 0)
|
---|
502 | throw new StringIndexOutOfBoundsException (len);
|
---|
503 | if (str_offset < 0 || str_offset + len > str.length)
|
---|
504 | throw new StringIndexOutOfBoundsException (str_offset);
|
---|
505 | ensureCapacity_unsynchronized (count + len);
|
---|
506 | System.arraycopy(value, offset, value, offset + len, count - offset);
|
---|
507 | System.arraycopy(str, str_offset, value, offset, len);
|
---|
508 | count += len;
|
---|
509 | return this;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /** Get the length of the <code>String</code> this
|
---|
513 | * <code>StringBuffer</code> would create. Not to be confused with the
|
---|
514 | * <em>capacity</em> of the <code>StringBuffer</code>.
|
---|
515 | * @return the length of this <code>StringBuffer</code>.
|
---|
516 | * @see #capacity()
|
---|
517 | * @see #setLength(int)
|
---|
518 | */
|
---|
519 | public int length ()
|
---|
520 | {
|
---|
521 | return count;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /** Replace characters between index <code>start</code> (inclusive) and
|
---|
525 | * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
|
---|
526 | * is larger than the size of this StringBuffer, all characters after
|
---|
527 | * <code>start</code> are replaced.
|
---|
528 | * @param start the beginning index of characters to delete (inclusive).
|
---|
529 | * @param end the ending index of characters to delete (exclusive).
|
---|
530 | * @param str the new <code>String</code> to insert.
|
---|
531 | * @return this <code>StringBuffer</code>.
|
---|
532 | */
|
---|
533 | public synchronized StringBuffer replace (int start, int end, String str)
|
---|
534 | {
|
---|
535 | if (start < 0 || start > count || start > end)
|
---|
536 | throw new StringIndexOutOfBoundsException (start);
|
---|
537 |
|
---|
538 | int len = str.length();
|
---|
539 | // Calculate the difference in 'count' after the replace.
|
---|
540 | int delta = len - ((end > count ? count : end) - start);
|
---|
541 | ensureCapacity_unsynchronized (count + delta);
|
---|
542 |
|
---|
543 | if (delta != 0 && end < count)
|
---|
544 | System.arraycopy(value, end, value, end + delta, count - end);
|
---|
545 |
|
---|
546 | str.getChars (0, len, value, start);
|
---|
547 | count += delta;
|
---|
548 | return this;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /** Reverse the characters in this StringBuffer.
|
---|
552 | * @return this <code>StringBuffer</code>.
|
---|
553 | */
|
---|
554 | public synchronized StringBuffer reverse ()
|
---|
555 | {
|
---|
556 | // Call ensureCapacity to enforce copy-on-write.
|
---|
557 | ensureCapacity_unsynchronized (count);
|
---|
558 | for (int i = 0; i < count / 2; ++i)
|
---|
559 | {
|
---|
560 | char c = value[i];
|
---|
561 | value[i] = value[count - i - 1];
|
---|
562 | value[count - i - 1] = c;
|
---|
563 | }
|
---|
564 | return this;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /** Set the character at the specified index.
|
---|
568 | * @param index the index of the character to set starting at 0.
|
---|
569 | * @param ch the value to set that character to.
|
---|
570 | * @exception IndexOutOfBoundsException if the specified character
|
---|
571 | * index is not between 0 and length() - 1 (inclusive).
|
---|
572 | */
|
---|
573 | public synchronized void setCharAt (int index, char ch)
|
---|
574 | {
|
---|
575 | if (index < 0 || index >= count)
|
---|
576 | throw new StringIndexOutOfBoundsException (index);
|
---|
577 | // Call ensureCapacity to enforce copy-on-write.
|
---|
578 | ensureCapacity_unsynchronized (count);
|
---|
579 | value[index] = ch;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /** Set the length of this StringBuffer.
|
---|
583 | * <P>
|
---|
584 | * If the new length is greater than the current length, all the new
|
---|
585 | * characters are set to '\0'.
|
---|
586 | * <P>
|
---|
587 | * If the new length is less than the current length, the first
|
---|
588 | * <code>newLength</code> characters of the old array will be
|
---|
589 | * @param newLength the new length
|
---|
590 | * @exception IndexOutOfBoundsException if the new length is
|
---|
591 | * negative.
|
---|
592 | * @see #length()
|
---|
593 | */
|
---|
594 | public synchronized void setLength (int newLength)
|
---|
595 | {
|
---|
596 | if (newLength < 0)
|
---|
597 | throw new StringIndexOutOfBoundsException (newLength);
|
---|
598 |
|
---|
599 | ensureCapacity_unsynchronized (newLength);
|
---|
600 | for (int i = count; i < newLength; ++i)
|
---|
601 | value[i] = '\0';
|
---|
602 | count = newLength;
|
---|
603 | }
|
---|
604 |
|
---|
605 | /** Create a new StringBuffer with default capacity 16.
|
---|
606 | * @see JLS 20.13.1
|
---|
607 | */
|
---|
608 | public StringBuffer ()
|
---|
609 | {
|
---|
610 | this (DEFAULT_CAPACITY);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
|
---|
614 | * @param capacity the initial capacity.
|
---|
615 | */
|
---|
616 | public StringBuffer (int capacity)
|
---|
617 | {
|
---|
618 | count = 0;
|
---|
619 | value = new char[capacity];
|
---|
620 | shared = false;
|
---|
621 | }
|
---|
622 |
|
---|
623 | /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>.
|
---|
624 | * Initial capacity will be the size of the String plus 16.
|
---|
625 | * @param str the <code>String</code> to make a <code>StringBuffer</code> out of.
|
---|
626 | * @XXX optimize for sharing.
|
---|
627 | */
|
---|
628 | public StringBuffer (String str)
|
---|
629 | {
|
---|
630 | // The documentation is not clear, but experimentation with
|
---|
631 | // other implementations indicates that StringBuffer(null)
|
---|
632 | // should throw a NullPointerException.
|
---|
633 | count = str.length();
|
---|
634 | // JLS: The initial capacity of the string buffer is 16 plus the
|
---|
635 | // length of the argument string.
|
---|
636 | value = new char[count + DEFAULT_CAPACITY];
|
---|
637 | str.getChars(0, count, value, 0);
|
---|
638 | shared = false;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Creates a substring of this StringBuffer, starting at a specified index
|
---|
643 | * and ending at the end of this StringBuffer.
|
---|
644 | *
|
---|
645 | * @param beginIndex index to start substring (base 0)
|
---|
646 | *
|
---|
647 | * @return new String which is a substring of this StringBuffer
|
---|
648 | *
|
---|
649 | * @exception StringIndexOutOfBoundsException
|
---|
650 | * if (beginIndex < 0 || beginIndex > this.length())
|
---|
651 | */
|
---|
652 | public String substring (int beginIndex)
|
---|
653 | {
|
---|
654 | return substring (beginIndex, count);
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Creates a substring of this StringBuffer, starting at a specified index
|
---|
659 | * and ending at one character before a specified index.
|
---|
660 | *
|
---|
661 | * @param beginIndex index to start substring (base 0)
|
---|
662 | * @param endIndex index after the last character to be
|
---|
663 | * copied into the substring
|
---|
664 | *
|
---|
665 | * @return new String which is a substring of this StringBuffer
|
---|
666 | *
|
---|
667 | * @exception StringIndexOutOfBoundsException
|
---|
668 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
---|
669 | */
|
---|
670 | public synchronized String substring (int beginIndex, int endIndex)
|
---|
671 | {
|
---|
672 | if (beginIndex < 0 || endIndex > count || beginIndex > endIndex)
|
---|
673 | throw new StringIndexOutOfBoundsException ();
|
---|
674 | // FIXME: for libgcj it would be possible, and more efficient, to
|
---|
675 | // enable sharing here.
|
---|
676 | return new String (value, beginIndex, endIndex - beginIndex);
|
---|
677 | }
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Creates a substring of this StringBuffer, starting at a specified index
|
---|
681 | * and ending at one character before a specified index.
|
---|
682 | * <p>
|
---|
683 | * To implement <code>CharSequence</code>.
|
---|
684 | * Calls <code>substring(beginIndex, endIndex)</code>.
|
---|
685 | *
|
---|
686 | * @param beginIndex index to start substring (base 0)
|
---|
687 | * @param endIndex index after the last character to be
|
---|
688 | * copied into the substring
|
---|
689 | *
|
---|
690 | * @return new String which is a substring of this StringBuffer
|
---|
691 | *
|
---|
692 | * @exception StringIndexOutOfBoundsException
|
---|
693 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
---|
694 | */
|
---|
695 | public CharSequence subSequence (int beginIndex, int endIndex)
|
---|
696 | {
|
---|
697 | return substring(beginIndex, endIndex);
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | /** Convert this <code>StringBuffer</code> to a <code>String</code>.
|
---|
702 | * @return the characters in this StringBuffer
|
---|
703 | */
|
---|
704 | public String toString ()
|
---|
705 | {
|
---|
706 | // Note: in libgcj this causes the StringBuffer to be shared. In
|
---|
707 | // Classpath it does not.
|
---|
708 | return new String (this);
|
---|
709 | }
|
---|
710 |
|
---|
711 | // Index of next available character. Note that this has
|
---|
712 | // permissions set this way so that String can get the value.
|
---|
713 | int count;
|
---|
714 |
|
---|
715 | // The buffer. Note that this has permissions set this way so that
|
---|
716 | // String can get the value.
|
---|
717 | char[] value;
|
---|
718 |
|
---|
719 | // True if we need to copy the buffer before writing to it again.
|
---|
720 | // FIXME: JDK 1.2 doesn't specify this. The new buffer-growing
|
---|
721 | // semantics make this less useful in that case, too. Note that
|
---|
722 | // this has permissions set this way so that String can get the
|
---|
723 | // value.
|
---|
724 | boolean shared;
|
---|
725 |
|
---|
726 | static final long serialVersionUID = 3388685877147921107L;
|
---|
727 | private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1
|
---|
728 | }
|
---|