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