All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----buildtools.PortableStringBuffer
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
String buffers are used by the compiler to implement the binary
string concatenation operator +
. For example, the code:
x = "a" + 4 + "c"
is compiled to the equivalent of:
which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.x = new StringBuffer().append("a").append(4).append("c") .toString()
The principal operations on a StringBuffer
are the
append
and insert
methods, which are
overloaded so as to accept data of any type. Each effectively
converts a given datum to a string and then appends or inserts the
characters of that string to the string buffer. The
append
method always adds these characters at the end
of the buffer; the insert
method adds the characters at
a specified point.
For example, if z
refers to a string buffer object
whose current contents are "start
", then
the method call z.append("le")
would cause the string
buffer to contain "startle
", whereas
z.insert(4, "le")
would alter the string buffer to
contain "starlet
".
In general, if sb refers to an instance of a StringBuffer
,
then sb.append(x)
has the same effect as
sb.insert(sb.length(), x)
.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.
length
argument.
boolean
argument to the string buffer.
char
argument to this string buffer.
char
array
argument to this string buffer.
char
array argument to this string buffer.
double
argument to this string buffer.
float
argument to this string buffer.
int
argument to this string buffer.
long
argument to this string buffer.
Object
argument to this string buffer.
index
argument,
is returned.
PortableStringBuffer
.
PortableStringBuffer
(shortening the PortableStringBuffer
by one character).
dst
.
boolean
argument into this string buffer.
char
argument into this string buffer.
char
array
argument into this string buffer.
str
array argument into this string buffer.
double
argument into this string buffer.
float
argument into this string buffer.
int
argument into this string buffer.
long
argument into this string buffer.
Object
argument into this string buffer.
PortableStringBuffer
with characters in the specified String
.
ch
.
String
that contains a subsequence of
characters currently contained in this PortableStringBuffer
.The
substring begins at the specified index and extends to the end of the
PortableStringBuffer
.
String
that contains a subsequence of
characters currently contained in this PortableStringBuffer
.
private char value[]
private int count
private boolean shared
static final long serialVersionUID
public PortableStringBuffer()
public PortableStringBuffer(int length)
length
argument.
length
argument is less than 0
.
public PortableStringBuffer(String str)
16
plus the length of the string argument.
public int length()
public int capacity()
private final void copy()
public synchronized void ensureCapacity(int minimumCapacity)
minimumCapacity
argument.
2
.
minimumCapacity
argument is nonpositive, this
method takes no action and simply returns.
private void expandCapacity(int minimumCapacity)
public synchronized void setLength(int newLength)
newLength
, the character at
index k in the new character sequence is the same as the
character at index k in the old sequence if k is less
than the length of the old character sequence; otherwise, it is the
null character ' '
.
In other words, if the newLength
argument is less than
the current length of the string buffer, the string buffer is
truncated to contain exactly the number of characters given by the
newLength
argument.
If the newLength
argument is greater than or equal
to the current length, sufficient null characters
('\u0000'
) are appended to the string buffer so that
length becomes the newLength
argument.
The newLength
argument must be greater than or equal
to 0
.
newLength
argument is negative.
public synchronized char charAt(int index)
index
argument,
is returned. The first character of a string buffer is at index
0
, the next at index 1
, and so on, for
array indexing.
The index argument must be greater than or equal to
0
, and less than the length of this string buffer.
index
is
negative or greater than or equal to length()
.
public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
dst
. The first character to
be copied is at index srcBegin
; the last character to
be copied is at index srcEnd-1
. The total number of
characters to be copied is srcEnd-srcBegin
. The
characters are copied into the subarray of dst
starting
at index dstBegin
and ending at index:
dstbegin + (srcEnd-srcBegin) - 1
dst
.
dst
is
null
.
srcBegin
is negative
srcBeing
argument is greater than
the srcEnd
argument.
srcEnd
is greater than
this.length()
, the current length of this
string buffer.
dstBegin+srcEnd-srcBegin
is greater than
dst.length
public synchronized void setCharAt(int index, char ch)
ch
. The string buffer is altered to represent a new
character sequence that is identical to the old character sequence,
except that it contains the character ch
at position
index
.
The offset argument must be greater than or equal to
0
, and less than the length of this string buffer.
index
is
negative or greater than or equal to length()
.
public synchronized PortableStringBuffer append(Object obj)
Object
argument to this string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
Object
.
PortableStringBuffer
object.
public synchronized PortableStringBuffer append(String str)
The characters of the String
argument are appended, in
order, to the contents of this string buffer, increasing the
length of this string buffer by the length of the argument.
If str
is null
, then the four characters
"null"
are appended to this string buffer.
Let n be the length of the old character sequence, the one
contained in the string buffer just prior to execution of the
append
method. Then the character at index k in
the new character sequence is equal to the character at index k
in the old character sequence, if k is less than n;
otherwise, it is equal to the character at index k-n in the
argument str
.
StringBuffer
.
public synchronized PortableStringBuffer append(char str[])
char
array
argument to this string buffer.
The characters of the array argument are appended, in order, to the contents of this string buffer. The length of this string buffer increases by the length of the argument.
The overall effect is exactly as if the argument were converted to
a string by the method {@link String#valueOf(char[])} and the
characters of that string were then {@link #append(String) appended}
to this PortableStringBuffer
object.
PortableStringBuffer
object.
public synchronized PortableStringBuffer append(char str[], int offset, int len)
char
array argument to this string buffer.
Characters of the character array str
, starting at
index offset
, are appended, in order, to the contents
of this string buffer. The length of this string buffer increases
by the value of len
.
The overall effect is exactly as if the arguments were converted to
a string by the method {@link String#valueOf(char[],int,int)} and the
characters of that string were then {@link #append(String) appended}
to this PortableStringBuffer
object.
PortableStringBuffer
object.
public PortableStringBuffer append(boolean b)
boolean
argument to the string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
boolean
.
PortableStringBuffer
.
public synchronized PortableStringBuffer append(char c)
char
argument to this string buffer.
The argument is appended to the contents of this string buffer.
The length of this string buffer increases by 1
.
The overall effect is exactly as if the argument were converted to
a string by the method {@link String#valueOf(char)} and the character
in that string were then {@link #append(String) appended} to this
PortableStringBuffer
object.
char
.
PortableStringBuffer
object.
public PortableStringBuffer append(int i)
int
argument to this string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
int
.
PortableStringBuffer
object.
public PortableStringBuffer append(long l)
long
argument to this string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
long
.
PortableStringBuffer
object.
public PortableStringBuffer append(float f)
float
argument to this string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
float
.
StringBuffer
object.
public PortableStringBuffer append(double d)
double
argument to this string buffer.
The argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then appended to this string buffer.
double
.
PortableStringBuffer
object.
public synchronized PortableStringBuffer delete(int start, int end)
PortableStringBuffer
.
The substring begins at the specified start
and extends to
the character at index end - 1
or to the end of the
PortableStringBuffer
if no such character exists. If
start
is equal to end
, no changes are made.
start
is negative, greater than length()
, or
greater than end
.
public synchronized PortableStringBuffer deleteCharAt(int index)
PortableStringBuffer
(shortening the PortableStringBuffer
by one character).
index
is negative or greater than or equal to
length()
.
public synchronized PortableStringBuffer replace(int start, int end, String str)
PortableStringBuffer
with characters in the specified String
. The substring
begins at the specified start
and extends to the character
at index end - 1
or to the end of the
PortableStringBuffer
if no such character exists. First the
characters in the substring are removed and then the specified
String
is inserted at start
. (The
PortableStringBuffer
will be lengthened to accommodate the
specified String if necessary.)
start
is negative, greater than length()
, or
greater than end
.
public String substring(int start)
String
that contains a subsequence of
characters currently contained in this PortableStringBuffer
.The
substring begins at the specified index and extends to the end of the
PortableStringBuffer
.
start
is
less than zero, or greater than the length of this
PortableStringBuffer
.
public synchronized String substring(int start, int end)
String
that contains a subsequence of
characters currently contained in this PortableStringBuffer
. The
substring begins at the specified start
and
extends to the character at index end - 1
. An
exception is thrown if
start
or end
are negative or greater than
length()
, or start
is
greater than end
.
public synchronized PortableStringBuffer insert(int index, char str[], int offset, int len)
str
array argument into this string buffer. The subarray begins at the
specified offset
and extends len
characters.
The characters of the subarray are inserted into this string buffer at
the position indicated by index
. The length of this
PortableStringBuffer
increases by len
characters.
index
is negative or greater than length()
, or
offset
or len
are negative, or
(offset+len)
is greater than
str.length
.
public synchronized PortableStringBuffer insert(int offset, Object obj)
Object
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
Object
.
PortableStringBuffer
object.
public synchronized PortableStringBuffer insert(int offset, String str)
The characters of the String
argument are inserted, in
order, into this string buffer at the indicated offset, moving up any
characters originally above that position and increasing the length
of this string buffer by the length of the argument. If
str
is null
, then the four characters
"null"
are inserted into this string buffer.
The character at index k in the new character sequence is equal to:
offset
-offset
in the
argument str
, if k is not less than
offset
but is less than offset+str.length()
-str.length()
in the
old character sequence, if k is not less than
offset+str.length()
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
PortableStringBuffer
object.
public synchronized PortableStringBuffer insert(int offset, char str[])
char
array
argument into this string buffer.
The characters of the array argument are inserted into the
contents of this string buffer at the position indicated by
offset
. The length of this string buffer increases by
the length of the argument.
The overall effect is exactly as if the argument were converted to
a string by the method {@link String#valueOf(char[])} and the
characters of that string were then
{@link #insert(int,String) inserted} into this
PortableStringBuffer
object at the position indicated by
offset
.
PortableStringBuffer
object.
public PortableStringBuffer insert(int offset, boolean b)
boolean
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
boolean
.
PortableStringBuffer
object.
public synchronized PortableStringBuffer insert(int offset, char c)
char
argument into this string buffer.
The second argument is inserted into the contents of this string
buffer at the position indicated by offset
. The length
of this string buffer increases by one.
The overall effect is exactly as if the argument were converted to
a string by the method {@link String#valueOf(char)} and the character
in that string were then {@link #insert(int, String) inserted} into
this PortableStringBuffer
object at the position indicated by
offset
.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
char
.
PortableStringBuffer
object.
public PortableStringBuffer insert(int offset, int i)
int
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
int
.
PortableStringBuffer
object.
public PortableStringBuffer insert(int offset, long l)
long
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the position
indicated by offset
.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
long
.
PortableStringBuffer
object.
public PortableStringBuffer insert(int offset, float f)
float
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
float
.
PortableStringBuffer
object.
public PortableStringBuffer insert(int offset, double d)
double
argument into this string buffer.
The second argument is converted to a string as if by the method
String.valueOf
, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
double
.
PortableStringBuffer
object.
public synchronized PortableStringBuffer reverse()
Let n be the length of the old character sequence, the one
contained in the string buffer just prior to execution of the
reverse
method. Then the character at index k in
the new character sequence is equal to the character at index
n-k-1 in the old character sequence.
public String toString()
String
object is allocated and initialized to
contain the character sequence currently represented by this
string buffer. This String
is then returned. Subsequent
changes to the string buffer do not affect the contents of the
String
.
Implementation advice: This method can be coded so as to create a new
String
object without allocating new memory to hold a
copy of the character sequence. Instead, the string can share the
memory used by the string buffer. Any subsequent operation that alters
the content or capacity of the string buffer must then make a copy of
the internal buffer at that time. This strategy is effective for
reducing the amount of memory allocated by a string concatenation
operation when it is implemented using a string buffer.
final void setShared()
final char[] getValue()
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
All Packages Class Hierarchy This Package Previous Next Index