You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/06/14 04:13:17 UTC
svn commit: r414026 [2/3] - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang:
AbstractStringBuilder.java StringBuffer.java StringBuilder.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java?rev=414026&r1=414025&r2=414026&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java Tue Jun 13 19:13:17 2006
@@ -1,26 +1,27 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/*
+ * Copyright 1998, 2005 The Apache Software Foundation or its licensors, as
+ * applicable
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
*/
package java.lang;
import java.io.IOException;
-import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
import java.io.Serializable;
-import java.util.Arrays;
/**
* StringBuffer is a variable size contiguous indexable array of characters. The
@@ -40,55 +41,45 @@
* @see StringBuilder
* @since 1.0
*/
-public final class StringBuffer implements Appendable, Serializable, CharSequence {
-
- private static final long serialVersionUID = 3388685877147921107L;
-
- private static final int INITIAL_SIZE = 16;
-
- private int count;
-
- private boolean shared;
-
- private char[] value;
-
- /**
- * Constructs a new StringBuffer using the default capacity.
- */
- public StringBuffer() {
- this(INITIAL_SIZE);
- }
-
- /**
- * Constructs a new StringBuffer using the specified capacity.
- *
- * @param capacity
- * the initial capacity
- */
- public StringBuffer(int capacity) {
- count = 0;
- shared = false;
- value = new char[capacity];
- }
-
- /**
- * Constructs a new StringBuffer containing the characters in the specified
- * string and the default capacity.
- *
- * @param string
- * the string content with which to initialize the new
- * <code>StringBuffer</code> instance
- * @exception NullPointerException
- * on supplying a <code>null</code> value of
- * <code>string</code>
- */
- public StringBuffer(String string) {
- count = string.length();
- shared = false;
- value = new char[count + INITIAL_SIZE];
- string.getChars(0, count, value, 0);
- }
-
+public final class StringBuffer extends AbstractStringBuilder implements
+ Appendable, Serializable, CharSequence {
+
+ private static final long serialVersionUID = 3388685877147921107L;
+
+ private static final ObjectStreamField serialPersistentFields[] = {
+ new ObjectStreamField("count", int.class),
+ new ObjectStreamField("shared", boolean.class),
+ new ObjectStreamField("value", char[].class), };
+
+ /**
+ * Constructs a new StringBuffer using the default capacity.
+ */
+ public StringBuffer() {
+ super();
+ }
+
+ /**
+ * Constructs a new StringBuffer using the specified capacity.
+ *
+ * @param capacity the initial capacity
+ */
+ public StringBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Constructs a new StringBuffer containing the characters in the specified
+ * string and the default capacity.
+ *
+ * @param string the string content with which to initialize the new
+ * <code>StringBuffer</code> instance
+ * @throws NullPointerException on supplying a <code>null</code> value of
+ * <code>string</code>
+ */
+ public StringBuffer(String string) {
+ super(string);
+ }
+
/**
* <p>
* Constructs a StringBuffer and initializes it with the characters in the
@@ -101,1028 +92,222 @@
* @since 1.5
*/
public StringBuffer(CharSequence cs) {
- super();
- count = cs.length();
- shared = false;
- value = new char[count + INITIAL_SIZE];
- for (int i = 0; i < count; i++) {
- value[i] = cs.charAt(i);
- }
+ super(cs.toString());
+ }
+
+ /**
+ * Adds the string representation of the specified boolean to the end of
+ * this StringBuffer.
+ *
+ * @param b the boolean
+ * @return this StringBuffer
+ */
+ public StringBuffer append(boolean b) {
+ return append(b ? "true" : "false");
+ }
+
+ /**
+ * Adds the specified character to the end of this StringBuffer.
+ *
+ * @param ch a character
+ * @return this StringBuffer
+ */
+ public synchronized StringBuffer append(char ch) {
+ append0(ch);
+ return this;
+ }
+
+ /**
+ * Adds the string representation of the specified double to the end of this
+ * StringBuffer.
+ *
+ * @param d the double
+ * @return this StringBuffer
+ */
+ public StringBuffer append(double d) {
+ return append(Double.toString(d));
+ }
+
+ /**
+ * Adds the string representation of the specified float to the end of this
+ * StringBuffer.
+ *
+ * @param f the float
+ * @return this StringBuffer
+ */
+ public StringBuffer append(float f) {
+ return append(Float.toString(f));
+ }
+
+ /**
+ * Adds the string representation of the specified integer to the end of
+ * this StringBuffer.
+ *
+ * @param value the integer
+ * @return this StringBuffer
+ */
+ public StringBuffer append(int i) {
+ return append(Integer.toString(i));
+ }
+
+ /**
+ * Adds the string representation of the specified long to the end of this
+ * StringBuffer.
+ *
+ * @param l the long
+ * @return this StringBuffer
+ */
+ public StringBuffer append(long l) {
+ return append(Long.toString(l));
+ }
+
+ /**
+ * Adds the string representation of the specified object to the end of this
+ * StringBuffer.
+ *
+ * @param obj the object
+ * @return this StringBuffer
+ */
+ public synchronized StringBuffer append(Object obj) {
+ if (obj == null)
+ appendNull();
+ else
+ append0(obj.toString());
+ return this;
}
- /**
- * Adds the character array to the end of this StringBuffer.
- *
- * @param chars
- * the character array
- * @return this StringBuffer
- *
- * @exception NullPointerException
- * when chars is null
- */
- public synchronized StringBuffer append(char chars[]) {
- int newSize = count + chars.length;
- if (newSize > value.length) {
- ensureCapacityImpl(newSize);
- } else if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- System.arraycopy(chars, 0, value, count, chars.length);
- count = newSize;
- return this;
- }
-
- /**
- * Adds the specified sequence of characters to the end of this
- * StringBuffer.
- *
- * @param chars
- * a character array
- * @param start
- * the starting offset
- * @param length
- * the number of characters
- * @return this StringBuffer
- *
- * @exception ArrayIndexOutOfBoundsException
- * when <code>length < 0, start < 0</code> or
- * <code>start + length > chars.length</code>
- * @exception NullPointerException
- * when chars is null
- */
- public synchronized StringBuffer append(char chars[], int start, int length) {
- if (chars == null) {
- throw new NullPointerException();
+ /**
+ * Adds the specified string to the end of this StringBuffer.
+ *
+ * @param string the string
+ * @return this StringBuffer
+ */
+ public synchronized StringBuffer append(String string) {
+ append0(string);
+ return this;
+ }
+
+ /**
+ * Adds the specified StringBuffer to the end of this StringBuffer.
+ *
+ * @param sbuffer the StringBuffer
+ * @return this StringBuffer
+ *
+ * @since 1.4
+ */
+ public synchronized StringBuffer append(StringBuffer sb) {
+ if (sb == null) {
+ appendNull();
+ } else {
+ synchronized (sb) {
+ append0(sb.getValue(), 0, sb.length());
+ }
}
- // start + length could overflow, start/length maybe MaxInt
- if (start >= 0 && 0 <= length && length <= chars.length - start) {
- int newSize = count + length;
- if (newSize > value.length) {
- ensureCapacityImpl(newSize);
- } else if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- System.arraycopy(chars, start, value, count, length);
- count = newSize;
- return this;
- }
- throw new ArrayIndexOutOfBoundsException();
- }
-
- /**
- * Adds the specified character to the end of this StringBuffer.
- *
- * @param ch
- * a character
- * @return this StringBuffer
- */
- public synchronized StringBuffer append(char ch) {
- if (count >= value.length) {
- ensureCapacityImpl(count + 1);
- }
- if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- value[count] = ch;
- count++;
- return this;
- }
-
- /**
- * Adds the string representation of the specified double to the end of this
- * StringBuffer.
- *
- * @param d
- * the double
- * @return this StringBuffer
- */
- public StringBuffer append(double d) {
- return append(String.valueOf(d));
- }
-
- /**
- * Adds the string representation of the specified float to the end of this
- * StringBuffer.
- *
- * @param f
- * the float
- * @return this StringBuffer
- */
- public StringBuffer append(float f) {
- return append(String.valueOf(f));
- }
-
- /**
- * Adds the string representation of the specified integer to the end of
- * this StringBuffer.
- *
- * @param value
- * the integer
- * @return this StringBuffer
- */
- public StringBuffer append(int i) {
- return append(Integer.toString(i));
- }
-
- /**
- * Adds the string representation of the specified long to the end of this
- * StringBuffer.
- *
- * @param l
- * the long
- * @return this StringBuffer
- */
- public StringBuffer append(long l) {
- return append(Long.toString(l));
- }
-
- /**
- * Adds the string representation of the specified object to the end of this
- * StringBuffer.
- *
- * @param obj
- * the object
- * @return this StringBuffer
- */
- public StringBuffer append(Object obj) {
- return append(String.valueOf(obj));
- }
-
- /**
- * Adds the specified string to the end of this StringBuffer.
- *
- * @param string
- * the string
- * @return this StringBuffer
- */
- public synchronized StringBuffer append(String string) {
- if (string == null)
- string = String.valueOf(string);
- int adding = string.length();
- int newSize = count + adding;
- if (newSize > value.length) {
- ensureCapacityImpl(newSize);
- } else if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- string.getChars(0, adding, value, count);
- count = newSize;
- return this;
- }
-
- /**
- * Adds the string representation of the specified boolean to the end of
- * this StringBuffer.
- *
- * @param b
- * the boolean
- * @return this StringBuffer
- */
- public StringBuffer append(boolean b) {
- return append(String.valueOf(b));
- }
-
- /**
- * Answers the number of characters this StringBuffer can hold without
- * growing.
- *
- * @return the capacity of this StringBuffer
- *
- * @see #ensureCapacity
- * @see #length
- */
- public int capacity() {
- return value.length;
- }
-
- /**
- * Answers the character at the specified offset in this StringBuffer.
- *
- * @param index
- * the zero-based index in this StringBuffer
- * @return the character at the index
- *
- * @exception IndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index >= length()</code>
- */
- public synchronized char charAt(int index) {
- try {
- if (index < count)
- return value[index];
- } catch (IndexOutOfBoundsException e) {
- }
- throw new StringIndexOutOfBoundsException(index);
- }
-
- /**
- * Deletes a range of characters.
- *
- * @param start
- * the offset of the first character
- * @param end
- * the offset one past the last character
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>start < 0, start > end</code> or
- * <code>end > length()</code>
- */
- public synchronized StringBuffer delete(int start, int end) {
- if (start >= 0) {
- if (end > count)
- end = count;
- if (end > start) {
- int length = count - end;
- if (length > 0) {
- try {
- if (!shared) {
- System.arraycopy(value, end, value, start, length);
- } else {
- char[] newData = new char[value.length];
- System.arraycopy(value, 0, newData, 0, start);
- System
- .arraycopy(value, end, newData, start,
- length);
- value = newData;
- shared = false;
- }
- } catch (IndexOutOfBoundsException e) {
- throw new StringIndexOutOfBoundsException();
- }
- }
- count -= end - start;
- return this;
- }
- if (start == end)
- return this;
- }
- throw new StringIndexOutOfBoundsException();
- }
-
- /**
- * Deletes a single character
- *
- * @param location
- * the offset of the character to delete
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>location < 0</code> or
- * <code>location >= length()</code>
- */
- public synchronized StringBuffer deleteCharAt(int location) {
- if (0 <= location && location < count) {
- int length = count - location - 1;
- if (length > 0) {
- try {
- if (!shared) {
- System.arraycopy(value, location + 1, value, location,
- length);
- } else {
- char[] newData = new char[value.length];
- System.arraycopy(value, 0, newData, 0, location);
- System.arraycopy(value, location + 1, newData,
- location, length);
- value = newData;
- shared = false;
- }
- } catch (IndexOutOfBoundsException e) {
- throw new StringIndexOutOfBoundsException(location);
- }
- }
- count--;
- return this;
- }
- throw new StringIndexOutOfBoundsException(location);
- }
-
- /**
- * Ensures that this StringBuffer can hold the specified number of
- * characters without growing.
- *
- * @param min
- * the minimum number of elements that this StringBuffer will
- * hold before growing
- */
- public synchronized void ensureCapacity(int min) {
- if (min > value.length)
- ensureCapacityImpl(min);
- }
-
- private void ensureCapacityImpl(int min) {
- int twice = (value.length << 1) + 2;
- char[] newData = new char[min > twice ? min : twice];
- System.arraycopy(value, 0, newData, 0, count);
- value = newData;
- shared = false;
- }
-
- /**
- * Copies the specified characters in this StringBuffer to the character
- * array starting at the specified offset in the character array.
- *
- * @param start
- * the starting offset of characters to copy
- * @param end
- * the ending offset of characters to copy
- * @param buffer
- * the destination character array
- * @param index
- * the starting offset in the character array
- *
- * @exception IndexOutOfBoundsException
- * when <code>start < 0, end > length(),
- * start > end, index < 0, end - start > buffer.length - index</code>
- * @exception NullPointerException
- * when buffer is null
- */
- public synchronized void getChars(int start, int end, char[] buffer,
- int index) {
- // NOTE last character not copied!
- try {
- if (start <= count && end <= count) {
- System.arraycopy(value, start, buffer, index, end - start);
- return;
- }
- } catch (IndexOutOfBoundsException e) {
- }
- throw new ArrayIndexOutOfBoundsException();
- }
-
- /**
- * Inserts the character array at the specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param chars
- * the character array to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- * @exception NullPointerException
- * when chars is null
- */
- public synchronized StringBuffer insert(int index, char[] chars) {
- if (0 <= index && index <= count) {
- move(chars.length, index);
- System.arraycopy(chars, 0, value, index, chars.length);
- count += chars.length;
- return this;
- }
- throw new StringIndexOutOfBoundsException(index);
- }
-
- /**
- * Inserts the specified sequence of characters at the specified offset in
- * this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param chars
- * a character array
- * @param start
- * the starting offset
- * @param length
- * the number of characters
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>length < 0, start < 0,</code>
- * <code>start + length > chars.length, index < 0</code>
- * or <code>index > length()</code>
- * @exception NullPointerException
- * when chars is null
- */
- public synchronized StringBuffer insert(int index, char chars[], int start,
- int length) {
- if (0 <= index && index <= count) {
- // start + length could overflow, start/length maybe MaxInt
- if (start >= 0 && 0 <= length && length <= chars.length - start) {
- move(length, index);
- System.arraycopy(chars, start, value, index, length);
- count += length;
- return this;
- }
- throw new StringIndexOutOfBoundsException();
- }
- throw new StringIndexOutOfBoundsException(index);
- }
-
- /**
- * Inserts the character at the specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param ch
- * the character to insert
- * @return this StringBuffer
- *
- * @exception ArrayIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public synchronized StringBuffer insert(int index, char ch) {
- if (0 <= index && index <= count) {
- move(1, index);
- value[index] = ch;
- count++;
- return this;
- }
- throw new ArrayIndexOutOfBoundsException(index);
- }
-
- /**
- * Inserts the string representation of the specified double at the
- * specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param d
- * the double to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, double d) {
- return insert(index, String.valueOf(d));
- }
-
- /**
- * Inserts the string representation of the specified float at the specified
- * offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param f
- * the float to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, float f) {
- return insert(index, String.valueOf(f));
- }
-
- /**
- * Inserts the string representation of the specified integer at the
- * specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param i
- * the integer to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, int i) {
- return insert(index, Integer.toString(i));
- }
-
- /**
- * Inserts the string representation of the specified long at the specified
- * offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param l
- * the long to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, long l) {
- return insert(index, Long.toString(l));
- }
-
- /**
- * Inserts the string representation of the specified object at the
- * specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param obj
- * the object to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, Object obj) {
- return insert(index, String.valueOf(obj));
- }
-
- /**
- * Inserts the string at the specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param string
- * the string to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public synchronized StringBuffer insert(int index, String string) {
- if (0 <= index && index <= count) {
- if (string == null)
- string = String.valueOf(string);
- int min = string.length();
- move(min, index);
- string.getChars(0, min, value, index);
- count += min;
- return this;
- }
- throw new StringIndexOutOfBoundsException(index);
- }
-
- /**
- * Inserts the string representation of the specified boolean at the
- * specified offset in this StringBuffer.
- *
- * @param index
- * the index at which to insert
- * @param b
- * the boolean to insert
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index > length()</code>
- */
- public StringBuffer insert(int index, boolean b) {
- return insert(index, String.valueOf(b));
- }
-
- /**
- * Answers the size of this StringBuffer.
- *
- * @return the number of characters in this StringBuffer
- */
- public int length() {
- return count;
- }
-
- private void move(int size, int index) {
- int newSize;
- if (value.length - count >= size) {
- if (!shared) {
- System.arraycopy(value, index, value, index + size, count
- - index); // index == count case is no-op
- return;
- }
- newSize = value.length;
- } else {
- int a = count + size, b = (value.length << 1) + 2;
- newSize = a > b ? a : b;
- }
-
- char[] newData = new char[newSize];
- System.arraycopy(value, 0, newData, 0, index);
- // index == count case is no-op
- System.arraycopy(value, index, newData, index + size, count - index);
- value = newData;
- shared = false;
- }
-
- /**
- * Replace a range of characters with the characters in the specified
- * String.
- *
- * @param start
- * the offset of the first character
- * @param end
- * the offset one past the last character
- * @param string
- * a String
- * @return this StringBuffer
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>start < 0</code> or <code>start > end</code>
- */
- public synchronized StringBuffer replace(int start, int end, String string) {
- if (start >= 0) {
- if (end > count)
- end = count;
- if (end > start) {
- int stringLength = string.length();
- int diff = end - start - stringLength;
- if (diff > 0) { // replacing with fewer characters
- if (!shared) {
- // index == count case is no-op
- System.arraycopy(value, end, value, start
- + stringLength, count - end);
- } else {
- char[] newData = new char[value.length];
- System.arraycopy(value, 0, newData, 0, start);
- // index == count case is no-op
- System.arraycopy(value, end, newData, start
- + stringLength, count - end);
- value = newData;
- shared = false;
- }
- } else if (diff < 0) {
- // replacing with more characters...need some room
- move(-diff, end);
- } else if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- string.getChars(0, stringLength, value, start);
- count -= diff;
- return this;
- }
- if (start == end) {
- if (string == null)
- throw new NullPointerException();
- return insert(start, string);
- }
- }
- throw new StringIndexOutOfBoundsException();
- }
-
- /**
- * Reverses the order of characters in this StringBuffer.
- *
- * @return this StringBuffer
- */
- public synchronized StringBuffer reverse() {
- if (count < 2) {
- return this;
- }
- if (!shared) {
- for (int i = 0, end = count, mid = count / 2; i < mid; i++) {
- char temp = value[--end];
- value[end] = value[i];
- value[i] = temp;
- }
- } else {
- char[] newData = new char[value.length];
- for (int i = 0, end = count; i < count; i++) {
- newData[--end] = value[i];
- }
- value = newData;
- shared = false;
- }
- return this;
- }
-
- /**
- * Sets the character at the specified offset in this StringBuffer.
- *
- * @param index
- * the zero-based index in this StringBuffer
- * @param ch
- * the character
- *
- * @exception IndexOutOfBoundsException
- * when <code>index < 0</code> or
- * <code>index >= length()</code>
- */
- public synchronized void setCharAt(int index, char ch) {
- if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- if (0 <= index && index < count)
- value[index] = ch;
- else
- throw new StringIndexOutOfBoundsException(index);
- }
-
- /**
- * Sets the length of this StringBuffer to the specified length. If there
- * are more than length characters in this StringBuffer, the characters at
- * end are lost. If there are less than length characters in the
- * StringBuffer, the additional characters are set to <code>\\u0000</code>.
- *
- * @param length
- * the new length of this StringBuffer
- *
- * @exception IndexOutOfBoundsException
- * when <code>length < 0</code>
- *
- * @see #length
- */
- public synchronized void setLength(int length) {
- if (length > value.length)
- ensureCapacityImpl(length);
- if (count > length) {
- if (!shared) {
- // NOTE: delete & replace do not void characters orphaned at the
- // end
- try {
- Arrays.fill(value, length, count, (char) 0);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new IndexOutOfBoundsException();
- }
- } else {
- char[] newData = new char[value.length];
- if (length > 0) {
- System.arraycopy(value, 0, newData, 0, length);
- }
- value = newData;
- shared = false;
- }
- }
- count = length;
- }
-
- /**
- * Copies a range of characters into a new String.
- *
- * @param start
- * the offset of the first character
- * @return a new String containing the characters from start to the end of
- * the string
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>start < 0</code> or
- * <code>start > length()</code>
- */
- public synchronized String substring(int start) {
- if (0 <= start && start <= count) {
- shared = true;
- return new String(start, count - start, value);
- }
- throw new StringIndexOutOfBoundsException(start);
- }
-
- /**
- * Copies a range of characters into a new String.
- *
- * @param start
- * the offset of the first character
- * @param end
- * the offset one past the last character
- * @return a new String containing the characters from start to end - 1
- *
- * @exception StringIndexOutOfBoundsException
- * when <code>start < 0, start > end</code> or
- * <code>end > length()</code>
- */
- public synchronized String substring(int start, int end) {
- if (0 <= start && start <= end && end <= count) {
- shared = true;
- return new String(value, start, end - start);
- }
- throw new StringIndexOutOfBoundsException();
- }
-
- /**
- * Answers the contents of this StringBuffer.
- *
- * @return a String containing the characters in this StringBuffer
- */
- public synchronized String toString() {
- if (count >= 256 && count <= (value.length >> 1))
- return new String(value, 0, count);
- shared = true;
- return new String(0, count, value);
- }
-
- /*
- * Return the underlying buffer and set the shared flag.
- *
- */
- char[] shareValue() {
- shared = true;
- return value;
- }
-
- private synchronized void writeObject(ObjectOutputStream stream)
- throws IOException {
- stream.defaultWriteObject();
- }
-
- private void readObject(ObjectInputStream stream) throws IOException,
- ClassNotFoundException {
- stream.defaultReadObject();
- if (count > value.length)
- throw new InvalidObjectException(org.apache.harmony.luni.util.Msg
- .getString("K0199"));
- shared = false;
- }
-
- /**
- * Adds the specified StringBuffer to the end of this StringBuffer.
- *
- * @param sbuffer
- * the StringBuffer
- * @return this StringBuffer
- *
- * @since 1.4
- */
- public synchronized StringBuffer append(StringBuffer sbuffer) {
- if (sbuffer == null)
- return append((String) null);
- synchronized (sbuffer) {
- int adding = sbuffer.count;
- int newSize = count + adding;
- if (newSize > value.length) {
- ensureCapacityImpl(newSize);
- } else if (shared) {
- value = (char[]) value.clone();
- shared = false;
- }
- System.arraycopy(sbuffer.value, 0, value, count, adding);
- count = newSize;
- }
- return this;
- }
-
- /**
- * Copies a range of characters into a new String.
- *
- * @param start
- * the offset of the first character
- * @param end
- * the offset one past the last character
- * @return a new String containing the characters from start to end - 1
- *
- * @exception IndexOutOfBoundsException
- * when <code>start < 0, start > end</code> or
- * <code>end > length()</code>
- *
- * @since 1.4
- */
- public CharSequence subSequence(int start, int end) {
- return substring(start, end);
- }
-
- /**
- * Searches in this StringBuffer for the first index of the specified
- * character. The search for the character starts at the beginning and moves
- * towards the end.
- *
- *
- * @param string
- * the string to find
- * @return the index in this StringBuffer of the specified character, -1 if
- * the character isn't found
- *
- * @see #lastIndexOf(String)
- *
- * @since 1.4
- */
- public int indexOf(String string) {
- return indexOf(string, 0);
- }
-
- /**
- * Searches in this StringBuffer for the index of the specified character.
- * The search for the character starts at the specified offset and moves
- * towards the end.
- *
- * @param subString
- * the string to find
- * @param start
- * the starting offset
- * @return the index in this StringBuffer of the specified character, -1 if
- * the character isn't found
- *
- * @see #lastIndexOf(String,int)
- *
- * @since 1.4
- */
- public synchronized int indexOf(String subString, int start) {
- if (start < 0)
- start = 0;
- int subCount = subString.length();
- if (subCount > 0) {
- if (subCount + start > count)
- return -1;
- char firstChar = subString.charAt(0);
- while (true) {
- int i = start;
- boolean found = false;
- for (; i < count; i++)
- if (value[i] == firstChar) {
- found = true;
- break;
- }
- if (!found || subCount + i > count)
- return -1; // handles subCount > count || start >= count
- int o1 = i, o2 = 0;
- while (++o2 < subCount && value[++o1] == subString.charAt(o2)) {
- // Intentionally empty
- }
- if (o2 == subCount)
- return i;
- start = i + 1;
- }
- }
- return (start < count || start == 0) ? start : count;
- }
-
- /**
- * Searches in this StringBuffer for the last index of the specified
- * character. The search for the character starts at the end and moves
- * towards the beginning.
- *
- * @param string
- * the string to find
- * @return the index in this StringBuffer of the specified character, -1 if
- * the character isn't found
- *
- * @see #indexOf(String)
- *
- * @since 1.4
- */
- public synchronized int lastIndexOf(String string) {
- return lastIndexOf(string, count);
- }
-
- /**
- * Searches in this StringBuffer for the index of the specified character.
- * The search for the character starts at the specified offset and moves
- * towards the beginning.
- *
- * @param subString
- * the string to find
- * @param start
- * the starting offset
- * @return the index in this StringBuffer of the specified character, -1 if
- * the character isn't found
- *
- * @see #indexOf(String,int)
- *
- * @since 1.4
- */
- public synchronized int lastIndexOf(String subString, int start) {
- int subCount = subString.length();
- if (subCount <= count && start >= 0) {
- if (subCount > 0) {
- if (start > count - subCount)
- start = count - subCount; // count and subCount are both
- // >= 1
- char firstChar = subString.charAt(0);
- while (true) {
- int i = start;
- boolean found = false;
- for (; i >= 0; --i)
- if (value[i] == firstChar) {
- found = true;
- break;
- }
- if (!found)
- return -1;
- int o1 = i, o2 = 0;
- while (++o2 < subCount
- && value[++o1] == subString.charAt(o2)) {
- // Intentionally empty
- }
- if (o2 == subCount)
- return i;
- start = i - 1;
- }
- }
- return start < count ? start : count;
- }
- return -1;
- }
-
- /*
- * Returns the character array for this StringBuffer.
- */
- char[] getValue() {
- return value;
- }
-
+ return this;
+ }
+
+ /**
+ * Adds the character array to the end of this StringBuffer.
+ *
+ * @param chars the character array
+ * @return this StringBuffer
+ *
+ * @throws NullPointerException when chars is null
+ */
+ public synchronized StringBuffer append(char chars[]) {
+ append0(chars);
+ return this;
+ }
+
+ /**
+ * Adds the specified sequence of characters to the end of this
+ * StringBuffer.
+ *
+ * @param chars a character array
+ * @param start the starting offset
+ * @param length the number of characters
+ * @return this StringBuffer
+ *
+ * @throws ArrayIndexOutOfBoundsException when
+ * <code>length < 0, start < 0</code> or
+ * <code>start + length > chars.length</code>
+ * @throws NullPointerException when chars is null
+ */
+ public synchronized StringBuffer append(char chars[], int start, int length) {
+ append0(chars, start, length);
+ return this;
+ }
+
/**
* <p>
- * Trims the storage capacity of this buffer down to the size of the current
- * character sequence. Execution of this method may change the results
- * returned by the {@link #capacity()} method, but this is not required.
+ * Appends the <code>CharSequence</code> to this buffer. If the
+ * <code>CharSequence</code> is <code>null</code>, then the string
+ * <code>"null"</code> is appended.
* </p>
*
+ * @param s The <code>CharSequence</code> to append.
+ * @return A reference to this object.
* @since 1.5
*/
- public synchronized void trimToSize() {
- if (count < value.length) {
- char[] newValue = new char[count];
- System.arraycopy(value, 0, newValue, 0, count);
- value = newValue;
- shared = false;
- }
+ public synchronized StringBuffer append(CharSequence s) {
+ if (s == null)
+ appendNull();
+ else
+ append0(s.toString());
+ return this;
+ }
+
+ /**
+ * <p>
+ * Appends the subsequence of the <code>CharSequence</code> to this
+ * buffer. If the <code>CharSequence</code> is <code>null</code>, then
+ * the string <code>"null"</code> is used to extract a subsequence.
+ * </p>
+ *
+ * @param s The <code>CharSequence</code> to append.
+ * @param start The inclusive start index of the subsequence of the
+ * <code>CharSequence</code>.
+ * @param end The exclusive end index of the subsequence of the
+ * <code>CharSequence</code>.
+ * @return A reference to this object.
+ * @since 1.5
+ * @throws IndexOutOfBoundsException if <code>start</code> or
+ * <code>end</code> are negative, <code>start</code> is greater
+ * than <code>end</code> or <code>end</code> is greater than the
+ * length of <code>s</code>.
+ */
+ public synchronized StringBuffer append(CharSequence s, int start, int end) {
+ append0(s, start, end);
+ return this;
+ }
+
+ /**
+ * <p>
+ * Appends the encoded Unicode code point to this object. The code point is
+ * converted to a <code>char[]</code> as defined by
+ * {@link Character#toChars(int)}.
+ * </p>
+ *
+ * @param codePoint The Unicode code point to encode and append.
+ * @return A reference to this object.
+ * @see Character#toChars(int)
+ * @since 1.5
+ */
+ public StringBuffer appendCodePoint(int codePoint) {
+ return append(Character.toChars(codePoint));
+ }
+
+ /**
+ * Answers the character at the specified offset in this StringBuffer.
+ *
+ * @param index the zero-based index in this StringBuffer
+ * @return the character at the index
+ *
+ * @throws IndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index >= length()</code>
+ */
+ public synchronized char charAt(int index) {
+ return super.charAt(index);
}
/**
@@ -1140,9 +325,7 @@
* @since 1.5
*/
public synchronized int codePointAt(int index) {
- if (index < 0 || index >= count)
- throw new IndexOutOfBoundsException();
- return Character.codePointAt(value, index, count);
+ return super.codePointAt(index);
}
/**
@@ -1161,9 +344,7 @@
* @since 1.5
*/
public synchronized int codePointBefore(int index) {
- if (index < 1 || index > count)
- throw new IndexOutOfBoundsException();
- return Character.codePointBefore(value, index);
+ return super.codePointBefore(index);
}
/**
@@ -1181,98 +362,258 @@
* @since 1.5
*/
public synchronized int codePointCount(int beginIndex, int endIndex) {
- if (beginIndex < 0 || endIndex > count || beginIndex > endIndex)
- throw new IndexOutOfBoundsException();
- return Character.codePointCount(value, beginIndex, endIndex
- - beginIndex);
+ return super.codePointCount(beginIndex, endIndex);
}
/**
- * <p>
- * Returns the index within this object that is offset from
- * <code>index</code> by <code>codePointOffset</code> code points.
- * </p>
+ * Deletes a range of characters.
*
- * @param index The index within this object to calculate the offset from.
- * @param codePointOffset The number of code points to count.
- * @return The index within this object that is the offset.
- * @throws IndexOutOfBoundsException if <code>index</code> is negative or
- * greater than {@link #length()} or if there aren't enough code
- * points before or after <code>index</code> to match
- * <code>codePointOffset</code>.
- * @since 1.5
+ * @param start the offset of the first character
+ * @param end the offset one past the last character
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when
+ * <code>start < 0, start > end</code> or
+ * <code>end > length()</code>
*/
- public synchronized int offsetByCodePoints(int index, int codePointOffset) {
- return Character.offsetByCodePoints(value, 0, count, index,
- codePointOffset);
+ public synchronized StringBuffer delete(int start, int end) {
+ delete0(start, end);
+ return this;
}
/**
- * <p>Appends the <code>CharSequence</code> to this buffer. If the
- * <code>CharSequence</code> is <code>null</code>, then the string
- * <code>"null"</code> is appended.</p>
- * @param s The <code>CharSequence</code> to append.
- * @return A reference to this object.
- * @since 1.5
+ * Deletes a single character
+ *
+ * @param location the offset of the character to delete
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>location < 0</code>
+ * or <code>location >= length()</code>
*/
- public StringBuffer append(CharSequence s) {
- if (s == null)
- s = "null";
- append(s.toString());
+ public synchronized StringBuffer deleteCharAt(int location) {
+ deleteCharAt0(location);
return this;
}
/**
- * <p>Appends the subsequence of the <code>CharSequence</code> to this buffer. If the
- * <code>CharSequence</code> is <code>null</code>, then the string
- * <code>"null"</code> is used to extract a subsequence.</p>
- * @param s The <code>CharSequence</code> to append.
- * @param start The inclusive start index of the subsequence of the <code>CharSequence</code>.
- * @param end The exclusive end index of the subsequence of the <code>CharSequence</code>.
- * @return A reference to this object.
- * @since 1.5
- * @throws IndexOutOfBoundsException if <code>start</code> or <code>end</code> are negative, <code>start</code> is greater than <code>end</code> or <code>end</code> is greater than the length of <code>s</code>.
+ * Ensures that this StringBuffer can hold the specified number of
+ * characters without growing.
+ *
+ * @param min the minimum number of elements that this StringBuffer will
+ * hold before growing
*/
- public StringBuffer append(CharSequence s, int start, int end) {
- if (s == null)
- s = "null";
- if (start < 0 || end < 0 || start > end || end > s.length())
- throw new IndexOutOfBoundsException();
+ public synchronized void ensureCapacity(int min) {
+ super.ensureCapacity(min);
+ }
+
+ /**
+ * Copies the specified characters in this StringBuffer to the character
+ * array starting at the specified offset in the character array.
+ *
+ * @param start the starting offset of characters to copy
+ * @param end the ending offset of characters to copy
+ * @param buffer the destination character array
+ * @param index the starting offset in the character array
+ *
+ * @throws IndexOutOfBoundsException when <code>start < 0, end > length(),
+ * start > end, index < 0, end - start > buffer.length - index</code>
+ * @throws NullPointerException when buffer is null
+ */
+ public synchronized void getChars(int start, int end, char[] buffer, int idx) {
+ super.getChars(start, end, buffer, idx);
+ }
- append(s.subSequence(start, end));
+ /**
+ * Searches in this StringBuffer for the index of the specified character.
+ * The search for the character starts at the specified offset and moves
+ * towards the end.
+ *
+ * @param subString the string to find
+ * @param start the starting offset
+ * @return the index in this StringBuffer of the specified character, -1 if
+ * the character isn't found
+ *
+ * @see #lastIndexOf(String,int)
+ *
+ * @since 1.4
+ */
+ public synchronized int indexOf(String subString, int start) {
+ return super.indexOf(subString, start);
+ }
+
+ /**
+ * Inserts the character at the specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param ch the character to insert
+ * @return this StringBuffer
+ *
+ * @throws ArrayIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public synchronized StringBuffer insert(int index, char ch) {
+ insert0(index, ch);
return this;
}
/**
- * <p>
- * Appends the encoded Unicode code point to this object. The code point is
- * converted to a <code>char[]</code> as defined by
- * {@link Character#toChars(int)}.
- * </p>
+ * Inserts the string representation of the specified boolean at the
+ * specified offset in this StringBuffer.
*
- * @param codePoint The Unicode code point to encode and append.
- * @return A reference to this object.
- * @see Character#toChars(int)
- * @since 1.5
+ * @param index the index at which to insert
+ * @param b the boolean to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
*/
- public StringBuffer appendCodePoint(int codePoint) {
- return append(Character.toChars(codePoint));
+ public StringBuffer insert(int index, boolean b) {
+ return insert(index, b ? "true" : "false");
}
/**
- * <p>Inserts the <code>CharSequence</code> into this buffer at the <code>index</code>. If
- * <code>CharSequence</code> is <code>null</code>, then the string <code>"null"</code> is
- * inserted.</p>
+ * Inserts the string representation of the specified integer at the
+ * specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param i the integer to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public StringBuffer insert(int index, int i) {
+ return insert(index, Integer.toString(i));
+ }
+
+ /**
+ * Inserts the string representation of the specified long at the specified
+ * offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param l the long to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public StringBuffer insert(int index, long l) {
+ return insert(index, Long.toString(l));
+ }
+
+ /**
+ * Inserts the string representation of the specified double at the
+ * specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param d the double to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public StringBuffer insert(int index, double d) {
+ return insert(index, Double.toString(d));
+ }
+
+ /**
+ * Inserts the string representation of the specified float at the specified
+ * offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param f the float to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public StringBuffer insert(int index, float f) {
+ return insert(index, Float.toString(f));
+ }
+
+ /**
+ * Inserts the string representation of the specified object at the
+ * specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param obj the object to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public StringBuffer insert(int index, Object obj) {
+ return insert(index, obj == null ? "null" : obj.toString());
+ }
+
+ /**
+ * Inserts the string at the specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param string the string to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ */
+ public synchronized StringBuffer insert(int index, String string) {
+ insert0(index, string);
+ return this;
+ }
+
+ /**
+ * Inserts the character array at the specified offset in this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param chars the character array to insert
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index > length()</code>
+ * @throws NullPointerException when chars is null
+ */
+ public synchronized StringBuffer insert(int index, char[] chars) {
+ insert0(index, chars);
+ return this;
+ }
+
+ /**
+ * Inserts the specified sequence of characters at the specified offset in
+ * this StringBuffer.
+ *
+ * @param index the index at which to insert
+ * @param chars a character array
+ * @param start the starting offset
+ * @param length the number of characters
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when
+ * <code>length < 0, start < 0,</code>
+ * <code>start + length > chars.length, index < 0</code>
+ * or <code>index > length()</code>
+ * @throws NullPointerException when chars is null
+ */
+ public synchronized StringBuffer insert(int index, char chars[], int start,
+ int length) {
+ insert0(index, chars, start, length);
+ return this;
+ }
+
+ /**
+ * <p>
+ * Inserts the <code>CharSequence</code> into this buffer at the
+ * <code>index</code>. If <code>CharSequence</code> is
+ * <code>null</code>, then the string <code>"null"</code> is inserted.
+ * </p>
+ *
* @param index The index of this buffer to insert the sequence.
* @param s The <code>CharSequence</code> to insert.
* @return A reference to this object.
* @since 1.5
* @throws IndexOutOfBoundsException if the index is invalid.
*/
- public StringBuffer insert(int index, CharSequence s) {
- if (s == null)
- s = "null";
- insert(index, s.toString());
+ public synchronized StringBuffer insert(int index, CharSequence s) {
+ insert0(index, s == null ? "null" : s.toString());
return this;
}
@@ -1297,13 +638,187 @@
* than <code>end</code> or <code>end</code> is greater than the
* length of <code>s</code>.
*/
- public StringBuffer insert(int index, CharSequence s, int start, int end) {
- if (s == null)
- s = "null";
- if (index < 0 || index > count || start < 0 || end < 0 || start > end
- || end > s.length())
- throw new IndexOutOfBoundsException();
- insert(index, s.subSequence(start, end));
+ public synchronized StringBuffer insert(int index, CharSequence s,
+ int start, int end) {
+ insert0(index, s, start, end);
+ return this;
+ }
+
+ /**
+ * Searches in this StringBuffer for the index of the specified character.
+ * The search for the character starts at the specified offset and moves
+ * towards the beginning.
+ *
+ * @param subString the string to find
+ * @param start the starting offset
+ * @return the index in this StringBuffer of the specified character, -1 if
+ * the character isn't found
+ *
+ * @see #indexOf(String,int)
+ *
+ * @since 1.4
+ */
+ public synchronized int lastIndexOf(String subString, int start) {
+ return super.lastIndexOf(subString, start);
+ }
+
+ /**
+ * <p>
+ * Returns the index within this object that is offset from
+ * <code>index</code> by <code>codePointOffset</code> code points.
+ * </p>
+ *
+ * @param index The index within this object to calculate the offset from.
+ * @param codePointOffset The number of code points to count.
+ * @return The index within this object that is the offset.
+ * @throws IndexOutOfBoundsException if <code>index</code> is negative or
+ * greater than {@link #length()} or if there aren't enough code
+ * points before or after <code>index</code> to match
+ * <code>codePointOffset</code>.
+ * @since 1.5
+ */
+ public synchronized int offsetByCodePoints(int index, int codePointOffset) {
+ return super.offsetByCodePoints(index, codePointOffset);
+ }
+
+ /**
+ * Replace a range of characters with the characters in the specified
+ * String.
+ *
+ * @param start the offset of the first character
+ * @param end the offset one past the last character
+ * @param string a String
+ * @return this StringBuffer
+ *
+ * @throws StringIndexOutOfBoundsException when <code>start < 0</code> or
+ * <code>start > end</code>
+ */
+ public synchronized StringBuffer replace(int start, int end, String string) {
+ replace0(start, end, string);
return this;
+ }
+
+ /**
+ * Reverses the order of characters in this StringBuffer.
+ *
+ * @return this StringBuffer
+ */
+ public synchronized StringBuffer reverse() {
+ reverse0();
+ return this;
+ }
+
+ /**
+ * Sets the character at the specified offset in this StringBuffer.
+ *
+ * @param index the zero-based index in this StringBuffer
+ * @param ch the character
+ *
+ * @throws IndexOutOfBoundsException when <code>index < 0</code> or
+ * <code>index >= length()</code>
+ */
+ public synchronized void setCharAt(int index, char ch) {
+ super.setCharAt(index, ch);
+ }
+
+ /**
+ * Sets the length of this StringBuffer to the specified length. If there
+ * are more than length characters in this StringBuffer, the characters at
+ * end are lost. If there are less than length characters in the
+ * StringBuffer, the additional characters are set to <code>\\u0000</code>.
+ *
+ * @param length the new length of this StringBuffer
+ *
+ * @throws IndexOutOfBoundsException when <code>length < 0</code>
+ *
+ * @see #length
+ */
+ public synchronized void setLength(int length) {
+ super.setLength(length);
+ }
+
+ /**
+ * Copies a range of characters into a new String.
+ *
+ * @param start the offset of the first character
+ * @param end the offset one past the last character
+ * @return a new String containing the characters from start to end - 1
+ *
+ * @throws IndexOutOfBoundsException when
+ * <code>start < 0, start > end</code> or
+ * <code>end > length()</code>
+ *
+ * @since 1.4
+ */
+ public synchronized CharSequence subSequence(int start, int end) {
+ return super.substring(start, end);
+ }
+
+ /**
+ * Copies a range of characters into a new String.
+ *
+ * @param start the offset of the first character
+ * @return a new String containing the characters from start to the end of
+ * the string
+ *
+ * @throws StringIndexOutOfBoundsException when <code>start < 0</code> or
+ * <code>start > length()</code>
+ */
+ public synchronized String substring(int start) {
+ return super.substring(start);
+ }
+
+ /**
+ * Copies a range of characters into a new String.
+ *
+ * @param start the offset of the first character
+ * @param end the offset one past the last character
+ * @return a new String containing the characters from start to end - 1
+ *
+ * @throws StringIndexOutOfBoundsException when
+ * <code>start < 0, start > end</code> or
+ * <code>end > length()</code>
+ */
+ public synchronized String substring(int start, int end) {
+ return super.substring(start, end);
+ }
+
+ /**
+ * Answers the contents of this StringBuffer.
+ *
+ * @return a String containing the characters in this StringBuffer
+ */
+ public synchronized String toString() {
+ return super.toString();
+ }
+
+ /**
+ * <p>
+ * Trims the storage capacity of this buffer down to the size of the current
+ * character sequence. Execution of this method may change the results
+ * returned by the {@link #capacity()} method, but this is not required.
+ * </p>
+ *
+ * @since 1.5
+ */
+ public synchronized void trimToSize() {
+ super.trimToSize();
+ }
+
+ private synchronized void writeObject(ObjectOutputStream out)
+ throws IOException {
+ ObjectOutputStream.PutField fields = out.putFields();
+ fields.put("count", length());
+ fields.put("shared", false);
+ fields.put("value", getValue());
+ out.writeFields();
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream.GetField fields = in.readFields();
+ int count = fields.get("count", 0);
+ char[] value = (char[]) fields.get("value", null);
+ set(value, count);
}
}