/* * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang; import sun.misc.FloatingDecimal;import java.util.Arrays; /** * A mutable sequence of characters. * <p> * Implements a modifiable string. At any point in time it contains some * particular sequence of characters, but the length and content of the * sequence can be changed through certain method calls. * * @author Michael McCloskey * @author Martin Buchholz * @author Ulf Zibis * @since 1.5 */abstract class AbstractStringBuilder implements Appendable, CharSequence { /** * The value is used for character storage. */ char[] value; /** * The count is the number of characters used. */ int count; /** * This no-arg constructor is necessary for serialization of subclasses. */ AbstractStringBuilder() { } /** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; } /** * Returns the length (character count). * * @return the length of the sequence of characters currently * represented by this object */ public int length() { return count; } /** * Returns the current capacity. The capacity is the amount of storage * available for newly inserted characters, beyond which an allocation * will occur. * * @return the current capacity */ public int capacity() { return value.length; } /** * Ensures that the capacity is at least equal to the specified minimum. * If the current capacity is less than the argument, then a new internal * array is allocated with greater capacity. The new capacity is the * larger of: * <ul> * <li>The <code>minimumCapacity</code> argument. * <li>Twice the old capacity, plus <code>2</code>. * </ul> * If the <code>minimumCapacity</code> argument is nonpositive, this * method takes no action and simply returns. * * @param minimumCapacity the minimum desired capacity. */ public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > 0) ensureCapacityInternal(minimumCapacity); } /** * This method has the same contract as ensureCapacity, but is * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity); } /** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); } /** * Attempts to reduce storage used for the character sequence. * If the buffer is larger than necessary to hold its current sequence of * characters, then it may be resized to become more space efficient. * Calling this method may, but is not required to, affect the value * returned by a subsequent call to the {@link #capacity()} method. */ public void trimToSize() { if (count < value.length) { value = Arrays.copyOf(value, count); } } /** * Sets the length of the character sequence. * The sequence is changed to a new character sequence * whose length is specified by the argument. For every nonnegative * index <i>k</i> less than <code>newLength</code>, the character at * index <i>k</i> in the new character sequence is the same as the * character at index <i>k</i> in the old sequence if <i>k</i> is less * than the length of the old character sequence; otherwise, it is the * null character <code>'\u0000'</code>. * * In other words, if the <code>newLength</code> argument is less than * the current length, the length is changed to the specified length. * <p> * If the <code>newLength</code> argument is greater than or equal * to the current length, sufficient null characters * (<code>'\u0000'</code>) are appended so that * length becomes the <code>newLength</code> argument. * <p> * The <code>newLength</code> argument must be greater than or equal * to <code>0</code>. * * @param newLength the new length * @throws IndexOutOfBoundsException if the * <code>newLength</code> argument is negative. */ public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { for (; count < newLength; count++) value[count] = '\0'; } else { count = newLength; } } /** * Returns the <code>char</code> value in this sequence at the specified index. * The first <code>char</code> value is at index <code>0</code>, the next at index * <code>1</code>, and so on, as in array indexing. * <p> * The index argument must be greater than or equal to * <code>0</code>, and less than the length of this sequence. * * <p>If the <code>char</code> value specified by the index is a * <a href="Character.html#unicode">surrogate</a>, the surrogate * value is returned. * * @param index the index of the desired <code>char</code> value. * @return the <code>char</code> value at the specified index. * @throws IndexOutOfBoundsException if <code>index</code> is * negative or greater than or equal to <code>length()</code>. */ public char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; } /** * Returns the character (Unicode code point) at the specified * index. The index refers to <code>char</code> values * (Unicode code units) and ranges from <code>0</code> to * {@link #length()}<code> - 1</code>. * * <p> If the <code>char</code> value specified at the given index * is in the high-surrogate range, the following index is less * than the length of this sequence, and the * <code>char</code> value at the following index is in the * low-surrogate range, then the supplementary code point * corresponding to this surrogate pair is returned. Otherwise, * the <code>char</code> value at the given index is returned. * * @param index the index to the <code>char</code> values * @return the code point value of the character at the * <code>index</code> * @exception IndexOutOfBoundsException if the <code>index</code> * argument is negative or not less than the length of this * sequence. */ public int codePointAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointAt(value, index); } /** * Returns the character (Unicode code point) before the specified * index. The index refers to <code>char</code> values * (Unicode code units) and ranges from <code>1</code> to {@link * #length()}. * * <p> If the <code>char</code> value at <code>(index - 1)</code> * is in the low-surrogate range, <code>(index - 2)</code> is not * negative, and the <code>char</code> value at <code>(index - * 2)</code> is in the high-surrogate range, then the * supplementary code point value of the surrogate pair is * returned. If the <code>char</code> value at <code>index - * 1</code> is an unpaired low-surrogate or a high-surrogate, the * surrogate value is returned. * * @param index the index following the code point that should be returned * @return the Unicode code point value before the given index. * @exception IndexOutOfBoundsException if the <code>index</code> * argument is less than 1 or greater than the length * of this sequence. */ public int codePointBefore(int index) { int i = index - 1; if ((i < 0) || (i >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointBefore(value, index); } /** * Returns the number of Unicode code points in the specified text * range of this sequence. The text range begins at the specified * <code>beginIndex</code> and extends to the <code>char</code> at * index <code>endIndex - 1</code>. Thus the length (in * <code>char</code>s) of the text range is * <code>endIndex-beginIndex</code>. Unpaired surrogates within * this sequence count as one code point each. * * @param beginIndex the index to the first <code>char</code> of * the text range. * @param endIndex the index after the last <code>char</code> of * the text range. * @return the number of Unicode code points in the specified text * range * @exception IndexOutOfBoundsException if the * <code>beginIndex</code> is negative, or <code>endIndex</code> * is larger than the length of this sequence, or * <code>beginIndex</code> is larger than <code>endIndex</code>. */ public int codePointCount(int beginIndex, int endIndex) { if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { throw new IndexOutOfBoundsException(); } return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex); } /** * Returns the index within this sequence that is offset from the * given <code>index</code> by <code>codePointOffset</code> code * points. Unpaired surrogates within the text range given by * <code>index</code> and <code>codePointOffset</code> count as * one code point each. * * @param index the index to be offset * @param codePointOffset the offset in code points * @return the index within this sequence * @exception IndexOutOfBoundsException if <code>index</code> * is negative or larger then the length of this sequence, * or if <code>codePointOffset</code> is positive and the subsequence * starting with <code>index</code> has fewer than * <code>codePointOffset</code> code points, * or if <code>codePointOffset</code> is negative and the subsequence * before <code>index</code> has fewer than the absolute value of * <code>codePointOffset</code> code points. */ public int offsetByCodePoints(int index, int codePointOffset) { if (index < 0 || index > count) { throw new IndexOutOfBoundsException(); } return Character.offsetByCodePointsImpl(value, 0, count, index, codePointOffset); } /** * Characters are copied from this sequence 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. * @param srcEnd stop copying at this offset. * @param dst the array to copy the data into. * @param dstBegin offset into <code>dst</code>. * @throws NullPointerException if <code>dst</code> is * <code>null</code>. * @throws 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>. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than * <code>dst.length</code> * </ul> */ public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { if (srcBegin < 0) throw new StringIndexOutOfBoundsException(srcBegin); if ((srcEnd < 0) || (srcEnd > count)) throw new StringIndexOutOfBoundsException(srcEnd); if (srcBegin > srcEnd) throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); } /** * The character at the specified index is set to <code>ch</code>. This * sequence 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 sequence. * * @param index the index of the character to modify. * @param ch the new character. * @throws IndexOutOfBoundsException if <code>index</code> is * negative or greater than or equal to <code>length()</code>. */ public void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; } /** * Appends the string representation of the {@code Object} argument. * <p> * The overall effect is exactly as if the argument were converted * to a string by the method {@link String#valueOf(Object)}, * and the characters of that string were then * {@link #append(String) appended} to this character sequence. * * @param obj an {@code Object}. * @return a reference to this object. */ public AbstractStringBuilder append(Object obj) { return append(String.valueOf(obj)); } /** * Appends the specified string to this character sequence. * <p> * The characters of the {@code String} argument are appended, in * order, increasing the length of this sequence by the length of the * argument. If {@code str} is {@code null}, then the four * characters {@code "null"} are appended. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} 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}. * * @param str a string. * @return a reference to this object. */ public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(StringBuffer sb) { if (sb == null) return append("null"); int len = sb.length(); ensureCapacityInternal(count + len); sb.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); if (s instanceof StringBuffer) return this.append((StringBuffer)s); return this.append(s, 0, s.length()); } /** * Appends a subsequence of the specified {@code CharSequence} to this * sequence. * <p> * Characters of the argument {@code s}, starting at * index {@code start}, are appended, in order, to the contents of * this sequence up to the (exclusive) index {@code end}. The length * of this sequence is increased by the value of {@code end - start}. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} method. Then the character at * index <i>k</i> in this character sequence becomes equal to the * character at index <i>k</i> in this sequence, if <i>k</i> is less than * <i>n</i>; otherwise, it is equal to the character at index * <i>k+start-n</i> in the argument {@code s}. * <p> * If {@code s} is {@code null}, then this method appends * characters as if the s parameter was a sequence containing the four * characters {@code "null"}. * * @param s the sequence to append. * @param start the starting index of the subsequence to be appended. * @param end the end index of the subsequence to be appended. * @return a reference to this object. * @throws IndexOutOfBoundsException if * {@code start} is negative, or * {@code start} is greater than {@code end} or * {@code end} is greater than {@code s.length()} */ public AbstractStringBuilder append(CharSequence s, int start, int end) { if (s == null) s = "null"; if ((start < 0) || (start > end) || (end > s.length())) throw new IndexOutOfBoundsException( "start " + start + ", end " + end + ", s.length() " + s.length()); int len = end - start; ensureCapacityInternal(count + len); for (int i = start, j = count; i < end; i++, j++) value[j] = s.charAt(i); count += len; return this; } /** * Appends the string representation of the {@code char} array * argument to this sequence. * <p> * The characters of the array argument are appended, in order, to * the contents of this sequence. The length of this sequence * 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 character sequence. * * @param str the characters to be appended.Lines 1–500 of 1407. Load more
/* * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang; import sun.misc.FloatingDecimal; import java.util.Arrays; /** * A mutable sequence of characters. * <p> * Implements a modifiable string. At any point in time it contains some * particular sequence of characters, but the length and content of the * sequence can be changed through certain method calls. * * @author Michael McCloskey * @author Martin Buchholz * @author Ulf Zibis * @since 1.5 */ abstract class AbstractStringBuilder implements Appendable, CharSequence { /** * The value is used for character storage. */ char[] value; /** * The count is the number of characters used. */ int count; /** * This no-arg constructor is necessary for serialization of subclasses. */ AbstractStringBuilder() { } /** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; } /** * Returns the length (character count). * * @return the length of the sequence of characters currently * represented by this object */ public int length() { return count; } /** * Returns the current capacity. The capacity is the amount of storage * available for newly inserted characters, beyond which an allocation * will occur. * * @return the current capacity */ public int capacity() { return value.length; } /** * Ensures that the capacity is at least equal to the specified minimum. * If the current capacity is less than the argument, then a new internal * array is allocated with greater capacity. The new capacity is the * larger of: * <ul> * <li>The <code>minimumCapacity</code> argument. * <li>Twice the old capacity, plus <code>2</code>. * </ul> * If the <code>minimumCapacity</code> argument is nonpositive, this * method takes no action and simply returns. * * @param minimumCapacity the minimum desired capacity. */ public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > 0) ensureCapacityInternal(minimumCapacity); } /** * This method has the same contract as ensureCapacity, but is * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity); } /** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); } /** * Attempts to reduce storage used for the character sequence. * If the buffer is larger than necessary to hold its current sequence of * characters, then it may be resized to become more space efficient. * Calling this method may, but is not required to, affect the value * returned by a subsequent call to the {@link #capacity()} method. */ public void trimToSize() { if (count < value.length) { value = Arrays.copyOf(value, count); } } /** * Sets the length of the character sequence. * The sequence is changed to a new character sequence * whose length is specified by the argument. For every nonnegative * index <i>k</i> less than <code>newLength</code>, the character at * index <i>k</i> in the new character sequence is the same as the * character at index <i>k</i> in the old sequence if <i>k</i> is less * than the length of the old character sequence; otherwise, it is the * null character <code>'\u0000'</code>. * * In other words, if the <code>newLength</code> argument is less than * the current length, the length is changed to the specified length. * <p> * If the <code>newLength</code> argument is greater than or equal * to the current length, sufficient null characters * (<code>'\u0000'</code>) are appended so that * length becomes the <code>newLength</code> argument. * <p> * The <code>newLength</code> argument must be greater than or equal * to <code>0</code>. * * @param newLength the new length * @throws IndexOutOfBoundsException if the * <code>newLength</code> argument is negative. */ public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { for (; count < newLength; count++) value[count] = '\0'; } else { count = newLength; } } /** * Returns the <code>char</code> value in this sequence at the specified index. * The first <code>char</code> value is at index <code>0</code>, the next at index * <code>1</code>, and so on, as in array indexing. * <p> * The index argument must be greater than or equal to * <code>0</code>, and less than the length of this sequence. * * <p>If the <code>char</code> value specified by the index is a * <a href="Character.html#unicode">surrogate</a>, the surrogate * value is returned. * * @param index the index of the desired <code>char</code> value. * @return the <code>char</code> value at the specified index. * @throws IndexOutOfBoundsException if <code>index</code> is * negative or greater than or equal to <code>length()</code>. */ public char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; } /** * Returns the character (Unicode code point) at the specified * index. The index refers to <code>char</code> values * (Unicode code units) and ranges from <code>0</code> to * {@link #length()}<code> - 1</code>. * * <p> If the <code>char</code> value specified at the given index * is in the high-surrogate range, the following index is less * than the length of this sequence, and the * <code>char</code> value at the following index is in the * low-surrogate range, then the supplementary code point * corresponding to this surrogate pair is returned. Otherwise, * the <code>char</code> value at the given index is returned. * * @param index the index to the <code>char</code> values * @return the code point value of the character at the * <code>index</code> * @exception IndexOutOfBoundsException if the <code>index</code> * argument is negative or not less than the length of this * sequence. */ public int codePointAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointAt(value, index); } /** * Returns the character (Unicode code point) before the specified * index. The index refers to <code>char</code> values * (Unicode code units) and ranges from <code>1</code> to {@link * #length()}. * * <p> If the <code>char</code> value at <code>(index - 1)</code> * is in the low-surrogate range, <code>(index - 2)</code> is not * negative, and the <code>char</code> value at <code>(index - * 2)</code> is in the high-surrogate range, then the * supplementary code point value of the surrogate pair is * returned. If the <code>char</code> value at <code>index - * 1</code> is an unpaired low-surrogate or a high-surrogate, the * surrogate value is returned. * * @param index the index following the code point that should be returned * @return the Unicode code point value before the given index. * @exception IndexOutOfBoundsException if the <code>index</code> * argument is less than 1 or greater than the length * of this sequence. */ public int codePointBefore(int index) { int i = index - 1; if ((i < 0) || (i >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointBefore(value, index); } /** * Returns the number of Unicode code points in the specified text * range of this sequence. The text range begins at the specified * <code>beginIndex</code> and extends to the <code>char</code> at * index <code>endIndex - 1</code>. Thus the length (in * <code>char</code>s) of the text range is * <code>endIndex-beginIndex</code>. Unpaired surrogates within * this sequence count as one code point each. * * @param beginIndex the index to the first <code>char</code> of * the text range. * @param endIndex the index after the last <code>char</code> of * the text range. * @return the number of Unicode code points in the specified text * range * @exception IndexOutOfBoundsException if the * <code>beginIndex</code> is negative, or <code>endIndex</code> * is larger than the length of this sequence, or * <code>beginIndex</code> is larger than <code>endIndex</code>. */ public int codePointCount(int beginIndex, int endIndex) { if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { throw new IndexOutOfBoundsException(); } return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex); } /** * Returns the index within this sequence that is offset from the * given <code>index</code> by <code>codePointOffset</code> code * points. Unpaired surrogates within the text range given by * <code>index</code> and <code>codePointOffset</code> count as * one code point each. * * @param index the index to be offset * @param codePointOffset the offset in code points * @return the index within this sequence * @exception IndexOutOfBoundsException if <code>index</code> * is negative or larger then the length of this sequence, * or if <code>codePointOffset</code> is positive and the subsequence * starting with <code>index</code> has fewer than * <code>codePointOffset</code> code points, * or if <code>codePointOffset</code> is negative and the subsequence * before <code>index</code> has fewer than the absolute value of * <code>codePointOffset</code> code points. */ public int offsetByCodePoints(int index, int codePointOffset) { if (index < 0 || index > count) { throw new IndexOutOfBoundsException(); } return Character.offsetByCodePointsImpl(value, 0, count, index, codePointOffset); } /** * Characters are copied from this sequence 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. * @param srcEnd stop copying at this offset. * @param dst the array to copy the data into. * @param dstBegin offset into <code>dst</code>. * @throws NullPointerException if <code>dst</code> is * <code>null</code>. * @throws 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>. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than * <code>dst.length</code> * </ul> */ public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { if (srcBegin < 0) throw new StringIndexOutOfBoundsException(srcBegin); if ((srcEnd < 0) || (srcEnd > count)) throw new StringIndexOutOfBoundsException(srcEnd); if (srcBegin > srcEnd) throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); } /** * The character at the specified index is set to <code>ch</code>. This * sequence 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 sequence. * * @param index the index of the character to modify. * @param ch the new character. * @throws IndexOutOfBoundsException if <code>index</code> is * negative or greater than or equal to <code>length()</code>. */ public void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; } /** * Appends the string representation of the {@code Object} argument. * <p> * The overall effect is exactly as if the argument were converted * to a string by the method {@link String#valueOf(Object)}, * and the characters of that string were then * {@link #append(String) appended} to this character sequence. * * @param obj an {@code Object}. * @return a reference to this object. */ public AbstractStringBuilder append(Object obj) { return append(String.valueOf(obj)); } /** * Appends the specified string to this character sequence. * <p> * The characters of the {@code String} argument are appended, in * order, increasing the length of this sequence by the length of the * argument. If {@code str} is {@code null}, then the four * characters {@code "null"} are appended. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} 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}. * * @param str a string. * @return a reference to this object. */ public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(StringBuffer sb) { if (sb == null) return append("null"); int len = sb.length(); ensureCapacityInternal(count + len); sb.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); if (s instanceof StringBuffer) return this.append((StringBuffer)s); return this.append(s, 0, s.length()); } /** * Appends a subsequence of the specified {@code CharSequence} to this * sequence. * <p> * Characters of the argument {@code s}, starting at * index {@code start}, are appended, in order, to the contents of * this sequence up to the (exclusive) index {@code end}. The length * of this sequence is increased by the value of {@code end - start}. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} method. Then the character at * index <i>k</i> in this character sequence becomes equal to the * character at index <i>k</i> in this sequence, if <i>k</i> is less than * <i>n</i>; otherwise, it is equal to the character at index * <i>k+start-n</i> in the argument {@code s}. * <p> * If {@code s} is {@code null}, then this method appends * characters as if the s parameter was a sequence containing the four * characters {@code "null"}. * * @param s the sequence to append. * @param start the starting index of the subsequence to be appended. * @param end the end index of the subsequence to be appended. * @return a reference to this object. * @throws IndexOutOfBoundsException if * {@code start} is negative, or * {@code start} is greater than {@code end} or * {@code end} is greater than {@code s.length()} */ public AbstractStringBuilder append(CharSequence s, int start, int end) { if (s == null) s = "null"; if ((start < 0) || (start > end) || (end > s.length())) throw new IndexOutOfBoundsException( "start " + start + ", end " + end + ", s.length() " + s.length()); int len = end - start; ensureCapacityInternal(count + len); for (int i = start, j = count; i < end; i++, j++) value[j] = s.charAt(i); count += len; return this; } /** * Appends the string representation of the {@code char} array * argument to this sequence. * <p> * The characters of the array argument are appended, in order, to * the contents of this sequence. The length of this sequence * 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 character sequence. * * @param str the characters to be appended.