📄 stringbuffer.java
字号:
result = 0; gotResult = false; } CVM.simpleLockRelease(this); if (gotResult) { return result; } } return charAt(index); } /** * Characters are copied from this string buffer into the * destination character array <code>dst</code>. The first character to * be copied is at index <code>srcBegin</code>; the last character to * be copied is at index <code>srcEnd-1</code>. The total number of * characters to be copied is <code>srcEnd-srcBegin</code>. The * characters are copied into the subarray of <code>dst</code> starting * at index <code>dstBegin</code> and ending at index: * <p><blockquote><pre> * dstbegin + (srcEnd-srcBegin) - 1 * </pre></blockquote> * * @param srcBegin start copying at this offset in the string buffer. * @param srcEnd stop copying at this offset in the string buffer. * @param dst the array to copy the data into. * @param dstBegin offset into <code>dst</code>. * @exception NullPointerException if <code>dst</code> is * <code>null</code>. * @exception IndexOutOfBoundsException if any of the following is true: * <ul> * <li><code>srcBegin</code> is negative * <li><code>dstBegin</code> is negative * <li>the <code>srcBegin</code> argument is greater than * the <code>srcEnd</code> argument. * <li><code>srcEnd</code> is greater than * <code>this.length()</code>, the current length of this * string buffer. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than * <code>dst.length</code> * </ul> */ public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {/* IAI - 15 */ if ((dstBegin < 0) || (((long)(dstBegin) + srcEnd -srcBegin) > dst.length)) { throw new StringIndexOutOfBoundsException(); }/* IAI - 15 */ if (srcBegin < 0) { throw new StringIndexOutOfBoundsException(srcBegin); } if ((srcEnd < 0) || (srcEnd > count)) { throw new StringIndexOutOfBoundsException(srcEnd); } if (srcBegin > srcEnd) { throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); }/* IAI - 15 */ if(srcEnd != srcBegin) { /* IAI - 17 */ CVM.copyCharArray(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); }/* IAI - 15 */ } /** * The character at the specified index of this string buffer is set * to <code>ch</code>. 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 <code>ch</code> at position * <code>index</code>. * <p> * The index argument must be greater than or equal to * <code>0</code>, and less than the length of this string buffer. * * @param index the index of the character to modify. * @param ch the new character. * @exception IndexOutOfBoundsException if <code>index</code> is * negative or greater than or equal to <code>length()</code>. * @see java.lang.StringBuffer#length() */ public synchronized void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } if (shared) copy(); value[index] = ch; } /** * Appends the string representation of the <code>Object</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 obj an <code>Object</code>. * @return a reference to this <code>StringBuffer</code> object. * @see java.lang.String#valueOf(java.lang.Object) * @see java.lang.StringBuffer#append(java.lang.String) */ public synchronized StringBuffer append(Object obj) { return append(String.valueOf(obj)); } /** * Appends the string to this string buffer. * <p> * The characters of the <code>String</code> 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 <code>str</code> is <code>null</code>, then the four characters * <code>"null"</code> are appended to this string buffer. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the string buffer just prior to execution of the * <code>append</code> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index <i>k</i> * in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the * argument <code>str</code>. * * @param str a string. * @return a reference to this <code>StringBuffer</code>. */ public synchronized StringBuffer append(String str) { if (str == null) { str = String.valueOf(str); } int len = str.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); str.getChars(0, len, value, count); count = newcount; return this; } /** * Appends the specified <tt>StringBuffer</tt> to this * <tt>StringBuffer</tt>. * <p> * The characters of the <tt>StringBuffer</tt> argument are appended, * in order, to the contents of this <tt>StringBuffer</tt>, increasing the * length of this <tt>StringBuffer</tt> by the length of the argument. * If <tt>sb</tt> is <tt>null</tt>, then the four characters * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the <tt>StringBuffer</tt> just prior to execution of the * <tt>append</tt> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index <i>k</i> * in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the * argument <code>sb</code>. * <p> * The method <tt>ensureCapacity</tt> is first called on this * <tt>StringBuffer</tt> with the new buffer length as its argument. * (This ensures that the storage of this <tt>StringBuffer</tt> is * adequate to contain the additional characters being appended.) * * @param sb the <tt>StringBuffer</tt> to append. * @return a reference to this <tt>StringBuffer</tt>. * @since 1.4 */ public synchronized StringBuffer append(StringBuffer sb) { if (sb == null) { sb = NULL; } int len = sb.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); sb.getChars(0, len, value, count); count = newcount; return this; } private static final StringBuffer NULL = new StringBuffer("null"); /** * Appends the string representation of the <code>char</code> array * argument to this string buffer. * <p> * 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. * <p> * 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 <code>StringBuffer</code> object. * * @param str the characters to be appended. * @return a reference to this <code>StringBuffer</code> object. */ public native synchronized StringBuffer append(char str[]); /* * The original Java version * * public synchronized StringBuffer append(char str[]) { * int len = str.length; * int newcount = count + len; * if (newcount > value.length) * expandCapacity(newcount); * System.arraycopy(str, 0, value, count, len); * count = newcount; * return this; * } */ /** * Appends the string representation of a subarray of the * <code>char</code> array argument to this string buffer. * <p> * Characters of the character array <code>str</code>, starting at * index <code>offset</code>, are appended, in order, to the contents * of this string buffer. The length of this string buffer increases * by the value of <code>len</code>. * <p> * 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 <code>StringBuffer</code> object. * * @param str the characters to be appended. * @param offset the index of the first character to append. * @param len the number of characters to append. * @return a reference to this <code>StringBuffer</code> object. */ public native synchronized StringBuffer append(char str[], int offset, int len); /* * The original Java version * * public synchronized StringBuffer append(char str[], int offset, int len) { * int newcount = count + len; * if (newcount > value.length) * expandCapacity(newcount); * System.arraycopy(str, offset, value, count, len); * count = newcount; * return this; * } */ /** * Appends the string representation of the <code>boolean</code> * argument to the 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 b a <code>boolean</code>. * @return a reference to this <code>StringBuffer</code>. * @see java.lang.String#valueOf(boolean) * @see java.lang.StringBuffer#append(java.lang.String) */ 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>. * <p> * 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 * <code>StringBuffer</code> object. * * @param c a <code>char</code>. * @return a reference to this <code>StringBuffer</code> object. */ public synchronized StringBuffer append(char c) { int newcount = count + 1; if (newcount > value.length) expandCapacity(newcount); value[count++] = c; return this; } private StringBuffer appendSimpleSync(char c) { if (CVM.simpleLockGrab(this)) { boolean gotResult; if (count + 1 <= value.length) { value[count++] = c; gotResult = true; } else { gotResult = false; } CVM.simpleLockRelease(this); if (gotResult) { return this; } } return append(c); } /** * 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 a reference to this <code>StringBuffer</code> object. * @see java.lang.String#valueOf(int) * @see java.lang.StringBuffer#append(java.lang.String) */ public synchronized StringBuffer append(int i) { Integer.appendTo(i, this); return this; } /** * 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 a reference to this <code>StringBuffer</code> object. * @see java.lang.String#valueOf(long) * @see java.lang.StringBuffer#append(java.lang.String) */ public synchronized StringBuffer append(long l) { Long.appendTo(l, this); return this; } /** * 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>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -