2 * Copyright (c) 2003, 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 mutable sequence of characters. This class provides an API compatible
31 * with <code>StringBuffer</code>, but with no guarantee of synchronization.
32 * This class is designed for use as a drop-in replacement for
33 * <code>StringBuffer</code> in places where the string buffer was being
34 * used by a single thread (as is generally the case). Where possible,
35 * it is recommended that this class be used in preference to
36 * <code>StringBuffer</code> as it will be faster under most implementations.
38 * <p>The principal operations on a <code>StringBuilder</code> are the
39 * <code>append</code> and <code>insert</code> methods, which are
40 * overloaded so as to accept data of any type. Each effectively
41 * converts a given datum to a string and then appends or inserts the
42 * characters of that string to the string builder. The
43 * <code>append</code> method always adds these characters at the end
44 * of the builder; the <code>insert</code> method adds the characters at
47 * For example, if <code>z</code> refers to a string builder object
48 * whose current contents are "<code>start</code>", then
49 * the method call <code>z.append("le")</code> would cause the string
50 * builder to contain "<code>startle</code>", whereas
51 * <code>z.insert(4, "le")</code> would alter the string builder to
52 * contain "<code>starlet</code>".
54 * In general, if sb refers to an instance of a <code>StringBuilder</code>,
55 * then <code>sb.append(x)</code> has the same effect as
56 * <code>sb.insert(sb.length(), x)</code>.
58 * Every string builder has a capacity. As long as the length of the
59 * character sequence contained in the string builder does not exceed
60 * the capacity, it is not necessary to allocate a new internal
61 * buffer. If the internal buffer overflows, it is automatically made larger.
63 * <p>Instances of <code>StringBuilder</code> are not safe for
64 * use by multiple threads. If such synchronization is required then it is
65 * recommended that {@link java.lang.StringBuffer} be used.
67 * @author Michael McCloskey
68 * @see java.lang.StringBuffer
69 * @see java.lang.String
72 public final class StringBuilder
73 extends AbstractStringBuilder
74 implements java.io.Serializable, CharSequence
77 /** use serialVersionUID for interoperability */
78 static final long serialVersionUID = 4383685877147921099L;
81 * Constructs a string builder with no characters in it and an
82 * initial capacity of 16 characters.
84 public StringBuilder() {
89 * Constructs a string builder with no characters in it and an
90 * initial capacity specified by the <code>capacity</code> argument.
92 * @param capacity the initial capacity.
93 * @throws NegativeArraySizeException if the <code>capacity</code>
94 * argument is less than <code>0</code>.
96 public StringBuilder(int capacity) {
101 * Constructs a string builder initialized to the contents of the
102 * specified string. The initial capacity of the string builder is
103 * <code>16</code> plus the length of the string argument.
105 * @param str the initial contents of the buffer.
106 * @throws NullPointerException if <code>str</code> is <code>null</code>
108 public StringBuilder(String str) {
109 super(str.length() + 16);
114 * Constructs a string builder that contains the same characters
115 * as the specified <code>CharSequence</code>. The initial capacity of
116 * the string builder is <code>16</code> plus the length of the
117 * <code>CharSequence</code> argument.
119 * @param seq the sequence to copy.
120 * @throws NullPointerException if <code>seq</code> is <code>null</code>
122 public StringBuilder(CharSequence seq) {
123 this(seq.length() + 16);
127 public StringBuilder append(Object obj) {
128 return append(String.valueOf(obj));
131 public StringBuilder append(String str) {
136 // Appends the specified string builder to this sequence.
137 private StringBuilder append(StringBuilder sb) {
139 return append("null");
140 int len = sb.length();
141 int newcount = count + len;
142 if (newcount > value.length)
143 expandCapacity(newcount);
144 sb.getChars(0, len, value, count);
150 * Appends the specified <tt>StringBuffer</tt> to this sequence.
152 * The characters of the <tt>StringBuffer</tt> argument are appended,
153 * in order, to this sequence, increasing the
154 * length of this sequence by the length of the argument.
155 * If <tt>sb</tt> is <tt>null</tt>, then the four characters
156 * <tt>"null"</tt> are appended to this sequence.
158 * Let <i>n</i> be the length of this character sequence just prior to
159 * execution of the <tt>append</tt> method. Then the character at index
160 * <i>k</i> in the new character sequence is equal to the character at
161 * index <i>k</i> in the old character sequence, if <i>k</i> is less than
162 * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
163 * in the argument <code>sb</code>.
165 * @param sb the <tt>StringBuffer</tt> to append.
166 * @return a reference to this object.
168 public StringBuilder append(StringBuffer sb) {
175 public StringBuilder append(CharSequence s) {
178 if (s instanceof String)
179 return this.append((String)s);
180 if (s instanceof StringBuffer)
181 return this.append((StringBuffer)s);
182 if (s instanceof StringBuilder)
183 return this.append((StringBuilder)s);
184 return this.append(s, 0, s.length());
188 * @throws IndexOutOfBoundsException {@inheritDoc}
190 public StringBuilder append(CharSequence s, int start, int end) {
191 super.append(s, start, end);
195 public StringBuilder append(char[] str) {
201 * @throws IndexOutOfBoundsException {@inheritDoc}
203 public StringBuilder append(char[] str, int offset, int len) {
204 super.append(str, offset, len);
208 public StringBuilder append(boolean b) {
213 public StringBuilder append(char c) {
218 public StringBuilder append(int i) {
223 public StringBuilder append(long lng) {
228 public StringBuilder append(float f) {
233 public StringBuilder append(double d) {
241 public StringBuilder appendCodePoint(int codePoint) {
242 super.appendCodePoint(codePoint);
247 * @throws StringIndexOutOfBoundsException {@inheritDoc}
249 public StringBuilder delete(int start, int end) {
250 super.delete(start, end);
255 * @throws StringIndexOutOfBoundsException {@inheritDoc}
257 public StringBuilder deleteCharAt(int index) {
258 super.deleteCharAt(index);
263 * @throws StringIndexOutOfBoundsException {@inheritDoc}
265 public StringBuilder replace(int start, int end, String str) {
266 super.replace(start, end, str);
271 * @throws StringIndexOutOfBoundsException {@inheritDoc}
273 public StringBuilder insert(int index, char[] str, int offset,
276 super.insert(index, str, offset, len);
281 * @throws StringIndexOutOfBoundsException {@inheritDoc}
283 public StringBuilder insert(int offset, Object obj) {
284 return insert(offset, String.valueOf(obj));
288 * @throws StringIndexOutOfBoundsException {@inheritDoc}
290 public StringBuilder insert(int offset, String str) {
291 super.insert(offset, str);
296 * @throws StringIndexOutOfBoundsException {@inheritDoc}
298 public StringBuilder insert(int offset, char[] str) {
299 super.insert(offset, str);
304 * @throws IndexOutOfBoundsException {@inheritDoc}
306 public StringBuilder insert(int dstOffset, CharSequence s) {
309 if (s instanceof String)
310 return this.insert(dstOffset, (String)s);
311 return this.insert(dstOffset, s, 0, s.length());
315 * @throws IndexOutOfBoundsException {@inheritDoc}
317 public StringBuilder insert(int dstOffset, CharSequence s,
320 super.insert(dstOffset, s, start, end);
325 * @throws StringIndexOutOfBoundsException {@inheritDoc}
327 public StringBuilder insert(int offset, boolean b) {
328 super.insert(offset, b);
333 * @throws IndexOutOfBoundsException {@inheritDoc}
335 public StringBuilder insert(int offset, char c) {
336 super.insert(offset, c);
341 * @throws StringIndexOutOfBoundsException {@inheritDoc}
343 public StringBuilder insert(int offset, int i) {
344 return insert(offset, String.valueOf(i));
348 * @throws StringIndexOutOfBoundsException {@inheritDoc}
350 public StringBuilder insert(int offset, long l) {
351 return insert(offset, String.valueOf(l));
355 * @throws StringIndexOutOfBoundsException {@inheritDoc}
357 public StringBuilder insert(int offset, float f) {
358 return insert(offset, String.valueOf(f));
362 * @throws StringIndexOutOfBoundsException {@inheritDoc}
364 public StringBuilder insert(int offset, double d) {
365 return insert(offset, String.valueOf(d));
369 * @throws NullPointerException {@inheritDoc}
371 public int indexOf(String str) {
372 return indexOf(str, 0);
376 * @throws NullPointerException {@inheritDoc}
378 public int indexOf(String str, int fromIndex) {
379 return String.indexOf(value, 0, count,
380 str.toCharArray(), 0, str.length(), fromIndex);
384 * @throws NullPointerException {@inheritDoc}
386 public int lastIndexOf(String str) {
387 return lastIndexOf(str, count);
391 * @throws NullPointerException {@inheritDoc}
393 public int lastIndexOf(String str, int fromIndex) {
394 return String.lastIndexOf(value, 0, count,
395 str.toCharArray(), 0, str.length(), fromIndex);
398 public StringBuilder reverse() {
403 public String toString() {
404 // Create a copy, don't share the array
405 return new String(value, 0, count);
409 * Save the state of the <tt>StringBuilder</tt> instance to a stream
410 * (that is, serialize it).
412 * @serialData the number of characters currently stored in the string
413 * builder (<tt>int</tt>), followed by the characters in the
414 * string builder (<tt>char[]</tt>). The length of the
415 * <tt>char</tt> array may be greater than the number of
416 * characters currently stored in the string builder, in which
417 * case extra characters are ignored.
419 // private void writeObject(java.io.ObjectOutputStream s)
420 // throws java.io.IOException {
421 // s.defaultWriteObject();
422 // s.writeInt(count);
423 // s.writeObject(value);
427 * readObject is called to restore the state of the StringBuffer from
430 // private void readObject(java.io.ObjectInputStream s)
431 // throws java.io.IOException, ClassNotFoundException {
432 // s.defaultReadObject();
433 // count = s.readInt();
434 // value = (char[]) s.readObject();