FileDocCategorySizeDatePackage
StringBuffer.javaAPI DocAndroid 1.5 API24278Wed May 06 22:41:04 BST 2009java.lang

StringBuffer

public final class StringBuffer extends AbstractStringBuilder implements Serializable, Appendable, CharSequence
StringBuffer is a variable size contiguous indexable array of characters. The length of the StringBuffer is the number of characters it contains. The capacity of the StringBuffer is the number of characters it can hold.

Characters may be inserted at any position up to the length of the StringBuffer, increasing the length of the StringBuffer. Characters at any position in the StringBuffer may be replaced, which does not affect the StringBuffer length.

The capacity of a StringBuffer may be specified when the StringBuffer is created. If the capacity of the StringBuffer is exceeded, the capacity is increased.

see
String
see
StringBuilder
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final ObjectStreamField[]
serialPersistentFields
Constructors Summary
public StringBuffer()
Constructs a new StringBuffer using the default capacity which is 16.

since
Android 1.0

 //$NON-NLS-1$

                        
      
        super();
    
public StringBuffer(int capacity)
Constructs a new StringBuffer using the specified capacity.

param
capacity the initial capacity.
since
Android 1.0

        super(capacity);
    
public StringBuffer(String string)
Constructs a new StringBuffer containing the characters in the specified stringy. The capacity of the new buffer will be the length of the {@code String} plus the default capacity.

param
string the string content with which to initialize the new instance.
since
Android 1.0

        super(string);
    
public StringBuffer(CharSequence cs)
Constructs a StringBuffer and initializes it with the content from the specified {@code CharSequence}. The capacity of the new buffer will be the length of the {@code CharSequence} plus the default capacity.

param
cs the content to initialize the instance.
since
Android 1.0

        super(cs.toString());
    
Methods Summary
public java.lang.StringBufferappend(long l)
Adds the string representation of the specified long to the end of this StringBuffer.

param
l the long to append.
return
this StringBuffer.
see
String#valueOf(long)
since
Android 1.0

        return append(Long.toString(l));
    
public synchronized java.lang.StringBufferappend(java.lang.Object obj)
Adds the string representation of the specified object to the end of this StringBuffer.

If the specified object is {@code null} the string {@code "null"} is appended, otherwise the objects {@code toString} is used to get its string representation.

param
obj the object to append (may be null).
return
this StringBuffer.
see
String#valueOf(Object)
since
Android 1.0

        if (obj == null) {
            appendNull();
        } else {
            append0(obj.toString());
        }
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.String string)
Adds the specified string to the end of this buffer.

If the specified string is {@code null} the string {@code "null"} is appended, otherwise the contents of the specified string is appended.

param
string the string to append (may be null).
return
this StringBuffer.
since
Android 1.0

        append0(string);
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.StringBuffer sb)
Adds the specified StringBuffer to the end of this buffer.

If the specified StringBuffer is {@code null} the string {@code "null"} is appended, otherwise the contents of the specified StringBuffer is appended.

param
sb the StringBuffer to append (may be null).
return
this StringBuffer.
since
Android 1.0

        if (sb == null) {
            appendNull();
        } else {
            synchronized (sb) {
                append0(sb.getValue(), 0, sb.length());
            }
        }
        return this;
    
public synchronized java.lang.StringBufferappend(char[] chars)
Adds the character array to the end of this buffer.

param
chars the character array to append.
return
this StringBuffer.
since
Android 1.0

        append0(chars);
        return this;
    
public synchronized java.lang.StringBufferappend(char[] chars, int start, int length)
Adds the specified sequence of characters to the end of this buffer.

param
chars the character array to append.
param
start the starting offset.
param
length the number of characters.
return
this StringBuffer.
throws
ArrayIndexOutOfBoundsException if {@code length < 0} , {@code start < 0} or {@code start + length > chars.length}.
since
Android 1.0

        append0(chars, start, length);
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.CharSequence s)
Appends the specified CharSequence to this buffer.

If the specified CharSequence is {@code null} the string {@code "null"} is appended, otherwise the contents of the specified CharSequence is appended.

param
s the CharSequence to append.
return
this StringBuffer.
since
Android 1.0

        if (s == null) {
            appendNull();
        } else {
            append0(s.toString());
        }
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.CharSequence s, int start, int end)
Appends the specified subsequence of the CharSequence to this buffer.

If the specified CharSequence is {@code null}, then the string {@code "null"} is used to extract a subsequence.

param
s the CharSequence to append.
param
start the inclusive start index.
param
end the exclusive end index.
return
this StringBuffer.
throws
IndexOutOfBoundsException if {@code start} or {@code end} are negative, {@code start} is greater than {@code end} or {@code end} is greater than the length of {@code s}.
since
Android 1.0

        append0(s, start, end);
        return this;
    
public java.lang.StringBufferappend(boolean b)
Adds the string representation of the specified boolean to the end of this StringBuffer.

If the argument is {@code true} the string {@code "true"} is appended, otherwise the string {@code "false"} is appended.

param
b the boolean to append.
return
this StringBuffer.
see
String#valueOf(boolean)
since
Android 1.0

        return append(b ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$
    
public synchronized java.lang.StringBufferappend(char ch)
Adds the specified character to the end of this buffer.

param
ch the character to append.
return
this StringBuffer.
see
String#valueOf(char)
since
Android 1.0

        append0(ch);
        return this;
    
public java.lang.StringBufferappend(double d)
Adds the string representation of the specified double to the end of this StringBuffer.

param
d the double to append.
return
this StringBuffer.
see
String#valueOf(double)
since
Android 1.0

        return append(Double.toString(d));
    
public java.lang.StringBufferappend(float f)
Adds the string representation of the specified float to the end of this StringBuffer.

param
f the float to append.
return
this StringBuffer.
see
String#valueOf(float)
since
Android 1.0

        return append(Float.toString(f));
    
public java.lang.StringBufferappend(int i)
Adds the string representation of the specified integer to the end of this StringBuffer.

param
i the integer to append.
return
this StringBuffer.
see
String#valueOf(int)
since
Android 1.0

        return append(Integer.toString(i));
    
public java.lang.StringBufferappendCodePoint(int codePoint)
Appends the string representation of the specified Unicode code point to the end of this buffer.

The code point is converted to a {@code char[]} as defined by {@link Character#toChars(int)}.

param
codePoint the Unicode code point to encode and append.
return
this StringBuffer.
see
Character#toChars(int)
since
Android 1.0

        return append(Character.toChars(codePoint));
    
public synchronized charcharAt(int index)

        return super.charAt(index);
    
public synchronized intcodePointAt(int index)

        return super.codePointAt(index);
    
public synchronized intcodePointBefore(int index)

        return super.codePointBefore(index);
    
public synchronized intcodePointCount(int beginIndex, int endIndex)

        return super.codePointCount(beginIndex, endIndex);
    
public synchronized java.lang.StringBufferdelete(int start, int end)
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.
throws
StringIndexOutOfBoundsException if {@code start < 0}, {@code start > end} or {@code end > length()}.
since
Android 1.0

        delete0(start, end);
        return this;
    
public synchronized java.lang.StringBufferdeleteCharAt(int location)
Deletes the character at the specified offset.

param
location the offset of the character to delete.
return
this StringBuffer.
throws
StringIndexOutOfBoundsException if {@code location < 0} or {@code location >= length()}
since
Android 1.0

        deleteCharAt0(location);
        return this;
    
public synchronized voidensureCapacity(int min)

        super.ensureCapacity(min);
    
public synchronized voidgetChars(int start, int end, char[] buffer, int idx)
Copies the requested sequence of characters to the {@code char[]} passed starting at {@code idx}.

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
idx the starting offset in the character array.
throws
IndexOutOfBoundsException if {@code start < 0}, {@code end > length()}, {@code start > end}, {@code index < 0}, {@code end - start > buffer.length - index}
since
Android 1.0

        super.getChars(start, end, buffer, idx);
    
public synchronized intindexOf(java.lang.String subString, int start)

        return super.indexOf(subString, start);
    
public synchronized java.lang.StringBufferinsert(int index, char ch)
Inserts the character into this buffer at the specified offset.

param
index the index at which to insert.
param
ch the character to insert.
return
this buffer.
throws
ArrayIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        insert0(index, ch);
        return this;
    
public java.lang.StringBufferinsert(int index, boolean b)
Inserts the string representation of the specified boolean into this buffer at the specified offset.

param
index the index at which to insert.
param
b the boolean to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, b ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
    
public java.lang.StringBufferinsert(int index, int i)
Inserts the string representation of the specified integer into this buffer at the specified offset.

param
index the index at which to insert.
param
i the integer to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, Integer.toString(i));
    
public java.lang.StringBufferinsert(int index, long l)
Inserts the string representation of the specified long into this buffer at the specified offset.

param
index the index at which to insert.
param
l the long to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, Long.toString(l));
    
public java.lang.StringBufferinsert(int index, double d)
Inserts the string representation of the specified into this buffer double at the specified offset.

param
index the index at which to insert.
param
d the double to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, Double.toString(d));
    
public java.lang.StringBufferinsert(int index, float f)
Inserts the string representation of the specified float into this buffer at the specified offset.

param
index the index at which to insert.
param
f the float to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, Float.toString(f));
    
public java.lang.StringBufferinsert(int index, java.lang.Object obj)
Inserts the string representation of the specified object into this buffer at the specified offset.

If the specified object is {@code null}, the string {@code "null"} is inserted, otherwise the objects {@code toString} method is used to get its string representation.

param
index the index at which to insert.
param
obj the object to insert (may be null).
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        return insert(index, obj == null ? "null" : obj.toString()); //$NON-NLS-1$
    
public synchronized java.lang.StringBufferinsert(int index, java.lang.String string)
Inserts the string into this buffer at the specified offset.

If the specified string is {@code null}, the string {@code "null"} is inserted, otherwise the contents of the string is inserted.

param
index the index at which to insert.
param
string the string to insert (may be null).
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        insert0(index, string);
        return this;
    
public synchronized java.lang.StringBufferinsert(int index, char[] chars)
Inserts the character array into this buffer at the specified offset.

param
index the index at which to insert.
param
chars the character array to insert.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        insert0(index, chars);
        return this;
    
public synchronized java.lang.StringBufferinsert(int index, char[] chars, int start, int length)
Inserts the specified subsequence of characters into this buffer at the specified index.

param
index the index at which to insert.
param
chars the character array to insert.
param
start the starting offset.
param
length the number of characters.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code length < 0}, {@code start < 0}, {@code start + length > chars.length}, {@code index < 0} or {@code index > length()}
since
Android 1.0

        insert0(index, chars, start, length);
        return this;
    
public synchronized java.lang.StringBufferinsert(int index, java.lang.CharSequence s)
Inserts the specified CharSequence into this buffer at the specified index.

If the specified CharSequence is {@code null}, the string {@code "null"} is inserted, otherwise the contents of the CharSequence.

param
index The index at which to insert.
param
s The char sequence to insert.
return
this buffer.
throws
IndexOutOfBoundsException if {@code index < 0} or {@code index > length()}.
since
Android 1.0

        insert0(index, s == null ? "null" : s.toString()); //$NON-NLS-1$
        return this;
    
public synchronized java.lang.StringBufferinsert(int index, java.lang.CharSequence s, int start, int end)
Inserts the specified subsequence into this buffer at the specified index.

If the specified CharSequence is {@code null}, the string {@code "null"} is inserted, otherwise the contents of the CharSequence.

param
index The index at which to insert.
param
s The char sequence to insert.
param
start The inclusive start index in the char sequence.
param
end The exclusive end index in the char sequence.
return
this buffer.
throws
IndexOutOfBoundsException if {@code index} is negative or greater than the current length, {@code start} or {@code end} are negative, {@code start} is greater than {@code end} or {@code end} is greater than the length of {@code s}.
since
Android 1.0

        insert0(index, s, start, end);
        return this;
    
public synchronized intlastIndexOf(java.lang.String subString, int start)

        return super.lastIndexOf(subString, start);
    
public synchronized intoffsetByCodePoints(int index, int codePointOffset)

        return super.offsetByCodePoints(index, codePointOffset);
    
private voidreadObject(java.io.ObjectInputStream in)

        ObjectInputStream.GetField fields = in.readFields();
        int count = fields.get("count", 0); //$NON-NLS-1$
        char[] value = (char[]) fields.get("value", null); //$NON-NLS-1$
        set(value, count);
    
public synchronized java.lang.StringBufferreplace(int start, int end, java.lang.String string)
Replaces the characters in the specified range with the contents of the specified string.

param
start the inclusive begin index.
param
end the exclusive end index.
param
string the string that will replace the contents in the range.
return
this buffer.
throws
StringIndexOutOfBoundsException if {@code start} or {@code end} are negative, {@code start} is greater than {@code end} or {@code end} is greater than the length of {@code s}.
since
Android 1.0

        replace0(start, end, string);
        return this;
    
public synchronized java.lang.StringBufferreverse()
Reverses the order of characters in this buffer.

return
this buffer.
since
Android 1.0

        reverse0();
        return this;
    
public synchronized voidsetCharAt(int index, char ch)

        super.setCharAt(index, ch);
    
public synchronized voidsetLength(int length)

        super.setLength(length);
    
public synchronized java.lang.CharSequencesubSequence(int start, int end)

        return super.substring(start, end);
    
public synchronized java.lang.Stringsubstring(int start)

        return super.substring(start);
    
public synchronized java.lang.Stringsubstring(int start, int end)

        return super.substring(start, end);
    
public synchronized java.lang.StringtoString()

        return super.toString();
    
public synchronized voidtrimToSize()

        super.trimToSize();
    
private synchronized voidwriteObject(java.io.ObjectOutputStream out)

        ObjectOutputStream.PutField fields = out.putFields();
        fields.put("count", length()); //$NON-NLS-1$
        fields.put("shared", false); //$NON-NLS-1$
        fields.put("value", getValue()); //$NON-NLS-1$
        out.writeFields();