stringbuffer.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 922 行 · 第 1/3 页
JAVA
922 行
{ if (str == null) str = "null"; int len = str.count; ensureCapacity_unsynchronized(count + len); str.getChars(0, len, value, count); count += len; return this; } /** * Append the <code>StringBuffer</code> value of the argument to this * <code>StringBuffer</code>. This behaves the same as * <code>append((Object) stringBuffer)</code>, except it is more efficient. * * @param stringBuffer the <code>StringBuffer</code> to convert and append * @return this <code>StringBuffer</code> * @see #append(Object) * @since 1.4 */ public synchronized StringBuffer append(StringBuffer stringBuffer) { if (stringBuffer == null) return append("null"); synchronized (stringBuffer) { int len = stringBuffer.count; ensureCapacity_unsynchronized(count + len); System.arraycopy(stringBuffer.value, 0, value, count, len); count += len; } return this; } /** * Append the <code>char</code> array to this <code>StringBuffer</code>. * This is similar (but more efficient) than * <code>append(new String(data))</code>, except in the case of null. * * @param data the <code>char[]</code> to append * @return this <code>StringBuffer</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @see #append(char[], int, int) */ public StringBuffer append(char[] data) { return append(data, 0, data.length); } /** * Append part of the <code>char</code> array to this * <code>StringBuffer</code>. This is similar (but more efficient) than * <code>append(new String(data, offset, count))</code>, except in the case * of null. * * @param data the <code>char[]</code> to append * @param offset the start location in <code>str</code> * @param count the number of characters to get from <code>str</code> * @return this <code>StringBuffer</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @throws IndexOutOfBoundsException if offset or count is out of range * (while unspecified, this is a StringIndexOutOfBoundsException) */ public synchronized StringBuffer append(char[] data, int offset, int count) { if (offset < 0 || count < 0 || offset > data.length - count) throw new StringIndexOutOfBoundsException(); ensureCapacity_unsynchronized(this.count + count); System.arraycopy(data, offset, value, this.count, count); this.count += count; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param bool the <code>boolean</code> to convert and append * @return this <code>StringBuffer</code> * @see String#valueOf(boolean) */ public StringBuffer append(boolean bool) { return append(bool ? "true" : "false"); } /** * Append the <code>char</code> to this <code>StringBuffer</code>. * * @param ch the <code>char</code> to append * @return this <code>StringBuffer</code> */ public synchronized StringBuffer append(char ch) { ensureCapacity_unsynchronized(count + 1); value[count++] = ch; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param inum the <code>int</code> to convert and append * @return this <code>StringBuffer</code> * @see String#valueOf(int) */ // GCJ LOCAL: this is native for efficiency. public native StringBuffer append (int inum); /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param lnum the <code>long</code> to convert and append * @return this <code>StringBuffer</code> * @see String#valueOf(long) */ public StringBuffer append(long lnum) { return append(Long.toString(lnum, 10)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param fnum the <code>float</code> to convert and append * @return this <code>StringBuffer</code> * @see String#valueOf(float) */ public StringBuffer append(float fnum) { return append(Float.toString(fnum)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param dnum the <code>double</code> to convert and append * @return this <code>StringBuffer</code> * @see String#valueOf(double) */ public StringBuffer append(double dnum) { return append(Double.toString(dnum)); } /** * Delete characters from this <code>StringBuffer</code>. * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is * harmless for end to be larger than length(). * * @param start the first character to delete * @param end the index after the last character to delete * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds * @since 1.2 */ public synchronized StringBuffer delete(int start, int end) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; // This will unshare if required. ensureCapacity_unsynchronized(count); if (count - end != 0) System.arraycopy(value, end, value, start, count - end); count -= end - start; return this; } /** * Delete a character from this <code>StringBuffer</code>. * * @param index the index of the character to delete * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if index is out of bounds * @since 1.2 */ public StringBuffer deleteCharAt(int index) { return delete(index, index + 1); } /** * Replace characters between index <code>start</code> (inclusive) and * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code> * is larger than the size of this StringBuffer, all characters after * <code>start</code> are replaced. * * @param start the beginning index of characters to delete (inclusive) * @param end the ending index of characters to delete (exclusive) * @param str the new <code>String</code> to insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds * @throws NullPointerException if str is null * @since 1.2 */ public synchronized StringBuffer replace(int start, int end, String str) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException(start); int len = str.count; // Calculate the difference in 'count' after the replace. int delta = len - (end > count ? count : end) + start; ensureCapacity_unsynchronized(count + delta); if (delta != 0 && end < count) System.arraycopy(value, end, value, end + delta, count - end); str.getChars(0, len, value, start); count += delta; return this; } /** * Creates a substring of this StringBuffer, starting at a specified index * and ending at the end of this StringBuffer. * * @param beginIndex index to start substring (base 0) * @return new String which is a substring of this StringBuffer * @throws StringIndexOutOfBoundsException if beginIndex is out of bounds * @see #substring(int, int) * @since 1.2 */ public String substring(int beginIndex) { return substring(beginIndex, count); } /** * Creates a substring of this StringBuffer, starting at a specified index * and ending at one character before a specified index. This is implemented * the same as <code>substring(beginIndex, endIndex)</code>, to satisfy * the CharSequence interface. * * @param beginIndex index to start at (inclusive, base 0) * @param endIndex index to end at (exclusive) * @return new String which is a substring of this StringBuffer * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of * bounds * @see #substring(int, int) * @since 1.4 */ public CharSequence subSequence(int beginIndex, int endIndex) { return substring(beginIndex, endIndex); } /** * Creates a substring of this StringBuffer, starting at a specified index * and ending at one character before a specified index. * * @param beginIndex index to start at (inclusive, base 0) * @param endIndex index to end at (exclusive) * @return new String which is a substring of this StringBuffer * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out * of bounds * @since 1.2 */ public synchronized String substring(int beginIndex, int endIndex) { int len = endIndex - beginIndex; if (beginIndex < 0 || endIndex > count || endIndex < beginIndex) throw new StringIndexOutOfBoundsException(); if (len == 0) return ""; // Don't copy unless substring is smaller than 1/4 of the buffer. boolean share_buffer = ((len << 2) >= value.length); if (share_buffer) this.shared = true; // Package constructor avoids an array copy. return new String(value, beginIndex, len, share_buffer); } /** * Insert a subarray of the <code>char[]</code> argument into this * <code>StringBuffer</code>. * * @param offset the place to insert in this buffer * @param str the <code>char[]</code> to insert * @param str_offset the index in <code>str</code> to start inserting from * @param len the number of characters to insert * @return this <code>StringBuffer</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @throws StringIndexOutOfBoundsException if any index is out of bounds * @since 1.2 */ public synchronized StringBuffer insert(int offset, char[] str, int str_offset, int len) { if (offset < 0 || offset > count || len < 0 || str_offset < 0 || str_offset > str.length - len) throw new StringIndexOutOfBoundsException(); ensureCapacity_unsynchronized(count + len); System.arraycopy(value, offset, value, offset + len, count - offset); System.arraycopy(str, str_offset, value, offset, len); count += len; return this;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?