stringbuffer.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 806 行 · 第 1/2 页
JAVA
806 行
* @see java.lang.String#valueOf(boolean)
* @see java.lang.StringBuffer#append(java.lang.String)
* @since JDK1.0
*/
public StringBuffer append(boolean b) {
return append(String.valueOf(b));
}
/**
* Appends the string representation of the <code>char</code>
* argument to this string buffer.
* <p>
* The argument is appended to the contents of this string buffer.
* The length of this string buffer increases by <code>1</code>.
*
* @param ch a <code>char</code>.
* @return this string buffer.
* @since JDK1.0
*/
public synchronized StringBuffer append(char c) {
int newcount = count + 1;
if (newcount > value.length)
expandCapacity(newcount);
value[count++] = c;
return this;
}
/**
* Appends the string representation of the <code>int</code>
* argument to this string buffer.
* <p>
* The argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then appended to this string buffer.
*
* @param i an <code>int</code>.
* @return this string buffer.
* @see java.lang.String#valueOf(int)
* @see java.lang.StringBuffer#append(java.lang.String)
* @since JDK1.0
*/
public StringBuffer append(int i) {
return append(String.valueOf(i));
}
/**
* Appends the string representation of the <code>long</code>
* argument to this string buffer.
* <p>
* The argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then appended to this string buffer.
*
* @param l a <code>long</code>.
* @return this string buffer.
* @see java.lang.String#valueOf(long)
* @see java.lang.StringBuffer#append(java.lang.String)
* @since JDK1.0
*/
public StringBuffer append(long l) {
return append(String.valueOf(l));
}
/**
* Appends the string representation of the <code>float</code>
* argument to this string buffer.
* <p>
* The argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then appended to this string buffer.
*
* @param f a <code>float</code>.
* @return this string buffer.
* @see java.lang.String#valueOf(float)
* @see java.lang.StringBuffer#append(java.lang.String)
* @since JDK1.0
*/
public StringBuffer append(float f) {
return append(String.valueOf(f));
}
/**
* Appends the string representation of the <code>double</code>
* argument to this string buffer.
* <p>
* The argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then appended to this string buffer.
*
* @param d a <code>double</code>.
* @return this string buffer.
* @see java.lang.String#valueOf(double)
* @see java.lang.StringBuffer#append(java.lang.String)
* @since JDK1.0
*/
public StringBuffer append(double d) {
return append(String.valueOf(d));
}
/**
* Inserts the string representation of the <code>Object</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b an <code>Object</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(java.lang.Object)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public synchronized StringBuffer insert(int offset, Object obj) {
return insert(offset, String.valueOf(obj));
}
/**
* Inserts the string into this string buffer.
* <p>
* The characters of the <code>String</code> argument are inserted, in
* order, into this string buffer at the indicated offset. The length
* of this string buffer is increased by the length of the argument.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param str a string.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public synchronized StringBuffer insert(int offset, String str) {
if ((offset < 0) || (offset > count)) {
throw new StringIndexOutOfBoundsException();
}
int len = str.length();
int newcount = count + len;
if (newcount > value.length)
expandCapacity(newcount);
else if (shared)
copy();
System.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(0, len, value, offset);
count = newcount;
return this;
}
/**
* Inserts the string representation of the <code>char</code> array
* argument into this string buffer.
* <p>
* The characters of the array argument are inserted into the
* contents of this string buffer at the position indicated by
* <code>offset</code>. The length of this string buffer increases by
* the length of the argument.
*
* @param offset the offset.
* @param ch a character array.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @since JDK1.0
*/
public synchronized StringBuffer insert(int offset, char str[]) {
if ((offset < 0) || (offset > count)) {
throw new StringIndexOutOfBoundsException();
}
int len = str.length;
int newcount = count + len;
if (newcount > value.length)
expandCapacity(newcount);
else if (shared)
copy();
System.arraycopy(value, offset, value, offset + len, count - offset);
System.arraycopy(str, 0, value, offset, len);
count = newcount;
return this;
}
/**
* Inserts the string representation of the <code>boolean</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b a <code>boolean</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(boolean)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public StringBuffer insert(int offset, boolean b) {
return insert(offset, String.valueOf(b));
}
/**
* Inserts the string representation of the <code>char</code>
* argument into this string buffer.
* <p>
* The second argument is inserted into the contents of this string
* buffer at the position indicated by <code>offset</code>. The length
* of this string buffer increases by one.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param ch a <code>char</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public synchronized StringBuffer insert(int offset, char c) {
int newcount = count + 1;
if (newcount > value.length)
expandCapacity(newcount);
else if (shared)
copy();
System.arraycopy(value, offset, value, offset + 1, count - offset);
value[offset] = c;
count = newcount;
return this;
}
/**
* Inserts the string representation of the second <code>int</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b an <code>int</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(int)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public StringBuffer insert(int offset, int i) {
return insert(offset, String.valueOf(i));
}
/**
* Inserts the string representation of the <code>long</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b a <code>long</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(long)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public StringBuffer insert(int offset, long l) {
return insert(offset, String.valueOf(l));
}
/**
* Inserts the string representation of the <code>float</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b a <code>float</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(float)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public StringBuffer insert(int offset, float f) {
return insert(offset, String.valueOf(f));
}
/**
* Inserts the string representation of the <code>double</code>
* argument into this string buffer.
* <p>
* The second argument is converted to a string as if by the method
* <code>String.valueOf</code>, and the characters of that
* string are then inserted into this string buffer at the indicated
* offset.
* <p>
* The offset argument must be greater than or equal to
* <code>0</code>, and less than or equal to the length of this
* string buffer.
*
* @param offset the offset.
* @param b a <code>double</code>.
* @return this string buffer.
* @exception StringIndexOutOfBoundsException if the offset is invalid.
* @see java.lang.String#valueOf(double)
* @see java.lang.StringBuffer#insert(int, java.lang.String)
* @see java.lang.StringBuffer#length()
* @since JDK1.0
*/
public StringBuffer insert(int offset, double d) {
return insert(offset, String.valueOf(d));
}
/**
* The character sequence contained in this string buffer is
* replaced by the reverse of the sequence.
*
* @return this string buffer.
* @since JDK1.0.2
*/
public synchronized StringBuffer reverse() {
if (shared) copy();
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; --j) {
char temp = value[j];
value[j] = value[n - j];
value[n - j] = temp;
}
return this;
}
/**
* Converts to a string representing the data in this string buffer.
* A new <code>String</code> object is allocated and initialized to
* contain the character sequence currently represented by this
* string buffer. This <code>String</code> is then returned. Subsequent
* changes to the string buffer do not affect the contents of the
* <code>String</code>.
*
* @return a string representation of the string buffer.
* @since JDK1.0
*/
public String toString() {
return new String(this);
}
//
// The following two methods are needed by String to efficiently
// convert a StringBuffer into a String. They are not public.
// They shouldn't be called by anyone but String.
final void setShared() { shared = true; }
final char[] getValue() { return value; }
/**
* readObject is called to restore the state of the StringBuffer from
* a stream.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
value = (char[]) value.clone();
shared = false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?