0001: /*
0002: *
0003: *
0004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: */
0026:
0027: package java.lang;
0028:
0029: import com.sun.cldchi.jvm.JVM;
0030:
0031: /**
0032: * A string buffer implements a mutable sequence of characters.
0033: * A string buffer is like a {@link String}, but can be modified. At any
0034: * point in time it contains some particular sequence of characters, but
0035: * the length and content of the sequence can be changed through certain
0036: * method calls.
0037: * <p>
0038: * String buffers are safe for use by multiple threads. The methods
0039: * are synchronized where necessary so that all the operations on any
0040: * particular instance behave as if they occur in some serial order
0041: * that is consistent with the order of the method calls made by each of
0042: * the individual threads involved.
0043: * <p>
0044: * String buffers are used by the compiler to implement the binary
0045: * string concatenation operator <code>+</code>. For example, the code:
0046: * <p><blockquote><pre>
0047: * x = "a" + 4 + "c"
0048: * </pre></blockquote><p>
0049: * is compiled to the equivalent of:
0050: * <p><blockquote><pre>
0051: * x = new StringBuffer().append("a").append(4).append("c")
0052: * .toString()
0053: * </pre></blockquote>
0054: * which creates a new string buffer (initially empty), appends the string
0055: * representation of each operand to the string buffer in turn, and then
0056: * converts the contents of the string buffer to a string. Overall, this avoids
0057: * creating many temporary strings.
0058: * <p>
0059: * The principal operations on a <code>StringBuffer</code> are the
0060: * <code>append</code> and <code>insert</code> methods, which are
0061: * overloaded so as to accept data of any type. Each effectively
0062: * converts a given datum to a string and then appends or inserts the
0063: * characters of that string to the string buffer. The
0064: * <code>append</code> method always adds these characters at the end
0065: * of the buffer; the <code>insert</code> method adds the characters at
0066: * a specified point.
0067: * <p>
0068: * For example, if <code>z</code> refers to a string buffer object
0069: * whose current contents are "<code>start</code>", then
0070: * the method call <code>z.append("le")</code> would cause the string
0071: * buffer to contain "<code>startle</code>", whereas
0072: * <code>z.insert(4, "le")</code> would alter the string buffer to
0073: * contain "<code>starlet</code>".
0074: * <p>
0075: * In general, if sb refers to an instance of a <code>StringBuffer</code>,
0076: * then <code>sb.append(x)</code> has the same effect as
0077: * <code>sb.insert(sb.length(), x)</code>.
0078: * <p>
0079: * Every string buffer has a capacity. As long as the length of the
0080: * character sequence contained in the string buffer does not exceed
0081: * the capacity, it is not necessary to allocate a new internal
0082: * buffer array. If the internal buffer overflows, it is
0083: * automatically made larger.
0084: *
0085: * @version 12/17/01 (CLDC 1.1)
0086: * @see java.io.ByteArrayOutputStream
0087: * @see java.lang.String
0088: * @since JDK1.0, CLDC 1.0
0089: */
0090:
0091: public final class StringBuffer {
0092:
0093: /**
0094: * The value is used for character storage.
0095: */
0096: private char value[];
0097:
0098: /**
0099: * The count is the number of characters in the buffer.
0100: */
0101: private int count;
0102:
0103: /**
0104: * A flag indicating whether the buffer is shared
0105: */
0106: private boolean shared;
0107:
0108: /**
0109: * Constructs a string buffer with no characters in it and an
0110: * initial capacity of 16 characters.
0111: */
0112: public StringBuffer() {
0113: value = new char[16];
0114: }
0115:
0116: /**
0117: * Constructs a string buffer with no characters in it and an
0118: * initial capacity specified by the <code>length</code> argument.
0119: *
0120: * @param length the initial capacity.
0121: * @exception NegativeArraySizeException if the <code>length</code>
0122: * argument is less than <code>0</code>.
0123: */
0124: public StringBuffer(int length) {
0125: value = new char[length];
0126: }
0127:
0128: /**
0129: * Constructs a string buffer so that it represents the same
0130: * sequence of characters as the string argument; in other
0131: * words, the initial contents of the string buffer is a copy of the
0132: * argument string. The initial capacity of the string buffer is
0133: * <code>16</code> plus the length of the string argument.
0134: *
0135: * @param str the initial contents of the buffer.
0136: */
0137: public StringBuffer(String str) {
0138: this (str.length() + 16);
0139: append(str);
0140: }
0141:
0142: /**
0143: * Returns the length (character count) of this string buffer.
0144: *
0145: * @return the length of the sequence of characters currently
0146: * represented by this string buffer.
0147: */
0148: public int length() {
0149: return count;
0150: }
0151:
0152: /**
0153: * Returns the current capacity of the String buffer. The capacity
0154: * is the amount of storage available for newly inserted
0155: * characters; beyond which an allocation will occur.
0156: *
0157: * @return the current capacity of this string buffer.
0158: */
0159: public int capacity() {
0160: return value.length;
0161: }
0162:
0163: /**
0164: * Copies the buffer value. This is normally only called when shared
0165: * is true. It should only be called from a synchronized method.
0166: */
0167: private final void copy() {
0168: char newValue[] = new char[value.length];
0169: JVM.unchecked_char_arraycopy(value, 0, newValue, 0, count);
0170: value = newValue;
0171: shared = false;
0172: }
0173:
0174: /**
0175: * Ensures that the capacity of the buffer is at least equal to the
0176: * specified minimum.
0177: * If the current capacity of this string buffer is less than the
0178: * argument, then a new internal buffer is allocated with greater
0179: * capacity. The new capacity is the larger of:
0180: * <ul>
0181: * <li>The <code>minimumCapacity</code> argument.
0182: * <li>Twice the old capacity, plus <code>2</code>.
0183: * </ul>
0184: * If the <code>minimumCapacity</code> argument is nonpositive, this
0185: * method takes no action and simply returns.
0186: *
0187: * @param minimumCapacity the minimum desired capacity.
0188: */
0189: public synchronized void ensureCapacity(int minimumCapacity) {
0190: if (minimumCapacity > value.length) {
0191: expandCapacity(minimumCapacity);
0192: }
0193: }
0194:
0195: /**
0196: * This implements the expansion semantics of ensureCapacity but is
0197: * unsynchronized for use internally by methods which are already
0198: * synchronized.
0199: *
0200: * @see java.lang.StringBuffer#ensureCapacity(int)
0201: */
0202: private void expandCapacity(int minimumCapacity) {
0203: int newCapacity = (value.length + 1) * 2;
0204: if (newCapacity < 0) {
0205: newCapacity = Integer.MAX_VALUE;
0206: } else if (minimumCapacity > newCapacity) {
0207: newCapacity = minimumCapacity;
0208: }
0209:
0210: char newValue[] = new char[newCapacity];
0211: JVM.unchecked_char_arraycopy(value, 0, newValue, 0, count);
0212: value = newValue;
0213: shared = false;
0214: }
0215:
0216: /**
0217: * Sets the length of this string buffer.
0218: * This string buffer is altered to represent a new character sequence
0219: * whose length is specified by the argument. For every nonnegative
0220: * index <i>k</i> less than <code>newLength</code>, the character at
0221: * index <i>k</i> in the new character sequence is the same as the
0222: * character at index <i>k</i> in the old sequence if <i>k</i> is less
0223: * than the length of the old character sequence; otherwise, it is the
0224: * null character <code>'\u0000'</code>.
0225: *
0226: * In other words, if the <code>newLength</code> argument is less than
0227: * the current length of the string buffer, the string buffer is
0228: * truncated to contain exactly the number of characters given by the
0229: * <code>newLength</code> argument.
0230: * <p>
0231: * If the <code>newLength</code> argument is greater than or equal
0232: * to the current length, sufficient null characters
0233: * (<code>'\u0000'</code>) are appended to the string buffer so that
0234: * length becomes the <code>newLength</code> argument.
0235: * <p>
0236: * The <code>newLength</code> argument must be greater than or equal
0237: * to <code>0</code>.
0238: *
0239: * @param newLength the new length of the buffer.
0240: * @exception IndexOutOfBoundsException if the
0241: * <code>newLength</code> argument is negative.
0242: * @see java.lang.StringBuffer#length()
0243: */
0244: public synchronized void setLength(int newLength) {
0245: if (newLength < 0) {
0246: throw new StringIndexOutOfBoundsException(
0247: /* #ifdef VERBOSE_EXCEPTIONS */
0248: /// skipped newLength
0249: /* #endif */
0250: );
0251: }
0252:
0253: if (newLength > value.length) {
0254: expandCapacity(newLength);
0255: }
0256:
0257: if (count < newLength) {
0258: if (shared)
0259: copy();
0260: for (; count < newLength; count++) {
0261: value[count] = '\0';
0262: }
0263: } else {
0264: count = newLength;
0265: if (shared) {
0266: if (newLength > 0) {
0267: copy();
0268: } else {
0269: // If newLength is zero, assume the StringBuffer is being
0270: // stripped for reuse; Make new buffer of default size
0271: value = new char[16];
0272: shared = false;
0273: }
0274: }
0275: }
0276: }
0277:
0278: /**
0279: * The specified character of the sequence currently represented by
0280: * the string buffer, as indicated by the <code>index</code> argument,
0281: * is returned. The first character of a string buffer is at index
0282: * <code>0</code>, the next at index <code>1</code>, and so on, for
0283: * array indexing.
0284: * <p>
0285: * The index argument must be greater than or equal to
0286: * <code>0</code>, and less than the length of this string buffer.
0287: *
0288: * @param index the index of the desired character.
0289: * @return the character at the specified index of this string buffer.
0290: * @exception IndexOutOfBoundsException if <code>index</code> is
0291: * negative or greater than or equal to <code>length()</code>.
0292: * @see java.lang.StringBuffer#length()
0293: */
0294: public synchronized char charAt(int index) {
0295: if ((index < 0) || (index >= count)) {
0296: throw new StringIndexOutOfBoundsException(
0297: /* #ifdef VERBOSE_EXCEPTIONS */
0298: /// skipped index
0299: /* #endif */
0300: );
0301: }
0302: return value[index];
0303: }
0304:
0305: /**
0306: * Characters are copied from this string buffer into the
0307: * destination character array <code>dst</code>. The first character to
0308: * be copied is at index <code>srcBegin</code>; the last character to
0309: * be copied is at index <code>srcEnd-1</code>. The total number of
0310: * characters to be copied is <code>srcEnd-srcBegin</code>. The
0311: * characters are copied into the subarray of <code>dst</code> starting
0312: * at index <code>dstBegin</code> and ending at index:
0313: * <p><blockquote><pre>
0314: * dstbegin + (srcEnd-srcBegin) - 1
0315: * </pre></blockquote>
0316: *
0317: * @param srcBegin start copying at this offset in the string buffer.
0318: * @param srcEnd stop copying at this offset in the string buffer.
0319: * @param dst the array to copy the data into.
0320: * @param dstBegin offset into <code>dst</code>.
0321: * @exception NullPointerException if <code>dst</code> is
0322: * <code>null</code>.
0323: * @exception IndexOutOfBoundsException if any of the following is true:
0324: * <ul>
0325: * <li><code>srcBegin</code> is negative
0326: * <li><code>dstBegin</code> is negative
0327: * <li>the <code>srcBegin</code> argument is greater than
0328: * the <code>srcEnd</code> argument.
0329: * <li><code>srcEnd</code> is greater than
0330: * <code>this.length()</code>, the current length of this
0331: * string buffer.
0332: * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
0333: * <code>dst.length</code>
0334: * </ul>
0335: */
0336: public synchronized void getChars(int srcBegin, int srcEnd,
0337: char dst[], int dstBegin) {
0338: if (srcBegin < 0) {
0339: throw new StringIndexOutOfBoundsException(
0340: /* #ifdef VERBOSE_EXCEPTIONS */
0341: /// skipped srcBegin
0342: /* #endif */
0343: );
0344: }
0345: if ((srcEnd < 0) || (srcEnd > count)) {
0346: throw new StringIndexOutOfBoundsException(
0347: /* #ifdef VERBOSE_EXCEPTIONS */
0348: /// skipped srcEnd
0349: /* #endif */
0350: );
0351: }
0352: if (srcBegin > srcEnd) {
0353: throw new StringIndexOutOfBoundsException(
0354: /* #ifdef VERBOSE_EXCEPTIONS */
0355: /// skipped "srcBegin > srcEnd"
0356: /* #endif */
0357: );
0358: }
0359: // NOTE: dst not checked, cannot use unchecked arraycopy
0360: System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd
0361: - srcBegin);
0362: }
0363:
0364: /**
0365: * The character at the specified index of this string buffer is set
0366: * to <code>ch</code>. The string buffer is altered to represent a new
0367: * character sequence that is identical to the old character sequence,
0368: * except that it contains the character <code>ch</code> at position
0369: * <code>index</code>.
0370: * <p>
0371: * The offset argument must be greater than or equal to
0372: * <code>0</code>, and less than the length of this string buffer.
0373: *
0374: * @param index the index of the character to modify.
0375: * @param ch the new character.
0376: * @exception IndexOutOfBoundsException if <code>index</code> is
0377: * negative or greater than or equal to <code>length()</code>.
0378: * @see java.lang.StringBuffer#length()
0379: */
0380: public synchronized void setCharAt(int index, char ch) {
0381: if ((index < 0) || (index >= count)) {
0382: throw new StringIndexOutOfBoundsException(
0383: /* #ifdef VERBOSE_EXCEPTIONS */
0384: /// skipped index
0385: /* #endif */
0386: );
0387: }
0388: if (shared)
0389: copy();
0390: value[index] = ch;
0391: }
0392:
0393: /**
0394: * Appends the string representation of the <code>Object</code>
0395: * argument to this string buffer.
0396: * <p>
0397: * The argument is converted to a string as if by the method
0398: * <code>String.valueOf</code>, and the characters of that
0399: * string are then appended to this string buffer.
0400: *
0401: * @param obj an <code>Object</code>.
0402: * @return a reference to this <code>StringBuffer</code> object.
0403: * @see java.lang.String#valueOf(java.lang.Object)
0404: * @see java.lang.StringBuffer#append(java.lang.String)
0405: */
0406: public synchronized StringBuffer append(Object obj) {
0407: return append(String.valueOf(obj));
0408: }
0409:
0410: /**
0411: * Appends the string to this string buffer.
0412: * <p>
0413: * The characters of the <code>String</code> argument are appended, in
0414: * order, to the contents of this string buffer, increasing the
0415: * length of this string buffer by the length of the argument.
0416: * If <code>str</code> is <code>null</code>, then the four characters
0417: * <code>"null"</code> are appended to this string buffer.
0418: * <p>
0419: * Let <i>n</i> be the length of the old character sequence, the one
0420: * contained in the string buffer just prior to execution of the
0421: * <code>append</code> method. Then the character at index <i>k</i> in
0422: * the new character sequence is equal to the character at index <i>k</i>
0423: * in the old character sequence, if <i>k</i> is less than <i>n</i>;
0424: * otherwise, it is equal to the character at index <i>k-n</i> in the
0425: * argument <code>str</code>.
0426: *
0427: * @param str a string.
0428: * @return a reference to this <code>StringBuffer</code>.
0429: */
0430: public synchronized StringBuffer append(String str) {
0431: if (str == null) {
0432: str = String.valueOf(str);
0433: }
0434:
0435: int len = str.length();
0436: int newcount = count + len;
0437: if (newcount > value.length)
0438: expandCapacity(newcount);
0439: str.getChars(0, len, value, count);
0440: count = newcount;
0441: return this ;
0442: }
0443:
0444: /**
0445: * Appends the string representation of the <code>char</code> array
0446: * argument to this string buffer.
0447: * <p>
0448: * The characters of the array argument are appended, in order, to
0449: * the contents of this string buffer. The length of this string
0450: * buffer increases by the length of the argument.
0451: * <p>
0452: * The overall effect is exactly as if the argument were converted to
0453: * a string by the method {@link String#valueOf(char[])} and the
0454: * characters of that string were then {@link #append(String) appended}
0455: * to this <code>StringBuffer</code> object.
0456: *
0457: * @param str the characters to be appended.
0458: * @return a reference to this <code>StringBuffer</code> object.
0459: */
0460: public synchronized StringBuffer append(char str[]) {
0461: int len = str.length;
0462: int newcount = count + len;
0463: if (newcount > value.length)
0464: expandCapacity(newcount);
0465: JVM.unchecked_char_arraycopy(str, 0, value, count, len);
0466: count = newcount;
0467: return this ;
0468: }
0469:
0470: /**
0471: * Appends the string representation of a subarray of the
0472: * <code>char</code> array argument to this string buffer.
0473: * <p>
0474: * Characters of the character array <code>str</code>, starting at
0475: * index <code>offset</code>, are appended, in order, to the contents
0476: * of this string buffer. The length of this string buffer increases
0477: * by the value of <code>len</code>.
0478: * <p>
0479: * The overall effect is exactly as if the arguments were converted to
0480: * a string by the method {@link String#valueOf(char[],int,int)} and the
0481: * characters of that string were then {@link #append(String) appended}
0482: * to this <code>StringBuffer</code> object.
0483: *
0484: * @param str the characters to be appended.
0485: * @param offset the index of the first character to append.
0486: * @param len the number of characters to append.
0487: * @return a reference to this <code>StringBuffer</code> object.
0488: */
0489: public synchronized StringBuffer append(char str[], int offset,
0490: int len) {
0491: int newcount = count + len;
0492: if (newcount > value.length)
0493: expandCapacity(newcount);
0494: // NOTE: str and offset not checked, cannot use unchecked arraycopy
0495: System.arraycopy(str, offset, value, count, len);
0496: count = newcount;
0497: return this ;
0498: }
0499:
0500: /**
0501: * Appends the string representation of the <code>boolean</code>
0502: * argument to the string buffer.
0503: * <p>
0504: * The argument is converted to a string as if by the method
0505: * <code>String.valueOf</code>, and the characters of that
0506: * string are then appended to this string buffer.
0507: *
0508: * @param b a <code>boolean</code>.
0509: * @return a reference to this <code>StringBuffer</code>.
0510: * @see java.lang.String#valueOf(boolean)
0511: * @see java.lang.StringBuffer#append(java.lang.String)
0512: */
0513: public StringBuffer append(boolean b) {
0514: return append(String.valueOf(b));
0515: }
0516:
0517: /**
0518: * Appends the string representation of the <code>char</code>
0519: * argument to this string buffer.
0520: * <p>
0521: * The argument is appended to the contents of this string buffer.
0522: * The length of this string buffer increases by <code>1</code>.
0523: * <p>
0524: * The overall effect is exactly as if the argument were converted to
0525: * a string by the method {@link String#valueOf(char)} and the character
0526: * in that string were then {@link #append(String) appended} to this
0527: * <code>StringBuffer</code> object.
0528: *
0529: * @param c a <code>char</code>.
0530: * @return a reference to this <code>StringBuffer</code> object.
0531: */
0532: public synchronized StringBuffer append(char c) {
0533: int newcount = count + 1;
0534: if (newcount > value.length)
0535: expandCapacity(newcount);
0536: value[count++] = c;
0537: return this ;
0538: }
0539:
0540: /**
0541: * Appends the string representation of the <code>int</code>
0542: * argument to this string buffer.
0543: * <p>
0544: * The argument is converted to a string as if by the method
0545: * <code>String.valueOf</code>, and the characters of that
0546: * string are then appended to this string buffer.
0547: *
0548: * @param i an <code>int</code>.
0549: * @return a reference to this <code>StringBuffer</code> object.
0550: * @see java.lang.String#valueOf(int)
0551: * @see java.lang.StringBuffer#append(java.lang.String)
0552: */
0553: public StringBuffer append(int i) {
0554: return append(String.valueOf(i));
0555: }
0556:
0557: /**
0558: * Appends the string representation of the <code>long</code>
0559: * argument to this string buffer.
0560: * <p>
0561: * The argument is converted to a string as if by the method
0562: * <code>String.valueOf</code>, and the characters of that
0563: * string are then appended to this string buffer.
0564: *
0565: * @param l a <code>long</code>.
0566: * @return a reference to this <code>StringBuffer</code> object.
0567: * @see java.lang.String#valueOf(long)
0568: * @see java.lang.StringBuffer#append(java.lang.String)
0569: */
0570: public StringBuffer append(long l) {
0571: return append(String.valueOf(l));
0572: }
0573:
0574: /**
0575: * Appends the string representation of the <code>float</code>
0576: * argument to this string buffer.
0577: * <p>
0578: * The argument is converted to a string as if by the method
0579: * <code>String.valueOf</code>, and the characters of that
0580: * string are then appended to this string buffer.
0581: *
0582: * @param f a <code>float</code>.
0583: * @return a reference to this <code>StringBuffer</code> object.
0584: * @see java.lang.String#valueOf(float)
0585: * @see java.lang.StringBuffer#append(java.lang.String)
0586: * @since CLDC 1.1
0587: */
0588: public StringBuffer append(float f) {
0589: return append(String.valueOf(f));
0590: }
0591:
0592: /**
0593: * Appends the string representation of the <code>double</code>
0594: * argument to this string buffer.
0595: * <p>
0596: * The argument is converted to a string as if by the method
0597: * <code>String.valueOf</code>, and the characters of that
0598: * string are then appended to this string buffer.
0599: *
0600: * @param d a <code>double</code>.
0601: * @return a reference to this <code>StringBuffer</code> object.
0602: * @see java.lang.String#valueOf(double)
0603: * @see java.lang.StringBuffer#append(java.lang.String)
0604: * @since CLDC 1.1
0605: */
0606: public StringBuffer append(double d) {
0607: return append(String.valueOf(d));
0608: }
0609:
0610: /**
0611: * Removes the characters in a substring of this <code>StringBuffer</code>.
0612: * The substring begins at the specified <code>start</code> and extends to
0613: * the character at index <code>end - 1</code> or to the end of the
0614: * <code>StringBuffer</code> if no such character exists. If
0615: * <code>start</code> is equal to <code>end</code>, no changes are made.
0616: *
0617: * @param start The beginning index, inclusive.
0618: * @param end The ending index, exclusive.
0619: * @return This string buffer.
0620: * @exception StringIndexOutOfBoundsException if <code>start</code>
0621: * is negative, greater than <code>length()</code>, or
0622: * greater than <code>end</code>.
0623: * @since JDK1.2
0624: */
0625: public synchronized StringBuffer delete(int start, int end) {
0626: if (start < 0)
0627: throw new StringIndexOutOfBoundsException(
0628: /* #ifdef VERBOSE_EXCEPTIONS */
0629: /// skipped start
0630: /* #endif */
0631: );
0632: if (end > count)
0633: end = count;
0634: if (start > end)
0635: throw new StringIndexOutOfBoundsException();
0636:
0637: int len = end - start;
0638: if (len > 0) {
0639: if (shared)
0640: copy();
0641: JVM.unchecked_char_arraycopy(value, start + len, value,
0642: start, count - end);
0643: count -= len;
0644: }
0645: return this ;
0646: }
0647:
0648: /**
0649: * Removes the character at the specified position in this
0650: * <code>StringBuffer</code> (shortening the <code>StringBuffer</code>
0651: * by one character).
0652: *
0653: * @param index Index of character to remove
0654: * @return This string buffer.
0655: * @exception StringIndexOutOfBoundsException if the <code>index</code>
0656: * is negative or greater than or equal to
0657: * <code>length()</code>.
0658: * @since JDK1.2
0659: */
0660: public synchronized StringBuffer deleteCharAt(int index) {
0661: if ((index < 0) || (index >= count))
0662: throw new StringIndexOutOfBoundsException();
0663: if (shared)
0664: copy();
0665: JVM.unchecked_char_arraycopy(value, index + 1, value, index,
0666: count - index - 1);
0667: count--;
0668: return this ;
0669: }
0670:
0671: /**
0672: * Inserts the string representation of the <code>Object</code>
0673: * argument into this string buffer.
0674: * <p>
0675: * The second argument is converted to a string as if by the method
0676: * <code>String.valueOf</code>, and the characters of that
0677: * string are then inserted into this string buffer at the indicated
0678: * offset.
0679: * <p>
0680: * The offset argument must be greater than or equal to
0681: * <code>0</code>, and less than or equal to the length of this
0682: * string buffer.
0683: *
0684: * @param offset the offset.
0685: * @param obj an <code>Object</code>.
0686: * @return a reference to this <code>StringBuffer</code> object.
0687: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0688: * @see java.lang.String#valueOf(java.lang.Object)
0689: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0690: * @see java.lang.StringBuffer#length()
0691: */
0692: public synchronized StringBuffer insert(int offset, Object obj) {
0693: return insert(offset, String.valueOf(obj));
0694: }
0695:
0696: /**
0697: * Inserts the string into this string buffer.
0698: * <p>
0699: * The characters of the <code>String</code> argument are inserted, in
0700: * order, into this string buffer at the indicated offset, moving up any
0701: * characters originally above that position and increasing the length
0702: * of this string buffer by the length of the argument. If
0703: * <code>str</code> is <code>null</code>, then the four characters
0704: * <code>"null"</code> are inserted into this string buffer.
0705: * <p>
0706: * The character at index <i>k</i> in the new character sequence is
0707: * equal to:
0708: * <ul>
0709: * <li>the character at index <i>k</i> in the old character sequence, if
0710: * <i>k</i> is less than <code>offset</code>
0711: * <li>the character at index <i>k</i><code>-offset</code> in the
0712: * argument <code>str</code>, if <i>k</i> is not less than
0713: * <code>offset</code> but is less than <code>offset+str.length()</code>
0714: * <li>the character at index <i>k</i><code>-str.length()</code> in the
0715: * old character sequence, if <i>k</i> is not less than
0716: * <code>offset+str.length()</code>
0717: * </ul><p>
0718: * The offset argument must be greater than or equal to
0719: * <code>0</code>, and less than or equal to the length of this
0720: * string buffer.
0721: *
0722: * @param offset the offset.
0723: * @param str a string.
0724: * @return a reference to this <code>StringBuffer</code> object.
0725: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0726: * @see java.lang.StringBuffer#length()
0727: */
0728: public synchronized StringBuffer insert(int offset, String str) {
0729: if ((offset < 0) || (offset > count)) {
0730: throw new StringIndexOutOfBoundsException();
0731: }
0732:
0733: if (str == null) {
0734: str = String.valueOf(str);
0735: }
0736: int len = str.length();
0737: int newcount = count + len;
0738: if (newcount > value.length)
0739: expandCapacity(newcount);
0740: else if (shared)
0741: copy();
0742: JVM.unchecked_char_arraycopy(value, offset, value,
0743: offset + len, count - offset);
0744: str.getChars(0, len, value, offset);
0745: count = newcount;
0746: return this ;
0747: }
0748:
0749: /**
0750: * Inserts the string representation of the <code>char</code> array
0751: * argument into this string buffer.
0752: * <p>
0753: * The characters of the array argument are inserted into the
0754: * contents of this string buffer at the position indicated by
0755: * <code>offset</code>. The length of this string buffer increases by
0756: * the length of the argument.
0757: * <p>
0758: * The overall effect is exactly as if the argument were converted to
0759: * a string by the method {@link String#valueOf(char[])} and the
0760: * characters of that string were then
0761: * {@link #insert(int,String) inserted} into this
0762: * <code>StringBuffer</code> object at the position indicated by
0763: * <code>offset</code>.
0764: *
0765: * @param offset the offset.
0766: * @param str a character array.
0767: * @return a reference to this <code>StringBuffer</code> object.
0768: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0769: */
0770: public synchronized StringBuffer insert(int offset, char str[]) {
0771: if ((offset < 0) || (offset > count)) {
0772: throw new StringIndexOutOfBoundsException();
0773: }
0774: int len = str.length;
0775: int newcount = count + len;
0776: if (newcount > value.length)
0777: expandCapacity(newcount);
0778: else if (shared)
0779: copy();
0780: JVM.unchecked_char_arraycopy(value, offset, value,
0781: offset + len, count - offset);
0782: JVM.unchecked_char_arraycopy(str, 0, value, offset, len);
0783: count = newcount;
0784: return this ;
0785: }
0786:
0787: /**
0788: * Inserts the string representation of the <code>boolean</code>
0789: * argument into this string buffer.
0790: * <p>
0791: * The second argument is converted to a string as if by the method
0792: * <code>String.valueOf</code>, and the characters of that
0793: * string are then inserted into this string buffer at the indicated
0794: * offset.
0795: * <p>
0796: * The offset argument must be greater than or equal to
0797: * <code>0</code>, and less than or equal to the length of this
0798: * string buffer.
0799: *
0800: * @param offset the offset.
0801: * @param b a <code>boolean</code>.
0802: * @return a reference to this <code>StringBuffer</code> object.
0803: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0804: * @see java.lang.String#valueOf(boolean)
0805: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0806: * @see java.lang.StringBuffer#length()
0807: */
0808: public StringBuffer insert(int offset, boolean b) {
0809: return insert(offset, String.valueOf(b));
0810: }
0811:
0812: /**
0813: * Inserts the string representation of the <code>char</code>
0814: * argument into this string buffer.
0815: * <p>
0816: * The second argument is inserted into the contents of this string
0817: * buffer at the position indicated by <code>offset</code>. The length
0818: * of this string buffer increases by one.
0819: * <p>
0820: * The overall effect is exactly as if the argument were converted to
0821: * a string by the method {@link String#valueOf(char)} and the character
0822: * in that string were then {@link #insert(int, String) inserted} into
0823: * this <code>StringBuffer</code> object at the position indicated by
0824: * <code>offset</code>.
0825: * <p>
0826: * The offset argument must be greater than or equal to
0827: * <code>0</code>, and less than or equal to the length of this
0828: * string buffer.
0829: *
0830: * @param offset the offset.
0831: * @param c a <code>char</code>.
0832: * @return a reference to this <code>StringBuffer</code> object.
0833: * @exception IndexOutOfBoundsException if the offset is invalid.
0834: * @see java.lang.StringBuffer#length()
0835: */
0836: public synchronized StringBuffer insert(int offset, char c) {
0837: int newcount = count + 1;
0838: if (newcount > value.length)
0839: expandCapacity(newcount);
0840: else if (shared)
0841: copy();
0842: // NOTE: offset not checked, cannot use unchecked arraycopy
0843: System.arraycopy(value, offset, value, offset + 1, count
0844: - offset);
0845: value[offset] = c;
0846: count = newcount;
0847: return this ;
0848: }
0849:
0850: /**
0851: * Inserts the string representation of the second <code>int</code>
0852: * argument into this string buffer.
0853: * <p>
0854: * The second argument is converted to a string as if by the method
0855: * <code>String.valueOf</code>, and the characters of that
0856: * string are then inserted into this string buffer at the indicated
0857: * offset.
0858: * <p>
0859: * The offset argument must be greater than or equal to
0860: * <code>0</code>, and less than or equal to the length of this
0861: * string buffer.
0862: *
0863: * @param offset the offset.
0864: * @param i an <code>int</code>.
0865: * @return a reference to this <code>StringBuffer</code> object.
0866: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0867: * @see java.lang.String#valueOf(int)
0868: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0869: * @see java.lang.StringBuffer#length()
0870: */
0871: public StringBuffer insert(int offset, int i) {
0872: return insert(offset, String.valueOf(i));
0873: }
0874:
0875: /**
0876: * Inserts the string representation of the <code>long</code>
0877: * argument into this string buffer.
0878: * <p>
0879: * The second argument is converted to a string as if by the method
0880: * <code>String.valueOf</code>, and the characters of that
0881: * string are then inserted into this string buffer at the position
0882: * indicated by <code>offset</code>.
0883: * <p>
0884: * The offset argument must be greater than or equal to
0885: * <code>0</code>, and less than or equal to the length of this
0886: * string buffer.
0887: *
0888: * @param offset the offset.
0889: * @param l a <code>long</code>.
0890: * @return a reference to this <code>StringBuffer</code> object.
0891: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0892: * @see java.lang.String#valueOf(long)
0893: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0894: * @see java.lang.StringBuffer#length()
0895: */
0896: public StringBuffer insert(int offset, long l) {
0897: return insert(offset, String.valueOf(l));
0898: }
0899:
0900: /**
0901: * Inserts the string representation of the <code>float</code>
0902: * argument into this string buffer.
0903: * <p>
0904: * The second argument is converted to a string as if by the method
0905: * <code>String.valueOf</code>, and the characters of that
0906: * string are then inserted into this string buffer at the indicated
0907: * offset.
0908: * <p>
0909: * The offset argument must be greater than or equal to
0910: * <code>0</code>, and less than or equal to the length of this
0911: * string buffer.
0912: *
0913: * @param offset the offset.
0914: * @param f a <code>float</code>.
0915: * @return a reference to this <code>StringBuffer</code> object.
0916: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0917: * @see java.lang.String#valueOf(float)
0918: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0919: * @see java.lang.StringBuffer#length()
0920: * @since CLDC 1.1
0921: */
0922: public StringBuffer insert(int offset, float f) {
0923: return insert(offset, String.valueOf(f));
0924: }
0925:
0926: /**
0927: * Inserts the string representation of the <code>double</code>
0928: * argument into this string buffer.
0929: * <p>
0930: * The second argument is converted to a string as if by the method
0931: * <code>String.valueOf</code>, and the characters of that
0932: * string are then inserted into this string buffer at the indicated
0933: * offset.
0934: * <p>
0935: * The offset argument must be greater than or equal to
0936: * <code>0</code>, and less than or equal to the length of this
0937: * string buffer.
0938: *
0939: * @param offset the offset.
0940: * @param d a <code>double</code>.
0941: * @return a reference to this <code>StringBuffer</code> object.
0942: * @exception StringIndexOutOfBoundsException if the offset is invalid.
0943: * @see java.lang.String#valueOf(double)
0944: * @see java.lang.StringBuffer#insert(int, java.lang.String)
0945: * @see java.lang.StringBuffer#length()
0946: * @since CLDC 1.1
0947: */
0948: public StringBuffer insert(int offset, double d) {
0949: return insert(offset, String.valueOf(d));
0950: }
0951:
0952: /**
0953: * The character sequence contained in this string buffer is
0954: * replaced by the reverse of the sequence.
0955: * <p>
0956: * Let <i>n</i> be the length of the old character sequence, the one
0957: * contained in the string buffer just prior to execution of the
0958: * <code>reverse</code> method. Then the character at index <i>k</i> in
0959: * the new character sequence is equal to the character at index
0960: * <i>n-k-1</i> in the old character sequence.
0961: *
0962: * @return a reference to this <code>StringBuffer</code> object..
0963: * @since JDK1.0.2
0964: */
0965: public synchronized StringBuffer reverse() {
0966: if (shared)
0967: copy();
0968: int n = count - 1;
0969: for (int j = (n - 1) >> 1; j >= 0; --j) {
0970: char temp = value[j];
0971: value[j] = value[n - j];
0972: value[n - j] = temp;
0973: }
0974: return this ;
0975: }
0976:
0977: /**
0978: * Converts to a string representing the data in this string buffer.
0979: * A new <code>String</code> object is allocated and initialized to
0980: * contain the character sequence currently represented by this
0981: * string buffer. This <code>String</code> is then returned. Subsequent
0982: * changes to the string buffer do not affect the contents of the
0983: * <code>String</code>.
0984: * <p>
0985: * Implementation advice: This method can be coded so as to create a new
0986: * <code>String</code> object without allocating new memory to hold a
0987: * copy of the character sequence. Instead, the string can share the
0988: * memory used by the string buffer. Any subsequent operation that alters
0989: * the content or capacity of the string buffer must then make a copy of
0990: * the internal buffer at that time. This strategy is effective for
0991: * reducing the amount of memory allocated by a string concatenation
0992: * operation when it is implemented using a string buffer.
0993: *
0994: * @return a string representation of the string buffer.
0995: */
0996: public String toString() {
0997: return new String(this );
0998: }
0999:
1000: // The following two methods are needed by String to efficiently
1001: // convert a StringBuffer into a String. They are not public.
1002: // They shouldn't be called by anyone but String.
1003: final void setShared() {
1004: shared = true;
1005: }
1006:
1007: final char[] getValue() {
1008: return value;
1009: }
1010:
1011: }
|