StringBuilder.java revision 0
0N/A * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * A mutable sequence of characters. This class provides an API compatible 0N/A * with <code>StringBuffer</code>, but with no guarantee of synchronization. 0N/A * This class is designed for use as a drop-in replacement for 0N/A * <code>StringBuffer</code> in places where the string buffer was being 0N/A * used by a single thread (as is generally the case). Where possible, 0N/A * it is recommended that this class be used in preference to 0N/A * <code>StringBuffer</code> as it will be faster under most implementations. 0N/A * <p>The principal operations on a <code>StringBuilder</code> are the 0N/A * <code>append</code> and <code>insert</code> methods, which are 0N/A * overloaded so as to accept data of any type. Each effectively 0N/A * converts a given datum to a string and then appends or inserts the 0N/A * characters of that string to the string builder. The 0N/A * <code>append</code> method always adds these characters at the end 0N/A * of the builder; the <code>insert</code> method adds the characters at 0N/A * a specified point. 0N/A * For example, if <code>z</code> refers to a string builder object 0N/A * whose current contents are "<code>start</code>", then 0N/A * the method call <code>z.append("le")</code> would cause the string 0N/A * builder to contain "<code>startle</code>", whereas 0N/A * <code>z.insert(4, "le")</code> would alter the string builder to 0N/A * contain "<code>starlet</code>". 0N/A * In general, if sb refers to an instance of a <code>StringBuilder</code>, 0N/A * then <code>sb.append(x)</code> has the same effect as 0N/A * <code>sb.insert(sb.length(), x)</code>. 0N/A * Every string builder has a capacity. As long as the length of the 0N/A * character sequence contained in the string builder does not exceed 0N/A * the capacity, it is not necessary to allocate a new internal 0N/A * buffer. If the internal buffer overflows, it is automatically made larger. 0N/A * <p>Instances of <code>StringBuilder</code> are not safe for 0N/A * use by multiple threads. If such synchronization is required then it is 0N/A * recommended that {@link java.lang.StringBuffer} be used. 0N/A * @author Michael McCloskey 0N/A * @see java.lang.StringBuffer 0N/A * @see java.lang.String 0N/A /** use serialVersionUID for interoperability */ 0N/A * Constructs a string builder with no characters in it and an 0N/A * initial capacity of 16 characters. 0N/A * Constructs a string builder with no characters in it and an 0N/A * initial capacity specified by the <code>capacity</code> argument. 0N/A * @param capacity the initial capacity. 0N/A * @throws NegativeArraySizeException if the <code>capacity</code> 0N/A * argument is less than <code>0</code>. 0N/A * Constructs a string builder initialized to the contents of the 0N/A * specified string. The initial capacity of the string builder is 0N/A * <code>16</code> plus the length of the string argument. 0N/A * @param str the initial contents of the buffer. 0N/A * @throws NullPointerException if <code>str</code> is <code>null</code> 0N/A * Constructs a string builder that contains the same characters 0N/A * as the specified <code>CharSequence</code>. The initial capacity of 0N/A * the string builder is <code>16</code> plus the length of the 0N/A * <code>CharSequence</code> argument. 0N/A * @param seq the sequence to copy. 0N/A * @throws NullPointerException if <code>seq</code> is <code>null</code> 0N/A * @see java.lang.String#valueOf(java.lang.Object) 0N/A * @see #append(java.lang.String) 0N/A // Appends the specified string builder to this sequence. 0N/A * Appends the specified <tt>StringBuffer</tt> to this sequence. 0N/A * The characters of the <tt>StringBuffer</tt> argument are appended, 0N/A * in order, to this sequence, increasing the 0N/A * length of this sequence by the length of the argument. 0N/A * If <tt>sb</tt> is <tt>null</tt>, then the four characters 0N/A * <tt>"null"</tt> are appended to this sequence. 0N/A * Let <i>n</i> be the length of this character sequence just prior to 0N/A * execution of the <tt>append</tt> method. Then the character at index 0N/A * <i>k</i> in the new character sequence is equal to the character at 0N/A * index <i>k</i> in the old character sequence, if <i>k</i> is less than 0N/A * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i> 0N/A * in the argument <code>sb</code>. 0N/A * @param sb the <tt>StringBuffer</tt> to append. 0N/A * @return a reference to this object. 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(boolean) 0N/A * @see #append(java.lang.String) 0N/A * @see java.lang.String#valueOf(int) 0N/A * @see #append(java.lang.String) 0N/A * @see java.lang.String#valueOf(long) 0N/A * @see #append(java.lang.String) 0N/A * @see java.lang.String#valueOf(float) 0N/A * @see #append(java.lang.String) 0N/A * @see java.lang.String#valueOf(double) 0N/A * @see #append(java.lang.String) 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(java.lang.Object) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(boolean) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(int) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(long) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(float) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc} 0N/A * @see java.lang.String#valueOf(double) 0N/A * @see #insert(int, java.lang.String) 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A // Create a copy, don't share the array 0N/A * Save the state of the <tt>StringBuilder</tt> instance to a stream 0N/A * (that is, serialize it). 0N/A * @serialData the number of characters currently stored in the string 0N/A * builder (<tt>int</tt>), followed by the characters in the 0N/A * string builder (<tt>char[]</tt>). The length of the 0N/A * <tt>char</tt> array may be greater than the number of 0N/A * characters currently stored in the string builder, in which 0N/A * case extra characters are ignored. 0N/A * readObject is called to restore the state of the StringBuffer from