0N/A/*
2362N/A * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
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 *
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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/A
0N/A/**
0N/A * A thread-safe, mutable sequence of characters.
0N/A * A string buffer is like a {@link String}, but can be modified. At any
0N/A * point in time it contains some particular sequence of characters, but
0N/A * the length and content of the sequence can be changed through certain
0N/A * method calls.
0N/A * <p>
0N/A * String buffers are safe for use by multiple threads. The methods
0N/A * are synchronized where necessary so that all the operations on any
0N/A * particular instance behave as if they occur in some serial order
0N/A * that is consistent with the order of the method calls made by each of
0N/A * the individual threads involved.
0N/A * <p>
0N/A * The principal operations on a <code>StringBuffer</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 buffer. The
0N/A * <code>append</code> method always adds these characters at the end
0N/A * of the buffer; the <code>insert</code> method adds the characters at
0N/A * a specified point.
0N/A * <p>
0N/A * For example, if <code>z</code> refers to a string buffer 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 * buffer to contain "<code>startle</code>", whereas
0N/A * <code>z.insert(4, "le")</code> would alter the string buffer to
0N/A * contain "<code>starlet</code>".
0N/A * <p>
0N/A * In general, if sb refers to an instance of a <code>StringBuffer</code>,
0N/A * then <code>sb.append(x)</code> has the same effect as
0N/A * <code>sb.insert(sb.length(),&nbsp;x)</code>.
0N/A * <p>
0N/A * Whenever an operation occurs involving a source sequence (such as
0N/A * appending or inserting from a source sequence) this class synchronizes
0N/A * only on the string buffer performing the operation, not on the source.
0N/A * <p>
0N/A * Every string buffer has a capacity. As long as the length of the
0N/A * character sequence contained in the string buffer does not exceed
0N/A * the capacity, it is not necessary to allocate a new internal
0N/A * buffer array. If the internal buffer overflows, it is
0N/A * automatically made larger.
0N/A *
0N/A * As of release JDK 5, this class has been supplemented with an equivalent
0N/A * class designed for use by a single thread, {@link StringBuilder}. The
0N/A * <tt>StringBuilder</tt> class should generally be used in preference to
0N/A * this one, as it supports all of the same operations but it is faster, as
0N/A * it performs no synchronization.
0N/A *
0N/A * @author Arthur van Hoff
0N/A * @see java.lang.StringBuilder
0N/A * @see java.lang.String
0N/A * @since JDK1.0
0N/A */
0N/A public final class StringBuffer
0N/A extends AbstractStringBuilder
0N/A implements java.io.Serializable, CharSequence
0N/A{
0N/A
0N/A /** use serialVersionUID from JDK 1.0.2 for interoperability */
0N/A static final long serialVersionUID = 3388685877147921107L;
0N/A
0N/A /**
0N/A * Constructs a string buffer with no characters in it and an
0N/A * initial capacity of 16 characters.
0N/A */
0N/A public StringBuffer() {
0N/A super(16);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string buffer with no characters in it and
0N/A * the specified initial capacity.
0N/A *
0N/A * @param capacity the initial capacity.
0N/A * @exception NegativeArraySizeException if the <code>capacity</code>
0N/A * argument is less than <code>0</code>.
0N/A */
0N/A public StringBuffer(int capacity) {
0N/A super(capacity);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string buffer initialized to the contents of the
0N/A * specified string. The initial capacity of the string buffer is
0N/A * <code>16</code> plus the length of the string argument.
0N/A *
0N/A * @param str the initial contents of the buffer.
0N/A * @exception NullPointerException if <code>str</code> is <code>null</code>
0N/A */
0N/A public StringBuffer(String str) {
0N/A super(str.length() + 16);
0N/A append(str);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string buffer that contains the same characters
0N/A * as the specified <code>CharSequence</code>. The initial capacity of
0N/A * the string buffer is <code>16</code> plus the length of the
0N/A * <code>CharSequence</code> argument.
0N/A * <p>
0N/A * If the length of the specified <code>CharSequence</code> is
0N/A * less than or equal to zero, then an empty buffer of capacity
0N/A * <code>16</code> is returned.
0N/A *
0N/A * @param seq the sequence to copy.
0N/A * @exception NullPointerException if <code>seq</code> is <code>null</code>
0N/A * @since 1.5
0N/A */
0N/A public StringBuffer(CharSequence seq) {
0N/A this(seq.length() + 16);
0N/A append(seq);
0N/A }
0N/A
0N/A public synchronized int length() {
0N/A return count;
0N/A }
0N/A
0N/A public synchronized int capacity() {
0N/A return value.length;
0N/A }
0N/A
0N/A
0N/A public synchronized void ensureCapacity(int minimumCapacity) {
0N/A if (minimumCapacity > value.length) {
0N/A expandCapacity(minimumCapacity);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized void trimToSize() {
0N/A super.trimToSize();
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @see #length()
0N/A */
0N/A public synchronized void setLength(int newLength) {
0N/A super.setLength(newLength);
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @see #length()
0N/A */
0N/A public synchronized char charAt(int index) {
0N/A if ((index < 0) || (index >= count))
0N/A throw new StringIndexOutOfBoundsException(index);
0N/A return value[index];
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized int codePointAt(int index) {
0N/A return super.codePointAt(index);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized int codePointBefore(int index) {
0N/A return super.codePointBefore(index);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized int codePointCount(int beginIndex, int endIndex) {
0N/A return super.codePointCount(beginIndex, endIndex);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized int offsetByCodePoints(int index, int codePointOffset) {
0N/A return super.offsetByCodePoints(index, codePointOffset);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
570N/A public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
0N/A int dstBegin)
0N/A {
0N/A super.getChars(srcBegin, srcEnd, dst, dstBegin);
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @see #length()
0N/A */
0N/A public synchronized void setCharAt(int index, char ch) {
0N/A if ((index < 0) || (index >= count))
0N/A throw new StringIndexOutOfBoundsException(index);
0N/A value[index] = ch;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(Object obj) {
0N/A super.append(String.valueOf(obj));
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(String str) {
0N/A super.append(str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified <tt>StringBuffer</tt> to this sequence.
0N/A * <p>
0N/A * The characters of the <tt>StringBuffer</tt> argument are appended,
0N/A * in order, to the contents of this <tt>StringBuffer</tt>, increasing the
0N/A * length of this <tt>StringBuffer</tt> 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 <tt>StringBuffer</tt>.
0N/A * <p>
0N/A * Let <i>n</i> be the length of the old character sequence, the one
0N/A * contained in the <tt>StringBuffer</tt> just prior to execution of the
0N/A * <tt>append</tt> method. Then the character at index <i>k</i> in
0N/A * the new character sequence is equal to the character at index <i>k</i>
0N/A * in the old character sequence, if <i>k</i> is less than <i>n</i>;
0N/A * otherwise, it is equal to the character at index <i>k-n</i> in the
0N/A * argument <code>sb</code>.
0N/A * <p>
0N/A * This method synchronizes on <code>this</code> (the destination)
0N/A * object but does not synchronize on the source (<code>sb</code>).
0N/A *
0N/A * @param sb the <tt>StringBuffer</tt> to append.
0N/A * @return a reference to this object.
0N/A * @since 1.4
0N/A */
0N/A public synchronized StringBuffer append(StringBuffer sb) {
0N/A super.append(sb);
0N/A return this;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Appends the specified <code>CharSequence</code> to this
0N/A * sequence.
0N/A * <p>
0N/A * The characters of the <code>CharSequence</code> argument are appended,
0N/A * in order, increasing the length of this sequence by the length of the
0N/A * argument.
0N/A *
0N/A * <p>The result of this method is exactly the same as if it were an
0N/A * invocation of this.append(s, 0, s.length());
0N/A *
0N/A * <p>This method synchronizes on this (the destination)
0N/A * object but does not synchronize on the source (<code>s</code>).
0N/A *
0N/A * <p>If <code>s</code> is <code>null</code>, then the four characters
0N/A * <code>"null"</code> are appended.
0N/A *
0N/A * @param s the <code>CharSequence</code> to append.
0N/A * @return a reference to this object.
0N/A * @since 1.5
0N/A */
0N/A public StringBuffer append(CharSequence s) {
0N/A // Note, synchronization achieved via other invocations
0N/A if (s == null)
0N/A s = "null";
0N/A if (s instanceof String)
0N/A return this.append((String)s);
0N/A if (s instanceof StringBuffer)
0N/A return this.append((StringBuffer)s);
0N/A return this.append(s, 0, s.length());
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public synchronized StringBuffer append(CharSequence s, int start, int end)
0N/A {
0N/A super.append(s, start, end);
0N/A return this;
0N/A }
0N/A
570N/A public synchronized StringBuffer append(char[] str) {
0N/A super.append(str);
0N/A return this;
0N/A }
0N/A
570N/A /**
570N/A * @throws IndexOutOfBoundsException {@inheritDoc}
570N/A */
570N/A public synchronized StringBuffer append(char[] str, int offset, int len) {
0N/A super.append(str, offset, len);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(boolean b) {
0N/A super.append(b);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(char c) {
0N/A super.append(c);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(int i) {
0N/A super.append(i);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public synchronized StringBuffer appendCodePoint(int codePoint) {
0N/A super.appendCodePoint(codePoint);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(long lng) {
0N/A super.append(lng);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(float f) {
0N/A super.append(f);
0N/A return this;
0N/A }
0N/A
0N/A public synchronized StringBuffer append(double d) {
0N/A super.append(d);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public synchronized StringBuffer delete(int start, int end) {
0N/A super.delete(start, end);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public synchronized StringBuffer deleteCharAt(int index) {
0N/A super.deleteCharAt(index);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public synchronized StringBuffer replace(int start, int end, String str) {
0N/A super.replace(start, end, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public synchronized String substring(int start) {
0N/A return substring(start, count);
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.4
0N/A */
0N/A public synchronized CharSequence subSequence(int start, int end) {
0N/A return super.substring(start, end);
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public synchronized String substring(int start, int end) {
0N/A return super.substring(start, end);
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.2
0N/A */
570N/A public synchronized StringBuffer insert(int index, char[] str, int offset,
0N/A int len)
0N/A {
0N/A super.insert(index, str, offset, len);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public synchronized StringBuffer insert(int offset, Object obj) {
0N/A super.insert(offset, String.valueOf(obj));
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public synchronized StringBuffer insert(int offset, String str) {
0N/A super.insert(offset, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
570N/A public synchronized StringBuffer insert(int offset, char[] str) {
0N/A super.insert(offset, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public StringBuffer insert(int dstOffset, CharSequence s) {
0N/A // Note, synchronization achieved via other invocations
0N/A if (s == null)
0N/A s = "null";
0N/A if (s instanceof String)
0N/A return this.insert(dstOffset, (String)s);
0N/A return this.insert(dstOffset, s, 0, s.length());
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public synchronized StringBuffer insert(int dstOffset, CharSequence s,
0N/A int start, int end)
0N/A {
0N/A super.insert(dstOffset, s, start, end);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuffer insert(int offset, boolean b) {
0N/A return insert(offset, String.valueOf(b));
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public synchronized StringBuffer insert(int offset, char c) {
0N/A super.insert(offset, c);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuffer insert(int offset, int i) {
0N/A return insert(offset, String.valueOf(i));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuffer insert(int offset, long l) {
0N/A return insert(offset, String.valueOf(l));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuffer insert(int offset, float f) {
0N/A return insert(offset, String.valueOf(f));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuffer insert(int offset, double d) {
0N/A return insert(offset, String.valueOf(d));
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.4
0N/A */
0N/A public int indexOf(String str) {
0N/A return indexOf(str, 0);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.4
0N/A */
0N/A public synchronized int indexOf(String str, int fromIndex) {
0N/A return String.indexOf(value, 0, count,
0N/A str.toCharArray(), 0, str.length(), fromIndex);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.4
0N/A */
0N/A public int lastIndexOf(String str) {
0N/A // Note, synchronization achieved via other invocations
0N/A return lastIndexOf(str, count);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.4
0N/A */
0N/A public synchronized int lastIndexOf(String str, int fromIndex) {
0N/A return String.lastIndexOf(value, 0, count,
0N/A str.toCharArray(), 0, str.length(), fromIndex);
0N/A }
0N/A
0N/A /**
0N/A * @since JDK1.0.2
0N/A */
0N/A public synchronized StringBuffer reverse() {
0N/A super.reverse();
0N/A return this;
0N/A }
0N/A
0N/A public synchronized String toString() {
0N/A return new String(value, 0, count);
0N/A }
0N/A
0N/A /**
0N/A * Serializable fields for StringBuffer.
0N/A *
0N/A * @serialField value char[]
0N/A * The backing character array of this StringBuffer.
0N/A * @serialField count int
0N/A * The number of characters in this StringBuffer.
0N/A * @serialField shared boolean
0N/A * A flag indicating whether the backing array is shared.
0N/A * The value is ignored upon deserialization.
0N/A */
0N/A private static final java.io.ObjectStreamField[] serialPersistentFields =
0N/A {
0N/A new java.io.ObjectStreamField("value", char[].class),
0N/A new java.io.ObjectStreamField("count", Integer.TYPE),
0N/A new java.io.ObjectStreamField("shared", Boolean.TYPE),
0N/A };
0N/A
0N/A /**
0N/A * readObject is called to restore the state of the StringBuffer from
0N/A * a stream.
0N/A */
0N/A private synchronized void writeObject(java.io.ObjectOutputStream s)
0N/A throws java.io.IOException {
0N/A java.io.ObjectOutputStream.PutField fields = s.putFields();
0N/A fields.put("value", value);
0N/A fields.put("count", count);
0N/A fields.put("shared", false);
0N/A s.writeFields();
0N/A }
0N/A
0N/A /**
0N/A * readObject is called to restore the state of the StringBuffer from
0N/A * a stream.
0N/A */
0N/A private void readObject(java.io.ObjectInputStream s)
0N/A throws java.io.IOException, ClassNotFoundException {
0N/A java.io.ObjectInputStream.GetField fields = s.readFields();
0N/A value = (char[])fields.get("value", null);
0N/A count = fields.get("count", 0);
0N/A }
0N/A}