1 /*
2 * Copyright (c) 1994, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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} are the
43 * {@code append} and {@code insert} 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} method always adds these characters at the end
81 * Unless otherwise noted, passing a {@code null} argument to a constructor
82 * or method in this class will cause a {@link NullPointerException} to be
83 * thrown.
84 * <p>
85 * As of release JDK 5, this class has been supplemented with an equivalent
86 * class designed for use by a single thread, {@link StringBuilder}. The
87 * {@code StringBuilder} class should generally be used in preference to
88 * this one, as it supports all of the same operations but it is faster, as
89 * it performs no synchronization.
90 *
91 * @author Arthur van Hoff
92 * @see java.lang.StringBuilder
93 * @see java.lang.String
94 * @since JDK1.0
95 */
96 public final class StringBuffer
97 extends AbstractStringBuilder
98 implements java.io.Serializable, CharSequence
99 {
100
101 /** use serialVersionUID from JDK 1.0.2 for interoperability */
102 static final long serialVersionUID = 3388685877147921107L;
103
104 /**
105 * Constructs a string buffer with no characters in it and an
106 * initial capacity of 16 characters.
107 */
108 public StringBuffer() {
109 super(16);
110 }
111
112 /**
113 * Constructs a string buffer with no characters in it and
114 * the specified initial capacity.
115 *
116 * @param capacity the initial capacity.
117 * @exception NegativeArraySizeException if the {@code capacity}
118 * argument is less than {@code 0}.
119 */
120 public StringBuffer(int capacity) {
167 if (minimumCapacity > value.length) {
168 expandCapacity(minimumCapacity);
169 }
170 }
171
172 /**
173 * @since 1.5
174 */
175 @Override
176 public synchronized void trimToSize() {
177 super.trimToSize();
178 }
179
180 /**
181 * @throws IndexOutOfBoundsException {@inheritDoc}
182 * @see #length()
183 */
184 @Override
185 public synchronized void setLength(int newLength) {
186 super.setLength(newLength);
187 }
188
189 /**
190 * @throws IndexOutOfBoundsException {@inheritDoc}
191 * @see #length()
192 */
193 @Override
194 public synchronized char charAt(int index) {
195 if ((index < 0) || (index >= count))
196 throw new StringIndexOutOfBoundsException(index);
197 return value[index];
198 }
199
200 /**
201 * @since 1.5
202 */
203 @Override
204 public synchronized int codePointAt(int index) {
205 return super.codePointAt(index);
206 }
231
232 /**
233 * @throws IndexOutOfBoundsException {@inheritDoc}
234 */
235 @Override
236 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
237 int dstBegin)
238 {
239 super.getChars(srcBegin, srcEnd, dst, dstBegin);
240 }
241
242 /**
243 * @throws IndexOutOfBoundsException {@inheritDoc}
244 * @see #length()
245 */
246 @Override
247 public synchronized void setCharAt(int index, char ch) {
248 if ((index < 0) || (index >= count))
249 throw new StringIndexOutOfBoundsException(index);
250 value[index] = ch;
251 }
252
253 @Override
254 public synchronized StringBuffer append(Object obj) {
255 super.append(String.valueOf(obj));
256 return this;
257 }
258
259 @Override
260 public synchronized StringBuffer append(String str) {
261 super.append(str);
262 return this;
263 }
264
265 /**
266 * Appends the specified {@code StringBuffer} to this sequence.
267 * <p>
268 * The characters of the {@code StringBuffer} argument are appended,
269 * in order, to the contents of this {@code StringBuffer}, increasing the
270 * length of this {@code StringBuffer} by the length of the argument.
271 * If {@code sb} is {@code null}, then the four characters
272 * {@code "null"} are appended to this {@code StringBuffer}.
273 * <p>
274 * Let <i>n</i> be the length of the old character sequence, the one
275 * contained in the {@code StringBuffer} just prior to execution of the
276 * {@code append} method. Then the character at index <i>k</i> in
277 * the new character sequence is equal to the character at index <i>k</i>
278 * in the old character sequence, if <i>k</i> is less than <i>n</i>;
279 * otherwise, it is equal to the character at index <i>k-n</i> in the
280 * argument {@code sb}.
281 * <p>
282 * This method synchronizes on {@code this}, the destination
283 * object, but does not synchronize on the source ({@code sb}).
284 *
285 * @param sb the {@code StringBuffer} to append.
286 * @return a reference to this object.
287 * @since 1.4
288 */
289 public synchronized StringBuffer append(StringBuffer sb) {
290 super.append(sb);
291 return this;
292 }
293
294 /**
295 * @since 1.8
296 */
297 @Override
298 synchronized StringBuffer append(AbstractStringBuilder asb) {
299 super.append(asb);
300 return this;
301 }
302
303 /**
304 * Appends the specified {@code CharSequence} to this
305 * sequence.
306 * <p>
307 * The characters of the {@code CharSequence} argument are appended,
308 * in order, increasing the length of this sequence by the length of the
309 * argument.
310 *
311 * <p>The result of this method is exactly the same as if it were an
312 * invocation of this.append(s, 0, s.length());
313 *
314 * <p>This method synchronizes on {@code this}, the destination
315 * object, but does not synchronize on the source ({@code s}).
316 *
317 * <p>If {@code s} is {@code null}, then the four characters
318 * {@code "null"} are appended.
319 *
320 * @param s the {@code CharSequence} to append.
321 * @return a reference to this object.
322 * @since 1.5
323 */
324 @Override
325 public StringBuffer append(CharSequence s) {
326 // Note, synchronization achieved via invocations of other StringBuffer methods after
327 // narrowing of s to specific type
328 super.append(s);
329 return this;
330 }
331
332 /**
333 * @throws IndexOutOfBoundsException {@inheritDoc}
334 * @since 1.5
335 */
336 @Override
337 public synchronized StringBuffer append(CharSequence s, int start, int end)
338 {
339 super.append(s, start, end);
340 return this;
341 }
342
343 @Override
344 public synchronized StringBuffer append(char[] str) {
345 super.append(str);
346 return this;
347 }
348
349 /**
350 * @throws IndexOutOfBoundsException {@inheritDoc}
351 */
352 @Override
353 public synchronized StringBuffer append(char[] str, int offset, int len) {
354 super.append(str, offset, len);
355 return this;
356 }
357
358 @Override
359 public synchronized StringBuffer append(boolean b) {
360 super.append(b);
361 return this;
362 }
363
364 @Override
365 public synchronized StringBuffer append(char c) {
366 super.append(c);
367 return this;
368 }
369
370 @Override
371 public synchronized StringBuffer append(int i) {
372 super.append(i);
373 return this;
374 }
375
376 /**
377 * @since 1.5
378 */
379 @Override
380 public synchronized StringBuffer appendCodePoint(int codePoint) {
381 super.appendCodePoint(codePoint);
382 return this;
383 }
384
385 @Override
386 public synchronized StringBuffer append(long lng) {
387 super.append(lng);
388 return this;
389 }
390
391 @Override
392 public synchronized StringBuffer append(float f) {
393 super.append(f);
394 return this;
395 }
396
397 @Override
398 public synchronized StringBuffer append(double d) {
399 super.append(d);
400 return this;
401 }
402
403 /**
404 * @throws StringIndexOutOfBoundsException {@inheritDoc}
405 * @since 1.2
406 */
407 @Override
408 public synchronized StringBuffer delete(int start, int end) {
409 super.delete(start, end);
410 return this;
411 }
412
413 /**
414 * @throws StringIndexOutOfBoundsException {@inheritDoc}
415 * @since 1.2
416 */
417 @Override
418 public synchronized StringBuffer deleteCharAt(int index) {
419 super.deleteCharAt(index);
420 return this;
421 }
422
423 /**
424 * @throws StringIndexOutOfBoundsException {@inheritDoc}
425 * @since 1.2
426 */
427 @Override
428 public synchronized StringBuffer replace(int start, int end, String str) {
429 super.replace(start, end, str);
430 return this;
431 }
432
433 /**
434 * @throws StringIndexOutOfBoundsException {@inheritDoc}
435 * @since 1.2
436 */
437 @Override
438 public synchronized String substring(int start) {
439 return substring(start, count);
440 }
441
442 /**
443 * @throws IndexOutOfBoundsException {@inheritDoc}
444 * @since 1.4
445 */
446 @Override
447 public synchronized CharSequence subSequence(int start, int end) {
448 return super.substring(start, end);
449 }
450
451 /**
452 * @throws StringIndexOutOfBoundsException {@inheritDoc}
453 * @since 1.2
454 */
455 @Override
456 public synchronized String substring(int start, int end) {
457 return super.substring(start, end);
458 }
459
460 /**
461 * @throws StringIndexOutOfBoundsException {@inheritDoc}
462 * @since 1.2
463 */
464 @Override
465 public synchronized StringBuffer insert(int index, char[] str, int offset,
466 int len)
467 {
468 super.insert(index, str, offset, len);
469 return this;
470 }
471
472 /**
473 * @throws StringIndexOutOfBoundsException {@inheritDoc}
474 */
475 @Override
476 public synchronized StringBuffer insert(int offset, Object obj) {
477 super.insert(offset, String.valueOf(obj));
478 return this;
479 }
480
481 /**
482 * @throws StringIndexOutOfBoundsException {@inheritDoc}
483 */
484 @Override
485 public synchronized StringBuffer insert(int offset, String str) {
486 super.insert(offset, str);
487 return this;
488 }
489
490 /**
491 * @throws StringIndexOutOfBoundsException {@inheritDoc}
492 */
493 @Override
494 public synchronized StringBuffer insert(int offset, char[] str) {
495 super.insert(offset, str);
496 return this;
497 }
498
499 /**
500 * @throws IndexOutOfBoundsException {@inheritDoc}
501 * @since 1.5
502 */
503 @Override
504 public StringBuffer insert(int dstOffset, CharSequence s) {
505 // Note, synchronization achieved via invocations of other StringBuffer methods
506 // after narrowing of s to specific type
507 super.insert(dstOffset, s);
508 return this;
509 }
510
511 /**
512 * @throws IndexOutOfBoundsException {@inheritDoc}
513 * @since 1.5
514 */
515 @Override
516 public synchronized StringBuffer insert(int dstOffset, CharSequence s,
517 int start, int end)
518 {
519 super.insert(dstOffset, s, start, end);
520 return this;
521 }
522
523 /**
524 * @throws StringIndexOutOfBoundsException {@inheritDoc}
525 */
526 @Override
527 public StringBuffer insert(int offset, boolean b) {
528 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
529 // after conversion of b to String by super class method
530 super.insert(offset, b);
531 return this;
532 }
533
534 /**
535 * @throws IndexOutOfBoundsException {@inheritDoc}
536 */
537 @Override
538 public synchronized StringBuffer insert(int offset, char c) {
539 super.insert(offset, c);
540 return this;
541 }
542
543 /**
544 * @throws StringIndexOutOfBoundsException {@inheritDoc}
545 */
546 @Override
547 public StringBuffer insert(int offset, int i) {
548 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
549 // after conversion of i to String by super class method
550 super.insert(offset, i);
551 return this;
552 }
553
554 /**
555 * @throws StringIndexOutOfBoundsException {@inheritDoc}
556 */
557 @Override
558 public StringBuffer insert(int offset, long l) {
559 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
560 // after conversion of l to String by super class method
561 super.insert(offset, l);
562 return this;
563 }
564
565 /**
566 * @throws StringIndexOutOfBoundsException {@inheritDoc}
567 */
568 @Override
569 public StringBuffer insert(int offset, float f) {
570 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
571 // after conversion of f to String by super class method
572 super.insert(offset, f);
573 return this;
574 }
575
576 /**
577 * @throws StringIndexOutOfBoundsException {@inheritDoc}
578 */
579 @Override
580 public StringBuffer insert(int offset, double d) {
581 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
582 // after conversion of d to String by super class method
583 super.insert(offset, d);
584 return this;
585 }
586
587 /**
588 * @since 1.4
589 */
590 @Override
591 public int indexOf(String str) {
592 // Note, synchronization achieved via invocations of other StringBuffer methods
593 return super.indexOf(str);
594 }
595
596 /**
597 * @since 1.4
598 */
599 @Override
600 public synchronized int indexOf(String str, int fromIndex) {
601 return super.indexOf(str, fromIndex);
602 }
607 @Override
608 public int lastIndexOf(String str) {
609 // Note, synchronization achieved via invocations of other StringBuffer methods
610 return lastIndexOf(str, count);
611 }
612
613 /**
614 * @since 1.4
615 */
616 @Override
617 public synchronized int lastIndexOf(String str, int fromIndex) {
618 return super.lastIndexOf(str, fromIndex);
619 }
620
621 /**
622 * @since JDK1.0.2
623 */
624 @Override
625 public synchronized StringBuffer reverse() {
626 super.reverse();
627 return this;
628 }
629
630 @Override
631 public synchronized String toString() {
632 return new String(value, 0, count);
633 }
634
635 /**
636 * Serializable fields for StringBuffer.
637 *
638 * @serialField value char[]
639 * The backing character array of this StringBuffer.
640 * @serialField count int
641 * The number of characters in this StringBuffer.
642 * @serialField shared boolean
643 * A flag indicating whether the backing array is shared.
644 * The value is ignored upon deserialization.
645 */
646 private static final java.io.ObjectStreamField[] serialPersistentFields =
647 {
648 new java.io.ObjectStreamField("value", char[].class),
649 new java.io.ObjectStreamField("count", Integer.TYPE),
650 new java.io.ObjectStreamField("shared", Boolean.TYPE),
651 };
652
|
1 /*
2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.util.Arrays;
29
30 /**
31 * A thread-safe, mutable sequence of characters.
32 * A string buffer is like a {@link String}, but can be modified. At any
33 * point in time it contains some particular sequence of characters, but
34 * the length and content of the sequence can be changed through certain
35 * method calls.
36 * <p>
37 * String buffers are safe for use by multiple threads. The methods
38 * are synchronized where necessary so that all the operations on any
39 * particular instance behave as if they occur in some serial order
40 * that is consistent with the order of the method calls made by each of
41 * the individual threads involved.
42 * <p>
43 * The principal operations on a {@code StringBuffer} are the
44 * {@code append} and {@code insert} methods, which are
45 * overloaded so as to accept data of any type. Each effectively
46 * converts a given datum to a string and then appends or inserts the
47 * characters of that string to the string buffer. The
48 * {@code append} method always adds these characters at the end
82 * Unless otherwise noted, passing a {@code null} argument to a constructor
83 * or method in this class will cause a {@link NullPointerException} to be
84 * thrown.
85 * <p>
86 * As of release JDK 5, this class has been supplemented with an equivalent
87 * class designed for use by a single thread, {@link StringBuilder}. The
88 * {@code StringBuilder} class should generally be used in preference to
89 * this one, as it supports all of the same operations but it is faster, as
90 * it performs no synchronization.
91 *
92 * @author Arthur van Hoff
93 * @see java.lang.StringBuilder
94 * @see java.lang.String
95 * @since JDK1.0
96 */
97 public final class StringBuffer
98 extends AbstractStringBuilder
99 implements java.io.Serializable, CharSequence
100 {
101
102 /**
103 * A cache of the last value returned by toString. Cleared
104 * whenever the StringBuffer is modified.
105 */
106 private transient char[] toStringCache;
107
108 /** use serialVersionUID from JDK 1.0.2 for interoperability */
109 static final long serialVersionUID = 3388685877147921107L;
110
111 /**
112 * Constructs a string buffer with no characters in it and an
113 * initial capacity of 16 characters.
114 */
115 public StringBuffer() {
116 super(16);
117 }
118
119 /**
120 * Constructs a string buffer with no characters in it and
121 * the specified initial capacity.
122 *
123 * @param capacity the initial capacity.
124 * @exception NegativeArraySizeException if the {@code capacity}
125 * argument is less than {@code 0}.
126 */
127 public StringBuffer(int capacity) {
174 if (minimumCapacity > value.length) {
175 expandCapacity(minimumCapacity);
176 }
177 }
178
179 /**
180 * @since 1.5
181 */
182 @Override
183 public synchronized void trimToSize() {
184 super.trimToSize();
185 }
186
187 /**
188 * @throws IndexOutOfBoundsException {@inheritDoc}
189 * @see #length()
190 */
191 @Override
192 public synchronized void setLength(int newLength) {
193 super.setLength(newLength);
194 toStringCache = null;
195 }
196
197 /**
198 * @throws IndexOutOfBoundsException {@inheritDoc}
199 * @see #length()
200 */
201 @Override
202 public synchronized char charAt(int index) {
203 if ((index < 0) || (index >= count))
204 throw new StringIndexOutOfBoundsException(index);
205 return value[index];
206 }
207
208 /**
209 * @since 1.5
210 */
211 @Override
212 public synchronized int codePointAt(int index) {
213 return super.codePointAt(index);
214 }
239
240 /**
241 * @throws IndexOutOfBoundsException {@inheritDoc}
242 */
243 @Override
244 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
245 int dstBegin)
246 {
247 super.getChars(srcBegin, srcEnd, dst, dstBegin);
248 }
249
250 /**
251 * @throws IndexOutOfBoundsException {@inheritDoc}
252 * @see #length()
253 */
254 @Override
255 public synchronized void setCharAt(int index, char ch) {
256 if ((index < 0) || (index >= count))
257 throw new StringIndexOutOfBoundsException(index);
258 value[index] = ch;
259 toStringCache = null;
260 }
261
262 @Override
263 public synchronized StringBuffer append(Object obj) {
264 super.append(String.valueOf(obj));
265 toStringCache = null;
266 return this;
267 }
268
269 @Override
270 public synchronized StringBuffer append(String str) {
271 super.append(str);
272 toStringCache = null;
273 return this;
274 }
275
276 /**
277 * Appends the specified {@code StringBuffer} to this sequence.
278 * <p>
279 * The characters of the {@code StringBuffer} argument are appended,
280 * in order, to the contents of this {@code StringBuffer}, increasing the
281 * length of this {@code StringBuffer} by the length of the argument.
282 * If {@code sb} is {@code null}, then the four characters
283 * {@code "null"} are appended to this {@code StringBuffer}.
284 * <p>
285 * Let <i>n</i> be the length of the old character sequence, the one
286 * contained in the {@code StringBuffer} just prior to execution of the
287 * {@code append} method. Then the character at index <i>k</i> in
288 * the new character sequence is equal to the character at index <i>k</i>
289 * in the old character sequence, if <i>k</i> is less than <i>n</i>;
290 * otherwise, it is equal to the character at index <i>k-n</i> in the
291 * argument {@code sb}.
292 * <p>
293 * This method synchronizes on {@code this}, the destination
294 * object, but does not synchronize on the source ({@code sb}).
295 *
296 * @param sb the {@code StringBuffer} to append.
297 * @return a reference to this object.
298 * @since 1.4
299 */
300 public synchronized StringBuffer append(StringBuffer sb) {
301 super.append(sb);
302 toStringCache = null;
303 return this;
304 }
305
306 /**
307 * @since 1.8
308 */
309 @Override
310 synchronized StringBuffer append(AbstractStringBuilder asb) {
311 super.append(asb);
312 toStringCache = null;
313 return this;
314 }
315
316 /**
317 * Appends the specified {@code CharSequence} to this
318 * sequence.
319 * <p>
320 * The characters of the {@code CharSequence} argument are appended,
321 * in order, increasing the length of this sequence by the length of the
322 * argument.
323 *
324 * <p>The result of this method is exactly the same as if it were an
325 * invocation of this.append(s, 0, s.length());
326 *
327 * <p>This method synchronizes on {@code this}, the destination
328 * object, but does not synchronize on the source ({@code s}).
329 *
330 * <p>If {@code s} is {@code null}, then the four characters
331 * {@code "null"} are appended.
332 *
333 * @param s the {@code CharSequence} to append.
334 * @return a reference to this object.
335 * @since 1.5
336 */
337 @Override
338 public StringBuffer append(CharSequence s) {
339 // Note, synchronization achieved via invocations of other StringBuffer methods after
340 // narrowing of s to specific type
341 // Ditto for toStringCache clearing
342 super.append(s);
343 return this;
344 }
345
346 /**
347 * @throws IndexOutOfBoundsException {@inheritDoc}
348 * @since 1.5
349 */
350 @Override
351 public synchronized StringBuffer append(CharSequence s, int start, int end)
352 {
353 super.append(s, start, end);
354 toStringCache = null;
355 return this;
356 }
357
358 @Override
359 public synchronized StringBuffer append(char[] str) {
360 super.append(str);
361 toStringCache = null;
362 return this;
363 }
364
365 /**
366 * @throws IndexOutOfBoundsException {@inheritDoc}
367 */
368 @Override
369 public synchronized StringBuffer append(char[] str, int offset, int len) {
370 super.append(str, offset, len);
371 toStringCache = null;
372 return this;
373 }
374
375 @Override
376 public synchronized StringBuffer append(boolean b) {
377 super.append(b);
378 toStringCache = null;
379 return this;
380 }
381
382 @Override
383 public synchronized StringBuffer append(char c) {
384 super.append(c);
385 toStringCache = null;
386 return this;
387 }
388
389 @Override
390 public synchronized StringBuffer append(int i) {
391 super.append(i);
392 toStringCache = null;
393 return this;
394 }
395
396 /**
397 * @since 1.5
398 */
399 @Override
400 public synchronized StringBuffer appendCodePoint(int codePoint) {
401 super.appendCodePoint(codePoint);
402 toStringCache = null;
403 return this;
404 }
405
406 @Override
407 public synchronized StringBuffer append(long lng) {
408 super.append(lng);
409 toStringCache = null;
410 return this;
411 }
412
413 @Override
414 public synchronized StringBuffer append(float f) {
415 super.append(f);
416 toStringCache = null;
417 return this;
418 }
419
420 @Override
421 public synchronized StringBuffer append(double d) {
422 super.append(d);
423 toStringCache = null;
424 return this;
425 }
426
427 /**
428 * @throws StringIndexOutOfBoundsException {@inheritDoc}
429 * @since 1.2
430 */
431 @Override
432 public synchronized StringBuffer delete(int start, int end) {
433 super.delete(start, end);
434 toStringCache = null;
435 return this;
436 }
437
438 /**
439 * @throws StringIndexOutOfBoundsException {@inheritDoc}
440 * @since 1.2
441 */
442 @Override
443 public synchronized StringBuffer deleteCharAt(int index) {
444 super.deleteCharAt(index);
445 toStringCache = null;
446 return this;
447 }
448
449 /**
450 * @throws StringIndexOutOfBoundsException {@inheritDoc}
451 * @since 1.2
452 */
453 @Override
454 public synchronized StringBuffer replace(int start, int end, String str) {
455 super.replace(start, end, str);
456 toStringCache = null;
457 return this;
458 }
459
460 /**
461 * @throws StringIndexOutOfBoundsException {@inheritDoc}
462 * @since 1.2
463 */
464 @Override
465 public synchronized String substring(int start) {
466 return substring(start, count);
467 }
468
469 /**
470 * @throws IndexOutOfBoundsException {@inheritDoc}
471 * @since 1.4
472 */
473 @Override
474 public synchronized CharSequence subSequence(int start, int end) {
475 return super.substring(start, end);
476 }
477
478 /**
479 * @throws StringIndexOutOfBoundsException {@inheritDoc}
480 * @since 1.2
481 */
482 @Override
483 public synchronized String substring(int start, int end) {
484 return super.substring(start, end);
485 }
486
487 /**
488 * @throws StringIndexOutOfBoundsException {@inheritDoc}
489 * @since 1.2
490 */
491 @Override
492 public synchronized StringBuffer insert(int index, char[] str, int offset,
493 int len)
494 {
495 super.insert(index, str, offset, len);
496 toStringCache = null;
497 return this;
498 }
499
500 /**
501 * @throws StringIndexOutOfBoundsException {@inheritDoc}
502 */
503 @Override
504 public synchronized StringBuffer insert(int offset, Object obj) {
505 super.insert(offset, String.valueOf(obj));
506 toStringCache = null;
507 return this;
508 }
509
510 /**
511 * @throws StringIndexOutOfBoundsException {@inheritDoc}
512 */
513 @Override
514 public synchronized StringBuffer insert(int offset, String str) {
515 super.insert(offset, str);
516 toStringCache = null;
517 return this;
518 }
519
520 /**
521 * @throws StringIndexOutOfBoundsException {@inheritDoc}
522 */
523 @Override
524 public synchronized StringBuffer insert(int offset, char[] str) {
525 super.insert(offset, str);
526 toStringCache = null;
527 return this;
528 }
529
530 /**
531 * @throws IndexOutOfBoundsException {@inheritDoc}
532 * @since 1.5
533 */
534 @Override
535 public StringBuffer insert(int dstOffset, CharSequence s) {
536 // Note, synchronization achieved via invocations of other StringBuffer methods
537 // after narrowing of s to specific type
538 // Ditto for toStringCache clearing
539 super.insert(dstOffset, s);
540 return this;
541 }
542
543 /**
544 * @throws IndexOutOfBoundsException {@inheritDoc}
545 * @since 1.5
546 */
547 @Override
548 public synchronized StringBuffer insert(int dstOffset, CharSequence s,
549 int start, int end)
550 {
551 super.insert(dstOffset, s, start, end);
552 toStringCache = null;
553 return this;
554 }
555
556 /**
557 * @throws StringIndexOutOfBoundsException {@inheritDoc}
558 */
559 @Override
560 public StringBuffer insert(int offset, boolean b) {
561 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
562 // after conversion of b to String by super class method
563 // Ditto for toStringCache clearing
564 super.insert(offset, b);
565 return this;
566 }
567
568 /**
569 * @throws IndexOutOfBoundsException {@inheritDoc}
570 */
571 @Override
572 public synchronized StringBuffer insert(int offset, char c) {
573 super.insert(offset, c);
574 toStringCache = null;
575 return this;
576 }
577
578 /**
579 * @throws StringIndexOutOfBoundsException {@inheritDoc}
580 */
581 @Override
582 public StringBuffer insert(int offset, int i) {
583 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
584 // after conversion of i to String by super class method
585 // Ditto for toStringCache clearing
586 super.insert(offset, i);
587 return this;
588 }
589
590 /**
591 * @throws StringIndexOutOfBoundsException {@inheritDoc}
592 */
593 @Override
594 public StringBuffer insert(int offset, long l) {
595 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
596 // after conversion of l to String by super class method
597 // Ditto for toStringCache clearing
598 super.insert(offset, l);
599 return this;
600 }
601
602 /**
603 * @throws StringIndexOutOfBoundsException {@inheritDoc}
604 */
605 @Override
606 public StringBuffer insert(int offset, float f) {
607 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
608 // after conversion of f to String by super class method
609 // Ditto for toStringCache clearing
610 super.insert(offset, f);
611 return this;
612 }
613
614 /**
615 * @throws StringIndexOutOfBoundsException {@inheritDoc}
616 */
617 @Override
618 public StringBuffer insert(int offset, double d) {
619 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
620 // after conversion of d to String by super class method
621 // Ditto for toStringCache clearing
622 super.insert(offset, d);
623 return this;
624 }
625
626 /**
627 * @since 1.4
628 */
629 @Override
630 public int indexOf(String str) {
631 // Note, synchronization achieved via invocations of other StringBuffer methods
632 return super.indexOf(str);
633 }
634
635 /**
636 * @since 1.4
637 */
638 @Override
639 public synchronized int indexOf(String str, int fromIndex) {
640 return super.indexOf(str, fromIndex);
641 }
646 @Override
647 public int lastIndexOf(String str) {
648 // Note, synchronization achieved via invocations of other StringBuffer methods
649 return lastIndexOf(str, count);
650 }
651
652 /**
653 * @since 1.4
654 */
655 @Override
656 public synchronized int lastIndexOf(String str, int fromIndex) {
657 return super.lastIndexOf(str, fromIndex);
658 }
659
660 /**
661 * @since JDK1.0.2
662 */
663 @Override
664 public synchronized StringBuffer reverse() {
665 super.reverse();
666 toStringCache = null;
667 return this;
668 }
669
670 @Override
671 public synchronized String toString() {
672 if (toStringCache == null) {
673 toStringCache = Arrays.copyOfRange(value, 0, count);
674 }
675 return new String(toStringCache, true);
676 }
677
678 /**
679 * Serializable fields for StringBuffer.
680 *
681 * @serialField value char[]
682 * The backing character array of this StringBuffer.
683 * @serialField count int
684 * The number of characters in this StringBuffer.
685 * @serialField shared boolean
686 * A flag indicating whether the backing array is shared.
687 * The value is ignored upon deserialization.
688 */
689 private static final java.io.ObjectStreamField[] serialPersistentFields =
690 {
691 new java.io.ObjectStreamField("value", char[].class),
692 new java.io.ObjectStreamField("count", Integer.TYPE),
693 new java.io.ObjectStreamField("shared", Boolean.TYPE),
694 };
695
|