In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
2 * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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
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.
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
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>".
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>.
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.
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.
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.
78 * @author Arthur van Hoff
79 * @see java.lang.StringBuilder
80 * @see java.lang.String
83 public final class StringBuffer
84 extends AbstractStringBuilder
85 implements java.io.Serializable, CharSequence
88 /** use serialVersionUID from JDK 1.0.2 for interoperability */
89 static final long serialVersionUID = 3388685877147921107L;
92 * Constructs a string buffer with no characters in it and an
93 * initial capacity of 16 characters.
95 public StringBuffer() {
100 * Constructs a string buffer with no characters in it and
101 * the specified initial capacity.
103 * @param capacity the initial capacity.
104 * @exception NegativeArraySizeException if the <code>capacity</code>
105 * argument is less than <code>0</code>.
107 public StringBuffer(int capacity) {
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.
116 * @param str the initial contents of the buffer.
117 * @exception NullPointerException if <code>str</code> is <code>null</code>
119 public StringBuffer(String str) {
120 super(str.length() + 16);
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.
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.
134 * @param seq the sequence to copy.
135 * @exception NullPointerException if <code>seq</code> is <code>null</code>
138 public StringBuffer(CharSequence seq) {
139 this(seq.length() + 16);
143 public synchronized int length() {
147 public synchronized int capacity() {
152 public synchronized void ensureCapacity(int minimumCapacity) {
153 if (minimumCapacity > value.length) {
154 expandCapacity(minimumCapacity);
161 public synchronized void trimToSize() {
166 * @throws IndexOutOfBoundsException {@inheritDoc}
169 public synchronized void setLength(int newLength) {
170 super.setLength(newLength);
174 * @throws IndexOutOfBoundsException {@inheritDoc}
177 public synchronized char charAt(int index) {
178 if ((index < 0) || (index >= count))
179 throw new StringIndexOutOfBoundsException(index);
186 public synchronized int codePointAt(int index) {
187 return super.codePointAt(index);
193 public synchronized int codePointBefore(int index) {
194 return super.codePointBefore(index);
200 public synchronized int codePointCount(int beginIndex, int endIndex) {
201 return super.codePointCount(beginIndex, endIndex);
207 public synchronized int offsetByCodePoints(int index, int codePointOffset) {
208 return super.offsetByCodePoints(index, codePointOffset);
212 * @throws NullPointerException {@inheritDoc}
213 * @throws IndexOutOfBoundsException {@inheritDoc}
215 public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
218 super.getChars(srcBegin, srcEnd, dst, dstBegin);
222 * @throws IndexOutOfBoundsException {@inheritDoc}
225 public synchronized void setCharAt(int index, char ch) {
226 if ((index < 0) || (index >= count))
227 throw new StringIndexOutOfBoundsException(index);
231 public synchronized StringBuffer append(Object obj) {
232 super.append(String.valueOf(obj));
236 public synchronized StringBuffer append(String str) {
242 * Appends the specified <tt>StringBuffer</tt> to this sequence.
244 * The characters of the <tt>StringBuffer</tt> argument are appended,
245 * in order, to the contents of this <tt>StringBuffer</tt>, increasing the
246 * length of this <tt>StringBuffer</tt> by the length of the argument.
247 * If <tt>sb</tt> is <tt>null</tt>, then the four characters
248 * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
250 * Let <i>n</i> be the length of the old character sequence, the one
251 * contained in the <tt>StringBuffer</tt> just prior to execution of the
252 * <tt>append</tt> method. Then the character at index <i>k</i> in
253 * the new character sequence is equal to the character at index <i>k</i>
254 * in the old character sequence, if <i>k</i> is less than <i>n</i>;
255 * otherwise, it is equal to the character at index <i>k-n</i> in the
256 * argument <code>sb</code>.
258 * This method synchronizes on <code>this</code> (the destination)
259 * object but does not synchronize on the source (<code>sb</code>).
261 * @param sb the <tt>StringBuffer</tt> to append.
262 * @return a reference to this object.
265 public synchronized StringBuffer append(StringBuffer sb) {
272 * Appends the specified <code>CharSequence</code> to this
275 * The characters of the <code>CharSequence</code> argument are appended,
276 * in order, increasing the length of this sequence by the length of the
279 * <p>The result of this method is exactly the same as if it were an
280 * invocation of this.append(s, 0, s.length());
282 * <p>This method synchronizes on this (the destination)
283 * object but does not synchronize on the source (<code>s</code>).
285 * <p>If <code>s</code> is <code>null</code>, then the four characters
286 * <code>"null"</code> are appended.
288 * @param s the <code>CharSequence</code> to append.
289 * @return a reference to this object.
292 public StringBuffer append(CharSequence s) {
293 // Note, synchronization achieved via other invocations
296 if (s instanceof String)
297 return this.append((String)s);
298 if (s instanceof StringBuffer)
299 return this.append((StringBuffer)s);
300 return this.append(s, 0, s.length());
304 * @throws IndexOutOfBoundsException {@inheritDoc}
307 public synchronized StringBuffer append(CharSequence s, int start, int end)
309 super.append(s, start, end);
313 public synchronized StringBuffer append(char[] str) {
319 * @throws IndexOutOfBoundsException {@inheritDoc}
321 public synchronized StringBuffer append(char[] str, int offset, int len) {
322 super.append(str, offset, len);
326 public synchronized StringBuffer append(boolean b) {
331 public synchronized StringBuffer append(char c) {
336 public synchronized StringBuffer append(int i) {
344 public synchronized StringBuffer appendCodePoint(int codePoint) {
345 super.appendCodePoint(codePoint);
349 public synchronized StringBuffer append(long lng) {
354 public synchronized StringBuffer append(float f) {
359 public synchronized StringBuffer append(double d) {
365 * @throws StringIndexOutOfBoundsException {@inheritDoc}
368 public synchronized StringBuffer delete(int start, int end) {
369 super.delete(start, end);
374 * @throws StringIndexOutOfBoundsException {@inheritDoc}
377 public synchronized StringBuffer deleteCharAt(int index) {
378 super.deleteCharAt(index);
383 * @throws StringIndexOutOfBoundsException {@inheritDoc}
386 public synchronized StringBuffer replace(int start, int end, String str) {
387 super.replace(start, end, str);
392 * @throws StringIndexOutOfBoundsException {@inheritDoc}
395 public synchronized String substring(int start) {
396 return substring(start, count);
400 * @throws IndexOutOfBoundsException {@inheritDoc}
403 public synchronized CharSequence subSequence(int start, int end) {
404 return super.substring(start, end);
408 * @throws StringIndexOutOfBoundsException {@inheritDoc}
411 public synchronized String substring(int start, int end) {
412 return super.substring(start, end);
416 * @throws StringIndexOutOfBoundsException {@inheritDoc}
419 public synchronized StringBuffer insert(int index, char[] str, int offset,
422 super.insert(index, str, offset, len);
427 * @throws StringIndexOutOfBoundsException {@inheritDoc}
429 public synchronized StringBuffer insert(int offset, Object obj) {
430 super.insert(offset, String.valueOf(obj));
435 * @throws StringIndexOutOfBoundsException {@inheritDoc}
437 public synchronized StringBuffer insert(int offset, String str) {
438 super.insert(offset, str);
443 * @throws StringIndexOutOfBoundsException {@inheritDoc}
445 public synchronized StringBuffer insert(int offset, char[] str) {
446 super.insert(offset, str);
451 * @throws IndexOutOfBoundsException {@inheritDoc}
454 public StringBuffer insert(int dstOffset, CharSequence s) {
455 // Note, synchronization achieved via other invocations
458 if (s instanceof String)
459 return this.insert(dstOffset, (String)s);
460 return this.insert(dstOffset, s, 0, s.length());
464 * @throws IndexOutOfBoundsException {@inheritDoc}
467 public synchronized StringBuffer insert(int dstOffset, CharSequence s,
470 super.insert(dstOffset, s, start, end);
475 * @throws StringIndexOutOfBoundsException {@inheritDoc}
477 public StringBuffer insert(int offset, boolean b) {
478 return insert(offset, String.valueOf(b));
482 * @throws IndexOutOfBoundsException {@inheritDoc}
484 public synchronized StringBuffer insert(int offset, char c) {
485 super.insert(offset, c);
490 * @throws StringIndexOutOfBoundsException {@inheritDoc}
492 public StringBuffer insert(int offset, int i) {
493 return insert(offset, String.valueOf(i));
497 * @throws StringIndexOutOfBoundsException {@inheritDoc}
499 public StringBuffer insert(int offset, long l) {
500 return insert(offset, String.valueOf(l));
504 * @throws StringIndexOutOfBoundsException {@inheritDoc}
506 public StringBuffer insert(int offset, float f) {
507 return insert(offset, String.valueOf(f));
511 * @throws StringIndexOutOfBoundsException {@inheritDoc}
513 public StringBuffer insert(int offset, double d) {
514 return insert(offset, String.valueOf(d));
518 * @throws NullPointerException {@inheritDoc}
521 public int indexOf(String str) {
522 return indexOf(str, 0);
526 * @throws NullPointerException {@inheritDoc}
529 public synchronized int indexOf(String str, int fromIndex) {
530 return super.indexOf(str, fromIndex);
534 * @throws NullPointerException {@inheritDoc}
537 public int lastIndexOf(String str) {
538 // Note, synchronization achieved via other invocations
539 return lastIndexOf(str, count);
543 * @throws NullPointerException {@inheritDoc}
546 public synchronized int lastIndexOf(String str, int fromIndex) {
547 return String.lastIndexOf(value, 0, count,
548 str.toCharArray(), 0, str.length(), fromIndex);
554 public synchronized StringBuffer reverse() {
559 public synchronized String toString() {
560 return new String(value, 0, count);
564 // * Serializable fields for StringBuffer.
566 // * @serialField value char[]
567 // * The backing character array of this StringBuffer.
568 // * @serialField count int
569 // * The number of characters in this StringBuffer.
570 // * @serialField shared boolean
571 // * A flag indicating whether the backing array is shared.
572 // * The value is ignored upon deserialization.
574 // private static final java.io.ObjectStreamField[] serialPersistentFields =
576 // new java.io.ObjectStreamField("value", char[].class),
577 // new java.io.ObjectStreamField("count", Integer.TYPE),
578 // new java.io.ObjectStreamField("shared", Boolean.TYPE),
582 // * readObject is called to restore the state of the StringBuffer from
585 // private synchronized void writeObject(java.io.ObjectOutputStream s)
586 // throws java.io.IOException {
587 // java.io.ObjectOutputStream.PutField fields = s.putFields();
588 // fields.put("value", value);
589 // fields.put("count", count);
590 // fields.put("shared", false);
595 // * readObject is called to restore the state of the StringBuffer from
598 // private void readObject(java.io.ObjectInputStream s)
599 // throws java.io.IOException, ClassNotFoundException {
600 // java.io.ObjectInputStream.GetField fields = s.readFields();
601 // value = (char[])fields.get("value", null);
602 // count = fields.get("count", 0);