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
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) {
121 super(capacity);
122 }
123
124 /**
149 public StringBuffer(CharSequence seq) {
150 this(seq.length() + 16);
151 append(seq);
152 }
153
154 @Override
155 public synchronized int length() {
156 return count;
157 }
158
159 @Override
160 public synchronized int capacity() {
161 return value.length;
162 }
163
164
165 @Override
166 public synchronized void ensureCapacity(int minimumCapacity) {
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);
230 }
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 }
606 */
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
655 * a stream.
656 */
657 private synchronized void writeObject(java.io.ObjectOutputStream s)
658 throws java.io.IOException {
659 java.io.ObjectOutputStream.PutField fields = s.putFields();
660 fields.put("value", value);
661 fields.put("count", count);
662 fields.put("shared", false);
663 s.writeFields();
664 }
665
666 /**
667 * readObject is called to restore the state of the StringBuffer from
668 * a stream.
669 */
670 private void readObject(java.io.ObjectInputStream s)
671 throws java.io.IOException, ClassNotFoundException {
672 java.io.ObjectInputStream.GetField fields = s.readFields();
673 value = (char[])fields.get("value", null);
674 count = fields.get("count", 0);
675 }
676 }
|
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
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 /** use serialVersionUID from JDK 1.0.2 for interoperability */
103 static final long serialVersionUID = 3388685877147921107L;
104
105 /**
106 * Tracks if our char[] has been shared with a String returned
107 * from toString(). We implement a copy-on-write-if-shared scheme
108 * so that we can avoid an array-copy on toString. Each mutating
109 * operation copies the char[] if it is being shared.
110 */
111 private transient boolean isShared = false;
112
113 /**
114 * Performs the copy-on-write-if-shared update
115 */
116 private void COWIS() {
117 if (isShared) {
118 value = Arrays.copyOf(value, value.length);
119 isShared = false;
120 }
121 }
122 /**
123 * Constructs a string buffer with no characters in it and an
124 * initial capacity of 16 characters.
125 */
126 public StringBuffer() {
127 super(16);
128 }
129
130 /**
131 * Constructs a string buffer with no characters in it and
132 * the specified initial capacity.
133 *
134 * @param capacity the initial capacity.
135 * @exception NegativeArraySizeException if the {@code capacity}
136 * argument is less than {@code 0}.
137 */
138 public StringBuffer(int capacity) {
139 super(capacity);
140 }
141
142 /**
167 public StringBuffer(CharSequence seq) {
168 this(seq.length() + 16);
169 append(seq);
170 }
171
172 @Override
173 public synchronized int length() {
174 return count;
175 }
176
177 @Override
178 public synchronized int capacity() {
179 return value.length;
180 }
181
182
183 @Override
184 public synchronized void ensureCapacity(int minimumCapacity) {
185 if (minimumCapacity > value.length) {
186 expandCapacity(minimumCapacity);
187 isShared = false;
188 }
189 }
190
191 /**
192 * @since 1.5
193 */
194 @Override
195 public synchronized void trimToSize() {
196 char[] old = value;
197 super.trimToSize();
198 if (old != value) {
199 isShared = false;
200 }
201 }
202
203 /**
204 * @throws IndexOutOfBoundsException {@inheritDoc}
205 * @see #length()
206 */
207 @Override
208 public synchronized void setLength(int newLength) {
209 COWIS();
210 super.setLength(newLength);
211 }
212
213 /**
214 * @throws IndexOutOfBoundsException {@inheritDoc}
215 * @see #length()
216 */
217 @Override
218 public synchronized char charAt(int index) {
219 if ((index < 0) || (index >= count))
220 throw new StringIndexOutOfBoundsException(index);
221 return value[index];
222 }
223
224 /**
225 * @since 1.5
226 */
227 @Override
228 public synchronized int codePointAt(int index) {
229 return super.codePointAt(index);
254 }
255
256 /**
257 * @throws IndexOutOfBoundsException {@inheritDoc}
258 */
259 @Override
260 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
261 int dstBegin)
262 {
263 super.getChars(srcBegin, srcEnd, dst, dstBegin);
264 }
265
266 /**
267 * @throws IndexOutOfBoundsException {@inheritDoc}
268 * @see #length()
269 */
270 @Override
271 public synchronized void setCharAt(int index, char ch) {
272 if ((index < 0) || (index >= count))
273 throw new StringIndexOutOfBoundsException(index);
274 COWIS();
275 value[index] = ch;
276 }
277
278 @Override
279 public synchronized StringBuffer append(Object obj) {
280 COWIS();
281 super.append(String.valueOf(obj));
282 return this;
283 }
284
285 @Override
286 public synchronized StringBuffer append(String str) {
287 COWIS();
288 super.append(str);
289 return this;
290 }
291
292 /**
293 * Appends the specified {@code StringBuffer} to this sequence.
294 * <p>
295 * The characters of the {@code StringBuffer} argument are appended,
296 * in order, to the contents of this {@code StringBuffer}, increasing the
297 * length of this {@code StringBuffer} by the length of the argument.
298 * If {@code sb} is {@code null}, then the four characters
299 * {@code "null"} are appended to this {@code StringBuffer}.
300 * <p>
301 * Let <i>n</i> be the length of the old character sequence, the one
302 * contained in the {@code StringBuffer} just prior to execution of the
303 * {@code append} method. Then the character at index <i>k</i> in
304 * the new character sequence is equal to the character at index <i>k</i>
305 * in the old character sequence, if <i>k</i> is less than <i>n</i>;
306 * otherwise, it is equal to the character at index <i>k-n</i> in the
307 * argument {@code sb}.
308 * <p>
309 * This method synchronizes on {@code this}, the destination
310 * object, but does not synchronize on the source ({@code sb}).
311 *
312 * @param sb the {@code StringBuffer} to append.
313 * @return a reference to this object.
314 * @since 1.4
315 */
316 public synchronized StringBuffer append(StringBuffer sb) {
317 COWIS();
318 super.append(sb);
319 return this;
320 }
321
322 /**
323 * @since 1.8
324 */
325 @Override
326 synchronized StringBuffer append(AbstractStringBuilder asb) {
327 COWIS();
328 super.append(asb);
329 return this;
330 }
331
332 /**
333 * Appends the specified {@code CharSequence} to this
334 * sequence.
335 * <p>
336 * The characters of the {@code CharSequence} argument are appended,
337 * in order, increasing the length of this sequence by the length of the
338 * argument.
339 *
340 * <p>The result of this method is exactly the same as if it were an
341 * invocation of this.append(s, 0, s.length());
342 *
343 * <p>This method synchronizes on {@code this}, the destination
344 * object, but does not synchronize on the source ({@code s}).
345 *
346 * <p>If {@code s} is {@code null}, then the four characters
347 * {@code "null"} are appended.
348 *
349 * @param s the {@code CharSequence} to append.
350 * @return a reference to this object.
351 * @since 1.5
352 */
353 @Override
354 public StringBuffer append(CharSequence s) {
355 // Note, synchronization achieved via invocations of other StringBuffer methods after
356 // narrowing of s to specific type
357 // Ditto for COWIS
358 super.append(s);
359 return this;
360 }
361
362 /**
363 * @throws IndexOutOfBoundsException {@inheritDoc}
364 * @since 1.5
365 */
366 @Override
367 public synchronized StringBuffer append(CharSequence s, int start, int end)
368 {
369 COWIS();
370 super.append(s, start, end);
371 return this;
372 }
373
374 @Override
375 public synchronized StringBuffer append(char[] str) {
376 COWIS();
377 super.append(str);
378 return this;
379 }
380
381 /**
382 * @throws IndexOutOfBoundsException {@inheritDoc}
383 */
384 @Override
385 public synchronized StringBuffer append(char[] str, int offset, int len) {
386 COWIS();
387 super.append(str, offset, len);
388 return this;
389 }
390
391 @Override
392 public synchronized StringBuffer append(boolean b) {
393 COWIS();
394 super.append(b);
395 return this;
396 }
397
398 @Override
399 public synchronized StringBuffer append(char c) {
400 COWIS();
401 super.append(c);
402 return this;
403 }
404
405 @Override
406 public synchronized StringBuffer append(int i) {
407 COWIS();
408 super.append(i);
409 return this;
410 }
411
412 /**
413 * @since 1.5
414 */
415 @Override
416 public synchronized StringBuffer appendCodePoint(int codePoint) {
417 COWIS();
418 super.appendCodePoint(codePoint);
419 return this;
420 }
421
422 @Override
423 public synchronized StringBuffer append(long lng) {
424 COWIS();
425 super.append(lng);
426 return this;
427 }
428
429 @Override
430 public synchronized StringBuffer append(float f) {
431 COWIS();
432 super.append(f);
433 return this;
434 }
435
436 @Override
437 public synchronized StringBuffer append(double d) {
438 COWIS();
439 super.append(d);
440 return this;
441 }
442
443 /**
444 * @throws StringIndexOutOfBoundsException {@inheritDoc}
445 * @since 1.2
446 */
447 @Override
448 public synchronized StringBuffer delete(int start, int end) {
449 COWIS();
450 super.delete(start, end);
451 return this;
452 }
453
454 /**
455 * @throws StringIndexOutOfBoundsException {@inheritDoc}
456 * @since 1.2
457 */
458 @Override
459 public synchronized StringBuffer deleteCharAt(int index) {
460 COWIS();
461 super.deleteCharAt(index);
462 return this;
463 }
464
465 /**
466 * @throws StringIndexOutOfBoundsException {@inheritDoc}
467 * @since 1.2
468 */
469 @Override
470 public synchronized StringBuffer replace(int start, int end, String str) {
471 COWIS();
472 super.replace(start, end, str);
473 return this;
474 }
475
476 /**
477 * @throws StringIndexOutOfBoundsException {@inheritDoc}
478 * @since 1.2
479 */
480 @Override
481 public synchronized String substring(int start) {
482 return substring(start, count);
483 }
484
485 /**
486 * @throws IndexOutOfBoundsException {@inheritDoc}
487 * @since 1.4
488 */
489 @Override
490 public synchronized CharSequence subSequence(int start, int end) {
491 return super.substring(start, end);
492 }
493
494 /**
495 * @throws StringIndexOutOfBoundsException {@inheritDoc}
496 * @since 1.2
497 */
498 @Override
499 public synchronized String substring(int start, int end) {
500 return super.substring(start, end);
501 }
502
503 /**
504 * @throws StringIndexOutOfBoundsException {@inheritDoc}
505 * @since 1.2
506 */
507 @Override
508 public synchronized StringBuffer insert(int index, char[] str, int offset,
509 int len)
510 {
511 COWIS();
512 super.insert(index, str, offset, len);
513 return this;
514 }
515
516 /**
517 * @throws StringIndexOutOfBoundsException {@inheritDoc}
518 */
519 @Override
520 public synchronized StringBuffer insert(int offset, Object obj) {
521 COWIS();
522 super.insert(offset, String.valueOf(obj));
523 return this;
524 }
525
526 /**
527 * @throws StringIndexOutOfBoundsException {@inheritDoc}
528 */
529 @Override
530 public synchronized StringBuffer insert(int offset, String str) {
531 COWIS();
532 super.insert(offset, str);
533 return this;
534 }
535
536 /**
537 * @throws StringIndexOutOfBoundsException {@inheritDoc}
538 */
539 @Override
540 public synchronized StringBuffer insert(int offset, char[] str) {
541 COWIS();
542 super.insert(offset, str);
543 return this;
544 }
545
546 /**
547 * @throws IndexOutOfBoundsException {@inheritDoc}
548 * @since 1.5
549 */
550 @Override
551 public StringBuffer insert(int dstOffset, CharSequence s) {
552 // Note, synchronization achieved via invocations of other StringBuffer methods
553 // after narrowing of s to specific type
554 // Ditto for COWIS
555 super.insert(dstOffset, s);
556 return this;
557 }
558
559 /**
560 * @throws IndexOutOfBoundsException {@inheritDoc}
561 * @since 1.5
562 */
563 @Override
564 public synchronized StringBuffer insert(int dstOffset, CharSequence s,
565 int start, int end)
566 {
567 COWIS();
568 super.insert(dstOffset, s, start, end);
569 return this;
570 }
571
572 /**
573 * @throws StringIndexOutOfBoundsException {@inheritDoc}
574 */
575 @Override
576 public StringBuffer insert(int offset, boolean b) {
577 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
578 // after conversion of b to String by super class method
579 // Ditto for COWIS
580 super.insert(offset, b);
581 return this;
582 }
583
584 /**
585 * @throws IndexOutOfBoundsException {@inheritDoc}
586 */
587 @Override
588 public synchronized StringBuffer insert(int offset, char c) {
589 COWIS();
590 super.insert(offset, c);
591 return this;
592 }
593
594 /**
595 * @throws StringIndexOutOfBoundsException {@inheritDoc}
596 */
597 @Override
598 public StringBuffer insert(int offset, int i) {
599 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
600 // after conversion of i to String by super class method
601 // Ditto for COWIS
602 super.insert(offset, i);
603 return this;
604 }
605
606 /**
607 * @throws StringIndexOutOfBoundsException {@inheritDoc}
608 */
609 @Override
610 public StringBuffer insert(int offset, long l) {
611 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
612 // after conversion of l to String by super class method
613 // Ditto for COWIS
614 super.insert(offset, l);
615 return this;
616 }
617
618 /**
619 * @throws StringIndexOutOfBoundsException {@inheritDoc}
620 */
621 @Override
622 public StringBuffer insert(int offset, float f) {
623 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
624 // after conversion of f to String by super class method
625 // Ditto for COWIS
626 super.insert(offset, f);
627 return this;
628 }
629
630 /**
631 * @throws StringIndexOutOfBoundsException {@inheritDoc}
632 */
633 @Override
634 public StringBuffer insert(int offset, double d) {
635 // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
636 // after conversion of d to String by super class method
637 // Ditto for COWIS
638 super.insert(offset, d);
639 return this;
640 }
641
642 /**
643 * @since 1.4
644 */
645 @Override
646 public int indexOf(String str) {
647 // Note, synchronization achieved via invocations of other StringBuffer methods
648 return super.indexOf(str);
649 }
650
651 /**
652 * @since 1.4
653 */
654 @Override
655 public synchronized int indexOf(String str, int fromIndex) {
656 return super.indexOf(str, fromIndex);
657 }
661 */
662 @Override
663 public int lastIndexOf(String str) {
664 // Note, synchronization achieved via invocations of other StringBuffer methods
665 return lastIndexOf(str, count);
666 }
667
668 /**
669 * @since 1.4
670 */
671 @Override
672 public synchronized int lastIndexOf(String str, int fromIndex) {
673 return super.lastIndexOf(str, fromIndex);
674 }
675
676 /**
677 * @since JDK1.0.2
678 */
679 @Override
680 public synchronized StringBuffer reverse() {
681 COWIS();
682 super.reverse();
683 return this;
684 }
685
686 @Override
687 public synchronized String toString() {
688 isShared = true;
689 return new String(value, true);
690 }
691
692 /**
693 * Serializable fields for StringBuffer.
694 *
695 * @serialField value char[]
696 * The backing character array of this StringBuffer.
697 * @serialField count int
698 * The number of characters in this StringBuffer.
699 * @serialField shared boolean
700 * A flag indicating whether the backing array is shared.
701 * The value is ignored upon deserialization.
702 */
703 private static final java.io.ObjectStreamField[] serialPersistentFields =
704 {
705 new java.io.ObjectStreamField("value", char[].class),
706 new java.io.ObjectStreamField("count", Integer.TYPE),
707 new java.io.ObjectStreamField("shared", Boolean.TYPE),
708 };
709
712 * a stream.
713 */
714 private synchronized void writeObject(java.io.ObjectOutputStream s)
715 throws java.io.IOException {
716 java.io.ObjectOutputStream.PutField fields = s.putFields();
717 fields.put("value", value);
718 fields.put("count", count);
719 fields.put("shared", false);
720 s.writeFields();
721 }
722
723 /**
724 * readObject is called to restore the state of the StringBuffer from
725 * a stream.
726 */
727 private void readObject(java.io.ObjectInputStream s)
728 throws java.io.IOException, ClassNotFoundException {
729 java.io.ObjectInputStream.GetField fields = s.readFields();
730 value = (char[])fields.get("value", null);
731 count = fields.get("count", 0);
732 isShared = false;
733 }
734 }
|