1 /*
2 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.lang;
27
28
29 /**
30 * A thread-safe, mutable sequence of characters.
31 * A string buffer is like a {@link String}, but can be modified. At any
32 * point in time it contains some particular sequence of characters, but
33 * the length and content of the sequence can be changed through certain
34 * method calls.
35 * <p>
36 * String buffers are safe for use by multiple threads. The methods
37 * are synchronized where necessary so that all the operations on any
38 * particular instance behave as if they occur in some serial order
39 * that is consistent with the order of the method calls made by each of
40 * the individual threads involved.
41 * <p>
42 * The principal operations on a <code>StringBuffer</code> are the
43 * <code>append</code> and <code>insert</code> methods, which are
44 * overloaded so as to accept data of any type. Each effectively
45 * converts a given datum to a string and then appends or inserts the
46 * characters of that string to the string buffer. The
47 * <code>append</code> method always adds these characters at the end
48 * of the buffer; the <code>insert</code> method adds the characters at
49 * a specified point.
50 * <p>
51 * For example, if <code>z</code> refers to a string buffer object
52 * whose current contents are "<code>start</code>", then
53 * the method call <code>z.append("le")</code> would cause the string
54 * buffer to contain "<code>startle</code>", whereas
55 * <code>z.insert(4, "le")</code> would alter the string buffer to
56 * contain "<code>starlet</code>".
57 * <p>
58 * In general, if sb refers to an instance of a <code>StringBuffer</code>,
59 * then <code>sb.append(x)</code> has the same effect as
60 * <code>sb.insert(sb.length(), x)</code>.
61 * <p>
62 * Whenever an operation occurs involving a source sequence (such as
63 * appending or inserting from a source sequence) this class synchronizes
64 * only on the string buffer performing the operation, not on the source.
65 * <p>
66 * Every string buffer has a capacity. As long as the length of the
67 * character sequence contained in the string buffer does not exceed
68 * the capacity, it is not necessary to allocate a new internal
69 * buffer array. If the internal buffer overflows, it is
70 * automatically made larger.
71 *
72 * As of release JDK 5, this class has been supplemented with an equivalent
73 * class designed for use by a single thread, {@link StringBuilder}. The
74 * <tt>StringBuilder</tt> class should generally be used in preference to
75 * this one, as it supports all of the same operations but it is faster, as
76 * it performs no synchronization.
77 *
78 * @author Arthur van Hoff
79 * @see java.lang.StringBuilder
80 * @see java.lang.String
81 * @since JDK1.0
82 */
83 public final class StringBuffer
84 extends AbstractStringBuilder
85 implements java.io.Serializable, CharSequence
86 {
87
88 /** use serialVersionUID from JDK 1.0.2 for interoperability */
89 static final long serialVersionUID = 3388685877147921107L;
90
91 /**
92 * Constructs a string buffer with no characters in it and an
93 * initial capacity of 16 characters.
94 */
95 public StringBuffer() {
96 super(16);
97 }
98
99 /**
100 * Constructs a string buffer with no characters in it and
101 * the specified initial capacity.
102 *
103 * @param capacity the initial capacity.
104 * @exception NegativeArraySizeException if the <code>capacity</code>
105 * argument is less than <code>0</code>.
106 */
107 public StringBuffer(int capacity) {
108 super(capacity);
109 }
110
111 /**
112 * Constructs a string buffer initialized to the contents of the
113 * specified string. The initial capacity of the string buffer is
114 * <code>16</code> plus the length of the string argument.
115 *
116 * @param str the initial contents of the buffer.
117 * @exception NullPointerException if <code>str</code> is <code>null</code>
118 */
119 public StringBuffer(String str) {
120 super(str.length() + 16);
121 append(str);
122 }
123
124 /**
125 * Constructs a string buffer that contains the same characters
126 * as the specified <code>CharSequence</code>. The initial capacity of
127 * the string buffer is <code>16</code> plus the length of the
128 * <code>CharSequence</code> argument.
129 * <p>
130 * If the length of the specified <code>CharSequence</code> is
131 * less than or equal to zero, then an empty buffer of capacity
132 * <code>16</code> is returned.
133 *
134 * @param seq the sequence to copy.
135 * @exception NullPointerException if <code>seq</code> is <code>null</code>
136 * @since 1.5
137 */
138 public StringBuffer(CharSequence seq) {
139 this(seq.length() + 16);
140 append(seq);
141 }
142
143 public synchronized int length() {
144 return count;
145 }
146
147 public synchronized int capacity() {
148 return value.length;
149 }
150
151
152 public synchronized void ensureCapacity(int minimumCapacity) {
153 if (minimumCapacity > value.length) {
154 expandCapacity(minimumCapacity);
155 }
156 }
157
158 /**
159 * @since 1.5
160 */
161 public synchronized void trimToSize() {
162 super.trimToSize();
163 }
164
165 /**
166 * @throws IndexOutOfBoundsException {@inheritDoc}
167 * @see #length()
168 */
169 public synchronized void setLength(int newLength) {
170 super.setLength(newLength);
171 }
172
173 /**
174 * @throws IndexOutOfBoundsException {@inheritDoc}
175 * @see #length()
176 */
177 public synchronized char charAt(int index) {
178 if ((index < 0) || (index >= count))
179 throw new StringIndexOutOfBoundsException(index);
180 return value[index];
181 }
182
183 /**
184 * @since 1.5
185 */
186 public synchronized int codePointAt(int index) {
187 return super.codePointAt(index);
188 }
189
190 /**
191 * @since 1.5
192 */
193 public synchronized int codePointBefore(int index) {
194 return super.codePointBefore(index);
195 }
196
197 /**
198 * @since 1.5
199 */
200 public synchronized int codePointCount(int beginIndex, int endIndex) {
201 return super.codePointCount(beginIndex, endIndex);
202 }
203
204 /**
205 * @since 1.5
206 */
207 public synchronized int offsetByCodePoints(int index, int codePointOffset) {
208 return super.offsetByCodePoints(index, codePointOffset);
209 }
210
211 /**
212 * @throws NullPointerException {@inheritDoc}
213 * @throws IndexOutOfBoundsException {@inheritDoc}
214 */
215 public synchronized void getChars(int srcBegin, int srcEnd, char dst[],
216 int dstBegin)
217 {
218 super.getChars(srcBegin, srcEnd, dst, dstBegin);
219 }
220
221 /**
222 * @throws IndexOutOfBoundsException {@inheritDoc}
223 * @see #length()
224 */
225 public synchronized void setCharAt(int index, char ch) {
226 if ((index < 0) || (index >= count))
227 throw new StringIndexOutOfBoundsException(index);
228 value[index] = ch;
229 }
230
231 /**
232 * @see java.lang.String#valueOf(java.lang.Object)
233 * @see #append(java.lang.String)
234 */
235 public synchronized StringBuffer append(Object obj) {
236 super.append(String.valueOf(obj));
237 return this;
238 }
239
240 public synchronized StringBuffer append(String str) {
241 super.append(str);
242 return this;
243 }
244
245 /**
246 * Appends the specified <tt>StringBuffer</tt> to this sequence.
247 * <p>
248 * The characters of the <tt>StringBuffer</tt> argument are appended,
249 * in order, to the contents of this <tt>StringBuffer</tt>, increasing the
250 * length of this <tt>StringBuffer</tt> by the length of the argument.
251 * If <tt>sb</tt> is <tt>null</tt>, then the four characters
252 * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
253 * <p>
254 * Let <i>n</i> be the length of the old character sequence, the one
255 * contained in the <tt>StringBuffer</tt> just prior to execution of the
256 * <tt>append</tt> method. Then the character at index <i>k</i> in
257 * the new character sequence is equal to the character at index <i>k</i>
258 * in the old character sequence, if <i>k</i> is less than <i>n</i>;
259 * otherwise, it is equal to the character at index <i>k-n</i> in the
260 * argument <code>sb</code>.
261 * <p>
262 * This method synchronizes on <code>this</code> (the destination)
263 * object but does not synchronize on the source (<code>sb</code>).
264 *
265 * @param sb the <tt>StringBuffer</tt> to append.
266 * @return a reference to this object.
267 * @since 1.4
268 */
269 public synchronized StringBuffer append(StringBuffer sb) {
270 super.append(sb);
271 return this;
272 }
273
274
275 /**
276 * Appends the specified <code>CharSequence</code> to this
277 * sequence.
278 * <p>
279 * The characters of the <code>CharSequence</code> argument are appended,
280 * in order, increasing the length of this sequence by the length of the
281 * argument.
282 *
283 * <p>The result of this method is exactly the same as if it were an
284 * invocation of this.append(s, 0, s.length());
285 *
286 * <p>This method synchronizes on this (the destination)
287 * object but does not synchronize on the source (<code>s</code>).
288 *
289 * <p>If <code>s</code> is <code>null</code>, then the four characters
290 * <code>"null"</code> are appended.
291 *
292 * @param s the <code>CharSequence</code> to append.
293 * @return a reference to this object.
294 * @since 1.5
295 */
296 public StringBuffer append(CharSequence s) {
297 // Note, synchronization achieved via other invocations
298 if (s == null)
299 s = "null";
300 if (s instanceof String)
301 return this.append((String)s);
302 if (s instanceof StringBuffer)
303 return this.append((StringBuffer)s);
304 return this.append(s, 0, s.length());
305 }
306
307 /**
308 * @throws IndexOutOfBoundsException {@inheritDoc}
309 * @since 1.5
310 */
311 public synchronized StringBuffer append(CharSequence s, int start, int end)
312 {
313 super.append(s, start, end);
314 return this;
315 }
316
317 public synchronized StringBuffer append(char str[]) {
318 super.append(str);
319 return this;
320 }
321
322 public synchronized StringBuffer append(char str[], int offset, int len) {
323 super.append(str, offset, len);
324 return this;
325 }
326
327 /**
328 * @see java.lang.String#valueOf(boolean)
329 * @see #append(java.lang.String)
330 */
331 public synchronized StringBuffer append(boolean b) {
332 super.append(b);
333 return this;
334 }
335
336 public synchronized StringBuffer append(char c) {
337 super.append(c);
338 return this;
339 }
340
341 /**
342 * @see java.lang.String#valueOf(int)
343 * @see #append(java.lang.String)
344 */
345 public synchronized StringBuffer append(int i) {
346 super.append(i);
347 return this;
348 }
349
350 /**
351 * @since 1.5
352 */
353 public synchronized StringBuffer appendCodePoint(int codePoint) {
354 super.appendCodePoint(codePoint);
355 return this;
356 }
357
358 /**
359 * @see java.lang.String#valueOf(long)
360 * @see #append(java.lang.String)
361 */
362 public synchronized StringBuffer append(long lng) {
363 super.append(lng);
364 return this;
365 }
366
367 /**
368 * @see java.lang.String#valueOf(float)
369 * @see #append(java.lang.String)
370 */
371 public synchronized StringBuffer append(float f) {
372 super.append(f);
373 return this;
374 }
375
376 /**
377 * @see java.lang.String#valueOf(double)
378 * @see #append(java.lang.String)
379 */
380 public synchronized StringBuffer append(double d) {
381 super.append(d);
382 return this;
383 }
384
385 /**
386 * @throws StringIndexOutOfBoundsException {@inheritDoc}
387 * @since 1.2
388 */
389 public synchronized StringBuffer delete(int start, int end) {
390 super.delete(start, end);
391 return this;
392 }
393
394 /**
395 * @throws StringIndexOutOfBoundsException {@inheritDoc}
396 * @since 1.2
397 */
398 public synchronized StringBuffer deleteCharAt(int index) {
399 super.deleteCharAt(index);
400 return this;
401 }
402
403 /**
404 * @throws StringIndexOutOfBoundsException {@inheritDoc}
405 * @since 1.2
406 */
407 public synchronized StringBuffer replace(int start, int end, String str) {
408 super.replace(start, end, str);
409 return this;
410 }
411
412 /**
413 * @throws StringIndexOutOfBoundsException {@inheritDoc}
414 * @since 1.2
415 */
416 public synchronized String substring(int start) {
417 return substring(start, count);
418 }
419
420 /**
421 * @throws IndexOutOfBoundsException {@inheritDoc}
422 * @since 1.4
423 */
424 public synchronized CharSequence subSequence(int start, int end) {
425 return super.substring(start, end);
426 }
427
428 /**
429 * @throws StringIndexOutOfBoundsException {@inheritDoc}
430 * @since 1.2
431 */
432 public synchronized String substring(int start, int end) {
433 return super.substring(start, end);
434 }
435
436 /**
437 * @throws StringIndexOutOfBoundsException {@inheritDoc}
438 * @since 1.2
439 */
440 public synchronized StringBuffer insert(int index, char str[], int offset,
441 int len)
442 {
443 super.insert(index, str, offset, len);
444 return this;
445 }
446
447 /**
448 * @throws StringIndexOutOfBoundsException {@inheritDoc}
449 * @see java.lang.String#valueOf(java.lang.Object)
450 * @see #insert(int, java.lang.String)
451 * @see #length()
452 */
453 public synchronized StringBuffer insert(int offset, Object obj) {
454 super.insert(offset, String.valueOf(obj));
455 return this;
456 }
457
458 /**
459 * @throws StringIndexOutOfBoundsException {@inheritDoc}
460 * @see #length()
461 */
462 public synchronized StringBuffer insert(int offset, String str) {
463 super.insert(offset, str);
464 return this;
465 }
466
467 /**
468 * @throws StringIndexOutOfBoundsException {@inheritDoc}
469 */
470 public synchronized StringBuffer insert(int offset, char str[]) {
471 super.insert(offset, str);
472 return this;
473 }
474
475 /**
476 * @throws IndexOutOfBoundsException {@inheritDoc}
477 * @since 1.5
478 */
479 public StringBuffer insert(int dstOffset, CharSequence s) {
480 // Note, synchronization achieved via other invocations
481 if (s == null)
482 s = "null";
483 if (s instanceof String)
484 return this.insert(dstOffset, (String)s);
485 return this.insert(dstOffset, s, 0, s.length());
486 }
487
488 /**
489 * @throws IndexOutOfBoundsException {@inheritDoc}
490 * @since 1.5
491 */
492 public synchronized StringBuffer insert(int dstOffset, CharSequence s,
493 int start, int end)
494 {
495 super.insert(dstOffset, s, start, end);
496 return this;
497 }
498
499 /**
500 * @throws StringIndexOutOfBoundsException {@inheritDoc}
501 * @see java.lang.String#valueOf(boolean)
502 * @see #insert(int, java.lang.String)
503 * @see #length()
504 */
505 public StringBuffer insert(int offset, boolean b) {
506 return insert(offset, String.valueOf(b));
507 }
508
509 /**
510 * @throws IndexOutOfBoundsException {@inheritDoc}
511 * @see #length()
512 */
513 public synchronized StringBuffer insert(int offset, char c) {
514 super.insert(offset, c);
515 return this;
516 }
517
518 /**
519 * @throws StringIndexOutOfBoundsException {@inheritDoc}
520 * @see java.lang.String#valueOf(int)
521 * @see #insert(int, java.lang.String)
522 * @see #length()
523 */
524 public StringBuffer insert(int offset, int i) {
525 return insert(offset, String.valueOf(i));
526 }
527
528 /**
529 * @throws StringIndexOutOfBoundsException {@inheritDoc}
530 * @see java.lang.String#valueOf(long)
531 * @see #insert(int, java.lang.String)
532 * @see #length()
533 */
534 public StringBuffer insert(int offset, long l) {
535 return insert(offset, String.valueOf(l));
536 }
537
538 /**
539 * @throws StringIndexOutOfBoundsException {@inheritDoc}
540 * @see java.lang.String#valueOf(float)
541 * @see #insert(int, java.lang.String)
542 * @see #length()
543 */
544 public StringBuffer insert(int offset, float f) {
545 return insert(offset, String.valueOf(f));
546 }
547
548 /**
549 * @throws StringIndexOutOfBoundsException {@inheritDoc}
550 * @see java.lang.String#valueOf(double)
551 * @see #insert(int, java.lang.String)
552 * @see #length()
553 */
554 public StringBuffer insert(int offset, double d) {
555 return insert(offset, String.valueOf(d));
556 }
557
558 /**
559 * @throws NullPointerException {@inheritDoc}
560 * @since 1.4
561 */
562 public int indexOf(String str) {
563 return indexOf(str, 0);
564 }
565
566 /**
567 * @throws NullPointerException {@inheritDoc}
568 * @since 1.4
569 */
570 public synchronized int indexOf(String str, int fromIndex) {
571 return String.indexOf(value, 0, count,
572 str.toCharArray(), 0, str.length(), fromIndex);
573 }
574
575 /**
576 * @throws NullPointerException {@inheritDoc}
577 * @since 1.4
578 */
579 public int lastIndexOf(String str) {
580 // Note, synchronization achieved via other invocations
581 return lastIndexOf(str, count);
582 }
583
584 /**
585 * @throws NullPointerException {@inheritDoc}
586 * @since 1.4
587 */
588 public synchronized int lastIndexOf(String str, int fromIndex) {
589 return String.lastIndexOf(value, 0, count,
590 str.toCharArray(), 0, str.length(), fromIndex);
591 }
592
593 /**
594 * @since JDK1.0.2
595 */
596 public synchronized StringBuffer reverse() {
597 super.reverse();
598 return this;
599 }
600
601 public synchronized String toString() {
602 return new String(value, 0, count);
603 }
604
605 /**
606 * Serializable fields for StringBuffer.
607 *
608 * @serialField value char[]
609 * The backing character array of this StringBuffer.
610 * @serialField count int
611 * The number of characters in this StringBuffer.
612 * @serialField shared boolean
613 * A flag indicating whether the backing array is shared.
614 * The value is ignored upon deserialization.
615 */
616 private static final java.io.ObjectStreamField[] serialPersistentFields =
617 {
618 new java.io.ObjectStreamField("value", char[].class),
619 new java.io.ObjectStreamField("count", Integer.TYPE),
620 new java.io.ObjectStreamField("shared", Boolean.TYPE),
621 };
622
623 /**
624 * readObject is called to restore the state of the StringBuffer from
625 * a stream.
626 */
627 private synchronized void writeObject(java.io.ObjectOutputStream s)
628 throws java.io.IOException {
629 java.io.ObjectOutputStream.PutField fields = s.putFields();
630 fields.put("value", value);
631 fields.put("count", count);
632 fields.put("shared", false);
633 s.writeFields();
634 }
635
636 /**
637 * readObject is called to restore the state of the StringBuffer from
638 * a stream.
639 */
640 private void readObject(java.io.ObjectInputStream s)
641 throws java.io.IOException, ClassNotFoundException {
642 java.io.ObjectInputStream.GetField fields = s.readFields();
643 value = (char[])fields.get("value", null);
644 count = fields.get("count", 0);
645 }
646 }