1. /*
  2. * @(#)StringBuffer.java 1.78 03/05/16
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * A string buffer implements a mutable sequence of characters.
  10. * A string buffer is like a {@link String}, but can be modified. At any
  11. * point in time it contains some particular sequence of characters, but
  12. * the length and content of the sequence can be changed through certain
  13. * method calls.
  14. * <p>
  15. * String buffers are safe for use by multiple threads. The methods
  16. * are synchronized where necessary so that all the operations on any
  17. * particular instance behave as if they occur in some serial order
  18. * that is consistent with the order of the method calls made by each of
  19. * the individual threads involved.
  20. * <p>
  21. * String buffers are used by the compiler to implement the binary
  22. * string concatenation operator <code>+</code>. For example, the code:
  23. * <p><blockquote><pre>
  24. * x = "a" + 4 + "c"
  25. * </pre></blockquote><p>
  26. * is compiled to the equivalent of:
  27. * <p><blockquote><pre>
  28. * x = new StringBuffer().append("a").append(4).append("c")
  29. * .toString()
  30. * </pre></blockquote>
  31. * which creates a new string buffer (initially empty), appends the string
  32. * representation of each operand to the string buffer in turn, and then
  33. * converts the contents of the string buffer to a string. Overall, this avoids
  34. * creating many temporary strings.
  35. * <p>
  36. * The principal operations on a <code>StringBuffer</code> are the
  37. * <code>append</code> and <code>insert</code> methods, which are
  38. * overloaded so as to accept data of any type. Each effectively
  39. * converts a given datum to a string and then appends or inserts the
  40. * characters of that string to the string buffer. The
  41. * <code>append</code> method always adds these characters at the end
  42. * of the buffer; the <code>insert</code> method adds the characters at
  43. * a specified point.
  44. * <p>
  45. * For example, if <code>z</code> refers to a string buffer object
  46. * whose current contents are "<code>start</code>", then
  47. * the method call <code>z.append("le")</code> would cause the string
  48. * buffer to contain "<code>startle</code>", whereas
  49. * <code>z.insert(4, "le")</code> would alter the string buffer to
  50. * contain "<code>starlet</code>".
  51. * <p>
  52. * In general, if sb refers to an instance of a <code>StringBuffer</code>,
  53. * then <code>sb.append(x)</code> has the same effect as
  54. * <code>sb.insert(sb.length(), x)</code>.
  55. * <p>
  56. * Every string buffer has a capacity. As long as the length of the
  57. * character sequence contained in the string buffer does not exceed
  58. * the capacity, it is not necessary to allocate a new internal
  59. * buffer array. If the internal buffer overflows, it is
  60. * automatically made larger.
  61. *
  62. * @author Arthur van Hoff
  63. * @version 1.78, 05/16/03
  64. * @see java.io.ByteArrayOutputStream
  65. * @see java.lang.String
  66. * @since JDK1.0
  67. */
  68. public final class StringBuffer
  69. implements java.io.Serializable, CharSequence
  70. {
  71. /**
  72. * The value is used for character storage.
  73. *
  74. * @serial
  75. */
  76. private char value[];
  77. /**
  78. * The count is the number of characters in the buffer.
  79. *
  80. * @serial
  81. */
  82. private int count;
  83. /**
  84. * A flag indicating whether the buffer is shared
  85. *
  86. * @serial
  87. */
  88. private boolean shared;
  89. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  90. static final long serialVersionUID = 3388685877147921107L;
  91. /**
  92. * Constructs a string buffer with no characters in it and an
  93. * initial capacity of 16 characters.
  94. */
  95. public StringBuffer() {
  96. this(16);
  97. }
  98. /**
  99. * Constructs a string buffer with no characters in it and an
  100. * initial capacity specified by the <code>length</code> argument.
  101. *
  102. * @param length the initial capacity.
  103. * @exception NegativeArraySizeException if the <code>length</code>
  104. * argument is less than <code>0</code>.
  105. */
  106. public StringBuffer(int length) {
  107. value = new char[length];
  108. shared = false;
  109. }
  110. /**
  111. * Constructs a string buffer so that it represents the same
  112. * sequence of characters as the string argument; in other
  113. * words, the initial contents of the string buffer is a copy of the
  114. * argument string. The initial capacity of the string buffer is
  115. * <code>16</code> plus the length of the string argument.
  116. *
  117. * @param str the initial contents of the buffer.
  118. * @exception NullPointerException if <code>str</code> is <code>null</code>
  119. */
  120. public StringBuffer(String str) {
  121. this(str.length() + 16);
  122. append(str);
  123. }
  124. /**
  125. * Returns the length (character count) of this string buffer.
  126. *
  127. * @return the length of the sequence of characters currently
  128. * represented by this string buffer.
  129. */
  130. public synchronized int length() {
  131. return count;
  132. }
  133. /**
  134. * Returns the current capacity of the String buffer. The capacity
  135. * is the amount of storage available for newly inserted
  136. * characters; beyond which an allocation will occur.
  137. *
  138. * @return the current capacity of this string buffer.
  139. */
  140. public synchronized int capacity() {
  141. return value.length;
  142. }
  143. /**
  144. * Copies the buffer value. This is normally only called when shared
  145. * is true. It should only be called from a synchronized method.
  146. */
  147. private final void copy() {
  148. char newValue[] = new char[value.length];
  149. System.arraycopy(value, 0, newValue, 0, count);
  150. value = newValue;
  151. shared = false;
  152. }
  153. /**
  154. * Ensures that the capacity of the buffer is at least equal to the
  155. * specified minimum.
  156. * If the current capacity of this string buffer is less than the
  157. * argument, then a new internal buffer is allocated with greater
  158. * capacity. The new capacity is the larger of:
  159. * <ul>
  160. * <li>The <code>minimumCapacity</code> argument.
  161. * <li>Twice the old capacity, plus <code>2</code>.
  162. * </ul>
  163. * If the <code>minimumCapacity</code> argument is nonpositive, this
  164. * method takes no action and simply returns.
  165. *
  166. * @param minimumCapacity the minimum desired capacity.
  167. */
  168. public synchronized void ensureCapacity(int minimumCapacity) {
  169. if (minimumCapacity > value.length) {
  170. expandCapacity(minimumCapacity);
  171. }
  172. }
  173. /**
  174. * This implements the expansion semantics of ensureCapacity but is
  175. * unsynchronized for use internally by methods which are already
  176. * synchronized.
  177. *
  178. * @see java.lang.StringBuffer#ensureCapacity(int)
  179. */
  180. private void expandCapacity(int minimumCapacity) {
  181. int newCapacity = (value.length + 1) * 2;
  182. if (newCapacity < 0) {
  183. newCapacity = Integer.MAX_VALUE;
  184. } else if (minimumCapacity > newCapacity) {
  185. newCapacity = minimumCapacity;
  186. }
  187. char newValue[] = new char[newCapacity];
  188. System.arraycopy(value, 0, newValue, 0, count);
  189. value = newValue;
  190. shared = false;
  191. }
  192. /**
  193. * Sets the length of this String buffer.
  194. * This string buffer is altered to represent a new character sequence
  195. * whose length is specified by the argument. For every nonnegative
  196. * index <i>k</i> less than <code>newLength</code>, the character at
  197. * index <i>k</i> in the new character sequence is the same as the
  198. * character at index <i>k</i> in the old sequence if <i>k</i> is less
  199. * than the length of the old character sequence; otherwise, it is the
  200. * null character <code>'\u0000'</code>.
  201. *
  202. * In other words, if the <code>newLength</code> argument is less than
  203. * the current length of the string buffer, the string buffer is
  204. * truncated to contain exactly the number of characters given by the
  205. * <code>newLength</code> argument.
  206. * <p>
  207. * If the <code>newLength</code> argument is greater than or equal
  208. * to the current length, sufficient null characters
  209. * (<code>'\u0000'</code>) are appended to the string buffer so that
  210. * length becomes the <code>newLength</code> argument.
  211. * <p>
  212. * The <code>newLength</code> argument must be greater than or equal
  213. * to <code>0</code>.
  214. *
  215. * @param newLength the new length of the buffer.
  216. * @exception IndexOutOfBoundsException if the
  217. * <code>newLength</code> argument is negative.
  218. * @see java.lang.StringBuffer#length()
  219. */
  220. public synchronized void setLength(int newLength) {
  221. if (newLength < 0) {
  222. throw new StringIndexOutOfBoundsException(newLength);
  223. }
  224. if (newLength > value.length) {
  225. expandCapacity(newLength);
  226. }
  227. if (count < newLength) {
  228. if (shared) copy();
  229. for (; count < newLength; count++) {
  230. value[count] = '\0';
  231. }
  232. } else {
  233. count = newLength;
  234. if (shared) {
  235. if (newLength > 0) {
  236. copy();
  237. } else {
  238. // If newLength is zero, assume the StringBuffer is being
  239. // stripped for reuse; Make new buffer of default size
  240. value = new char[16];
  241. shared = false;
  242. }
  243. }
  244. }
  245. }
  246. /**
  247. * The specified character of the sequence currently represented by
  248. * the string buffer, as indicated by the <code>index</code> argument,
  249. * is returned. The first character of a string buffer is at index
  250. * <code>0</code>, the next at index <code>1</code>, and so on, for
  251. * array indexing.
  252. * <p>
  253. * The index argument must be greater than or equal to
  254. * <code>0</code>, and less than the length of this string buffer.
  255. *
  256. * @param index the index of the desired character.
  257. * @return the character at the specified index of this string buffer.
  258. * @exception IndexOutOfBoundsException if <code>index</code> is
  259. * negative or greater than or equal to <code>length()</code>.
  260. * @see java.lang.StringBuffer#length()
  261. */
  262. public synchronized char charAt(int index) {
  263. if ((index < 0) || (index >= count)) {
  264. throw new StringIndexOutOfBoundsException(index);
  265. }
  266. return value[index];
  267. }
  268. /**
  269. * Characters are copied from this string buffer into the
  270. * destination character array <code>dst</code>. The first character to
  271. * be copied is at index <code>srcBegin</code> the last character to
  272. * be copied is at index <code>srcEnd-1</code>. The total number of
  273. * characters to be copied is <code>srcEnd-srcBegin</code>. The
  274. * characters are copied into the subarray of <code>dst</code> starting
  275. * at index <code>dstBegin</code> and ending at index:
  276. * <p><blockquote><pre>
  277. * dstbegin + (srcEnd-srcBegin) - 1
  278. * </pre></blockquote>
  279. *
  280. * @param srcBegin start copying at this offset in the string buffer.
  281. * @param srcEnd stop copying at this offset in the string buffer.
  282. * @param dst the array to copy the data into.
  283. * @param dstBegin offset into <code>dst</code>.
  284. * @exception NullPointerException if <code>dst</code> is
  285. * <code>null</code>.
  286. * @exception IndexOutOfBoundsException if any of the following is true:
  287. * <ul>
  288. * <li><code>srcBegin</code> is negative
  289. * <li><code>dstBegin</code> is negative
  290. * <li>the <code>srcBegin</code> argument is greater than
  291. * the <code>srcEnd</code> argument.
  292. * <li><code>srcEnd</code> is greater than
  293. * <code>this.length()</code>, the current length of this
  294. * string buffer.
  295. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
  296. * <code>dst.length</code>
  297. * </ul>
  298. */
  299. public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  300. if (srcBegin < 0) {
  301. throw new StringIndexOutOfBoundsException(srcBegin);
  302. }
  303. if ((srcEnd < 0) || (srcEnd > count)) {
  304. throw new StringIndexOutOfBoundsException(srcEnd);
  305. }
  306. if (srcBegin > srcEnd) {
  307. throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
  308. }
  309. System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  310. }
  311. /**
  312. * The character at the specified index of this string buffer is set
  313. * to <code>ch</code>. The string buffer is altered to represent a new
  314. * character sequence that is identical to the old character sequence,
  315. * except that it contains the character <code>ch</code> at position
  316. * <code>index</code>.
  317. * <p>
  318. * The index argument must be greater than or equal to
  319. * <code>0</code>, and less than the length of this string buffer.
  320. *
  321. * @param index the index of the character to modify.
  322. * @param ch the new character.
  323. * @exception IndexOutOfBoundsException if <code>index</code> is
  324. * negative or greater than or equal to <code>length()</code>.
  325. * @see java.lang.StringBuffer#length()
  326. */
  327. public synchronized void setCharAt(int index, char ch) {
  328. if ((index < 0) || (index >= count)) {
  329. throw new StringIndexOutOfBoundsException(index);
  330. }
  331. if (shared) copy();
  332. value[index] = ch;
  333. }
  334. /**
  335. * Appends the string representation of the <code>Object</code>
  336. * argument to this string buffer.
  337. * <p>
  338. * The argument is converted to a string as if by the method
  339. * <code>String.valueOf</code>, and the characters of that
  340. * string are then appended to this string buffer.
  341. *
  342. * @param obj an <code>Object</code>.
  343. * @return a reference to this <code>StringBuffer</code> object.
  344. * @see java.lang.String#valueOf(java.lang.Object)
  345. * @see java.lang.StringBuffer#append(java.lang.String)
  346. */
  347. public synchronized StringBuffer append(Object obj) {
  348. return append(String.valueOf(obj));
  349. }
  350. /**
  351. * Appends the string to this string buffer.
  352. * <p>
  353. * The characters of the <code>String</code> argument are appended, in
  354. * order, to the contents of this string buffer, increasing the
  355. * length of this string buffer by the length of the argument.
  356. * If <code>str</code> is <code>null</code>, then the four characters
  357. * <code>"null"</code> are appended to this string buffer.
  358. * <p>
  359. * Let <i>n</i> be the length of the old character sequence, the one
  360. * contained in the string buffer just prior to execution of the
  361. * <code>append</code> method. Then the character at index <i>k</i> in
  362. * the new character sequence is equal to the character at index <i>k</i>
  363. * in the old character sequence, if <i>k</i> is less than <i>n</i>
  364. * otherwise, it is equal to the character at index <i>k-n</i> in the
  365. * argument <code>str</code>.
  366. *
  367. * @param str a string.
  368. * @return a reference to this <code>StringBuffer</code>.
  369. */
  370. public synchronized StringBuffer append(String str) {
  371. if (str == null) {
  372. str = String.valueOf(str);
  373. }
  374. int len = str.length();
  375. int newcount = count + len;
  376. if (newcount > value.length)
  377. expandCapacity(newcount);
  378. str.getChars(0, len, value, count);
  379. count = newcount;
  380. return this;
  381. }
  382. /**
  383. * Appends the specified <tt>StringBuffer</tt> to this
  384. * <tt>StringBuffer</tt>.
  385. * <p>
  386. * The characters of the <tt>StringBuffer</tt> argument are appended,
  387. * in order, to the contents of this <tt>StringBuffer</tt>, increasing the
  388. * length of this <tt>StringBuffer</tt> by the length of the argument.
  389. * If <tt>sb</tt> is <tt>null</tt>, then the four characters
  390. * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
  391. * <p>
  392. * Let <i>n</i> be the length of the old character sequence, the one
  393. * contained in the <tt>StringBuffer</tt> just prior to execution of the
  394. * <tt>append</tt> method. Then the character at index <i>k</i> in
  395. * the new character sequence is equal to the character at index <i>k</i>
  396. * in the old character sequence, if <i>k</i> is less than <i>n</i>
  397. * otherwise, it is equal to the character at index <i>k-n</i> in the
  398. * argument <code>sb</code>.
  399. * <p>
  400. * The method <tt>ensureCapacity</tt> is first called on this
  401. * <tt>StringBuffer</tt> with the new buffer length as its argument.
  402. * (This ensures that the storage of this <tt>StringBuffer</tt> is
  403. * adequate to contain the additional characters being appended.)
  404. *
  405. * @param sb the <tt>StringBuffer</tt> to append.
  406. * @return a reference to this <tt>StringBuffer</tt>.
  407. * @since 1.4
  408. */
  409. public synchronized StringBuffer append(StringBuffer sb) {
  410. if (sb == null) {
  411. sb = NULL;
  412. }
  413. int len = sb.length();
  414. int newcount = count + len;
  415. if (newcount > value.length)
  416. expandCapacity(newcount);
  417. sb.getChars(0, len, value, count);
  418. count = newcount;
  419. return this;
  420. }
  421. private static final StringBuffer NULL = new StringBuffer("null");
  422. /**
  423. * Appends the string representation of the <code>char</code> array
  424. * argument to this string buffer.
  425. * <p>
  426. * The characters of the array argument are appended, in order, to
  427. * the contents of this string buffer. The length of this string
  428. * buffer increases by the length of the argument.
  429. * <p>
  430. * The overall effect is exactly as if the argument were converted to
  431. * a string by the method {@link String#valueOf(char[])} and the
  432. * characters of that string were then {@link #append(String) appended}
  433. * to this <code>StringBuffer</code> object.
  434. *
  435. * @param str the characters to be appended.
  436. * @return a reference to this <code>StringBuffer</code> object.
  437. */
  438. public synchronized StringBuffer append(char str[]) {
  439. int len = str.length;
  440. int newcount = count + len;
  441. if (newcount > value.length)
  442. expandCapacity(newcount);
  443. System.arraycopy(str, 0, value, count, len);
  444. count = newcount;
  445. return this;
  446. }
  447. /**
  448. * Appends the string representation of a subarray of the
  449. * <code>char</code> array argument to this string buffer.
  450. * <p>
  451. * Characters of the character array <code>str</code>, starting at
  452. * index <code>offset</code>, are appended, in order, to the contents
  453. * of this string buffer. The length of this string buffer increases
  454. * by the value of <code>len</code>.
  455. * <p>
  456. * The overall effect is exactly as if the arguments were converted to
  457. * a string by the method {@link String#valueOf(char[],int,int)} and the
  458. * characters of that string were then {@link #append(String) appended}
  459. * to this <code>StringBuffer</code> object.
  460. *
  461. * @param str the characters to be appended.
  462. * @param offset the index of the first character to append.
  463. * @param len the number of characters to append.
  464. * @return a reference to this <code>StringBuffer</code> object.
  465. */
  466. public synchronized StringBuffer append(char str[], int offset, int len) {
  467. int newcount = count + len;
  468. if (newcount > value.length)
  469. expandCapacity(newcount);
  470. System.arraycopy(str, offset, value, count, len);
  471. count = newcount;
  472. return this;
  473. }
  474. /**
  475. * Appends the string representation of the <code>boolean</code>
  476. * argument to the string buffer.
  477. * <p>
  478. * The argument is converted to a string as if by the method
  479. * <code>String.valueOf</code>, and the characters of that
  480. * string are then appended to this string buffer.
  481. *
  482. * @param b a <code>boolean</code>.
  483. * @return a reference to this <code>StringBuffer</code>.
  484. * @see java.lang.String#valueOf(boolean)
  485. * @see java.lang.StringBuffer#append(java.lang.String)
  486. */
  487. public synchronized StringBuffer append(boolean b) {
  488. if (b) {
  489. int newcount = count + 4;
  490. if (newcount > value.length)
  491. expandCapacity(newcount);
  492. value[count++] = 't';
  493. value[count++] = 'r';
  494. value[count++] = 'u';
  495. value[count++] = 'e';
  496. } else {
  497. int newcount = count + 5;
  498. if (newcount > value.length)
  499. expandCapacity(newcount);
  500. value[count++] = 'f';
  501. value[count++] = 'a';
  502. value[count++] = 'l';
  503. value[count++] = 's';
  504. value[count++] = 'e';
  505. }
  506. return this;
  507. }
  508. /**
  509. * Appends the string representation of the <code>char</code>
  510. * argument to this string buffer.
  511. * <p>
  512. * The argument is appended to the contents of this string buffer.
  513. * The length of this string buffer increases by <code>1</code>.
  514. * <p>
  515. * The overall effect is exactly as if the argument were converted to
  516. * a string by the method {@link String#valueOf(char)} and the character
  517. * in that string were then {@link #append(String) appended} to this
  518. * <code>StringBuffer</code> object.
  519. *
  520. * @param c a <code>char</code>.
  521. * @return a reference to this <code>StringBuffer</code> object.
  522. */
  523. public synchronized StringBuffer append(char c) {
  524. int newcount = count + 1;
  525. if (newcount > value.length)
  526. expandCapacity(newcount);
  527. value[count++] = c;
  528. return this;
  529. }
  530. /**
  531. * Appends the string representation of the <code>int</code>
  532. * argument to this string buffer.
  533. * <p>
  534. * The argument is converted to a string as if by the method
  535. * <code>String.valueOf</code>, and the characters of that
  536. * string are then appended to this string buffer.
  537. *
  538. * @param i an <code>int</code>.
  539. * @return a reference to this <code>StringBuffer</code> object.
  540. * @see java.lang.String#valueOf(int)
  541. * @see java.lang.StringBuffer#append(java.lang.String)
  542. */
  543. public synchronized StringBuffer append(int i) {
  544. Integer.appendTo(i, this);
  545. return this;
  546. }
  547. /**
  548. * Appends the string representation of the <code>long</code>
  549. * argument to this string buffer.
  550. * <p>
  551. * The argument is converted to a string as if by the method
  552. * <code>String.valueOf</code>, and the characters of that
  553. * string are then appended to this string buffer.
  554. *
  555. * @param l a <code>long</code>.
  556. * @return a reference to this <code>StringBuffer</code> object.
  557. * @see java.lang.String#valueOf(long)
  558. * @see java.lang.StringBuffer#append(java.lang.String)
  559. */
  560. public synchronized StringBuffer append(long l) {
  561. Long.appendTo(l, this);
  562. return this;
  563. }
  564. /**
  565. * Appends the string representation of the <code>float</code>
  566. * argument to this string buffer.
  567. * <p>
  568. * The argument is converted to a string as if by the method
  569. * <code>String.valueOf</code>, and the characters of that
  570. * string are then appended to this string buffer.
  571. *
  572. * @param f a <code>float</code>.
  573. * @return a reference to this <code>StringBuffer</code> object.
  574. * @see java.lang.String#valueOf(float)
  575. * @see java.lang.StringBuffer#append(java.lang.String)
  576. */
  577. public synchronized StringBuffer append(float f) {
  578. new FloatingDecimal(f).appendTo(this);
  579. return this;
  580. }
  581. /**
  582. * Appends the string representation of the <code>double</code>
  583. * argument to this string buffer.
  584. * <p>
  585. * The argument is converted to a string as if by the method
  586. * <code>String.valueOf</code>, and the characters of that
  587. * string are then appended to this string buffer.
  588. *
  589. * @param d a <code>double</code>.
  590. * @return a reference to this <code>StringBuffer</code> object.
  591. * @see java.lang.String#valueOf(double)
  592. * @see java.lang.StringBuffer#append(java.lang.String)
  593. */
  594. public synchronized StringBuffer append(double d) {
  595. new FloatingDecimal(d).appendTo(this);
  596. return this;
  597. }
  598. /**
  599. * Removes the characters in a substring of this <code>StringBuffer</code>.
  600. * The substring begins at the specified <code>start</code> and extends to
  601. * the character at index <code>end - 1</code> or to the end of the
  602. * <code>StringBuffer</code> if no such character exists. If
  603. * <code>start</code> is equal to <code>end</code>, no changes are made.
  604. *
  605. * @param start The beginning index, inclusive.
  606. * @param end The ending index, exclusive.
  607. * @return This string buffer.
  608. * @exception StringIndexOutOfBoundsException if <code>start</code>
  609. * is negative, greater than <code>length()</code>, or
  610. * greater than <code>end</code>.
  611. * @since 1.2
  612. */
  613. public synchronized StringBuffer delete(int start, int end) {
  614. if (start < 0)
  615. throw new StringIndexOutOfBoundsException(start);
  616. if (end > count)
  617. end = count;
  618. if (start > end)
  619. throw new StringIndexOutOfBoundsException();
  620. int len = end - start;
  621. if (len > 0) {
  622. if (shared)
  623. copy();
  624. System.arraycopy(value, start+len, value, start, count-end);
  625. count -= len;
  626. }
  627. return this;
  628. }
  629. /**
  630. * Removes the character at the specified position in this
  631. * <code>StringBuffer</code> (shortening the <code>StringBuffer</code>
  632. * by one character).
  633. *
  634. * @param index Index of character to remove
  635. * @return This string buffer.
  636. * @exception StringIndexOutOfBoundsException if the <code>index</code>
  637. * is negative or greater than or equal to
  638. * <code>length()</code>.
  639. * @since 1.2
  640. */
  641. public synchronized StringBuffer deleteCharAt(int index) {
  642. if ((index < 0) || (index >= count))
  643. throw new StringIndexOutOfBoundsException();
  644. if (shared)
  645. copy();
  646. System.arraycopy(value, index+1, value, index, count-index-1);
  647. count--;
  648. return this;
  649. }
  650. /**
  651. * Replaces the characters in a substring of this <code>StringBuffer</code>
  652. * with characters in the specified <code>String</code>. The substring
  653. * begins at the specified <code>start</code> and extends to the character
  654. * at index <code>end - 1</code> or to the end of the
  655. * <code>StringBuffer</code> if no such character exists. First the
  656. * characters in the substring are removed and then the specified
  657. * <code>String</code> is inserted at <code>start</code>. (The
  658. * <code>StringBuffer</code> will be lengthened to accommodate the
  659. * specified String if necessary.)
  660. *
  661. * @param start The beginning index, inclusive.
  662. * @param end The ending index, exclusive.
  663. * @param str String that will replace previous contents.
  664. * @return This string buffer.
  665. * @exception StringIndexOutOfBoundsException if <code>start</code>
  666. * is negative, greater than <code>length()</code>, or
  667. * greater than <code>end</code>.
  668. * @since 1.2
  669. */
  670. public synchronized StringBuffer replace(int start, int end, String str) {
  671. if (start < 0)
  672. throw new StringIndexOutOfBoundsException(start);
  673. if (end > count)
  674. end = count;
  675. if (start > end)
  676. throw new StringIndexOutOfBoundsException();
  677. int len = str.length();
  678. int newCount = count + len - (end - start);
  679. if (newCount > value.length)
  680. expandCapacity(newCount);
  681. else if (shared)
  682. copy();
  683. System.arraycopy(value, end, value, start + len, count - end);
  684. str.getChars(0, len, value, start);
  685. count = newCount;
  686. return this;
  687. }
  688. /**
  689. * Returns a new <code>String</code> that contains a subsequence of
  690. * characters currently contained in this <code>StringBuffer</code>.The
  691. * substring begins at the specified index and extends to the end of the
  692. * <code>StringBuffer</code>.
  693. *
  694. * @param start The beginning index, inclusive.
  695. * @return The new string.
  696. * @exception StringIndexOutOfBoundsException if <code>start</code> is
  697. * less than zero, or greater than the length of this
  698. * <code>StringBuffer</code>.
  699. * @since 1.2
  700. */
  701. public synchronized String substring(int start) {
  702. return substring(start, count);
  703. }
  704. /**
  705. * Returns a new character sequence that is a subsequence of this sequence.
  706. *
  707. * <p> An invocation of this method of the form
  708. *
  709. * <blockquote><pre>
  710. * sb.subSequence(begin, end)</pre></blockquote>
  711. *
  712. * behaves in exactly the same way as the invocation
  713. *
  714. * <blockquote><pre>
  715. * sb.substring(begin, end)</pre></blockquote>
  716. *
  717. * This method is provided so that the <tt>StringBuffer</tt> class can
  718. * implement the {@link CharSequence} interface. </p>
  719. *
  720. * @param start the start index, inclusive.
  721. * @param end the end index, exclusive.
  722. * @return the specified subsequence.
  723. *
  724. * @throws IndexOutOfBoundsException
  725. * if <tt>start</tt> or <tt>end</tt> are negative,
  726. * if <tt>end</tt> is greater than <tt>length()</tt>,
  727. * or if <tt>start</tt> is greater than <tt>end</tt>
  728. *
  729. * @since 1.4
  730. * @spec JSR-51
  731. */
  732. public CharSequence subSequence(int start, int end) {
  733. return this.substring(start, end);
  734. }
  735. /**
  736. * Returns a new <code>String</code> that contains a subsequence of
  737. * characters currently contained in this <code>StringBuffer</code>. The
  738. * substring begins at the specified <code>start</code> and
  739. * extends to the character at index <code>end - 1</code>. An
  740. * exception is thrown if
  741. *
  742. * @param start The beginning index, inclusive.
  743. * @param end The ending index, exclusive.
  744. * @return The new string.
  745. * @exception StringIndexOutOfBoundsException if <code>start</code>
  746. * or <code>end</code> are negative or greater than
  747. * <code>length()</code>, or <code>start</code> is
  748. * greater than <code>end</code>.
  749. * @since 1.2
  750. */
  751. public synchronized String substring(int start, int end) {
  752. if (start < 0)
  753. throw new StringIndexOutOfBoundsException(start);
  754. if (end > count)
  755. throw new StringIndexOutOfBoundsException(end);
  756. if (start > end)
  757. throw new StringIndexOutOfBoundsException(end - start);
  758. return new String(value, start, end - start);
  759. }
  760. /**
  761. * Inserts the string representation of a subarray of the <code>str</code>
  762. * array argument into this string buffer. The subarray begins at the
  763. * specified <code>offset</code> and extends <code>len</code> characters.
  764. * The characters of the subarray are inserted into this string buffer at
  765. * the position indicated by <code>index</code>. The length of this
  766. * <code>StringBuffer</code> increases by <code>len</code> characters.
  767. *
  768. * @param index position at which to insert subarray.
  769. * @param str A character array.
  770. * @param offset the index of the first character in subarray to
  771. * to be inserted.
  772. * @param len the number of characters in the subarray to
  773. * to be inserted.
  774. * @return This string buffer.
  775. * @exception StringIndexOutOfBoundsException if <code>index</code>
  776. * is negative or greater than <code>length()</code>, or
  777. * <code>offset</code> or <code>len</code> are negative, or
  778. * <code>(offset+len)</code> is greater than
  779. * <code>str.length</code>.
  780. * @since 1.2
  781. */
  782. public synchronized StringBuffer insert(int index, char str[], int offset,
  783. int len) {
  784. if ((index < 0) || (index > count))
  785. throw new StringIndexOutOfBoundsException();
  786. if ((offset < 0) || (offset + len < 0) || (offset + len > str.length))
  787. throw new StringIndexOutOfBoundsException(offset);
  788. if (len < 0)
  789. throw new StringIndexOutOfBoundsException(len);
  790. int newCount = count + len;
  791. if (newCount > value.length)
  792. expandCapacity(newCount);
  793. else if (shared)
  794. copy();
  795. System.arraycopy(value, index, value, index + len, count - index);
  796. System.arraycopy(str, offset, value, index, len);
  797. count = newCount;
  798. return this;
  799. }
  800. /**
  801. * Inserts the string representation of the <code>Object</code>
  802. * argument into this string buffer.
  803. * <p>
  804. * The second argument is converted to a string as if by the method
  805. * <code>String.valueOf</code>, and the characters of that
  806. * string are then inserted into this string buffer at the indicated
  807. * offset.
  808. * <p>
  809. * The offset argument must be greater than or equal to
  810. * <code>0</code>, and less than or equal to the length of this
  811. * string buffer.
  812. *
  813. * @param offset the offset.
  814. * @param obj an <code>Object</code>.
  815. * @return a reference to this <code>StringBuffer</code> object.
  816. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  817. * @see java.lang.String#valueOf(java.lang.Object)
  818. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  819. * @see java.lang.StringBuffer#length()
  820. */
  821. public synchronized StringBuffer insert(int offset, Object obj) {
  822. return insert(offset, String.valueOf(obj));
  823. }
  824. /**
  825. * Inserts the string into this string buffer.
  826. * <p>
  827. * The characters of the <code>String</code> argument are inserted, in
  828. * order, into this string buffer at the indicated offset, moving up any
  829. * characters originally above that position and increasing the length
  830. * of this string buffer by the length of the argument. If
  831. * <code>str</code> is <code>null</code>, then the four characters
  832. * <code>"null"</code> are inserted into this string buffer.
  833. * <p>
  834. * The character at index <i>k</i> in the new character sequence is
  835. * equal to:
  836. * <ul>
  837. * <li>the character at index <i>k</i> in the old character sequence, if
  838. * <i>k</i> is less than <code>offset</code>
  839. * <li>the character at index <i>k</i><code>-offset</code> in the
  840. * argument <code>str</code>, if <i>k</i> is not less than
  841. * <code>offset</code> but is less than <code>offset+str.length()</code>
  842. * <li>the character at index <i>k</i><code>-str.length()</code> in the
  843. * old character sequence, if <i>k</i> is not less than
  844. * <code>offset+str.length()</code>
  845. * </ul><p>
  846. * The offset argument must be greater than or equal to
  847. * <code>0</code>, and less than or equal to the length of this
  848. * string buffer.
  849. *
  850. * @param offset the offset.
  851. * @param str a string.
  852. * @return a reference to this <code>StringBuffer</code> object.
  853. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  854. * @see java.lang.StringBuffer#length()
  855. */
  856. public synchronized StringBuffer insert(int offset, String str) {
  857. if ((offset < 0) || (offset > count)) {
  858. throw new StringIndexOutOfBoundsException();
  859. }
  860. if (str == null) {
  861. str = String.valueOf(str);
  862. }
  863. int len = str.length();
  864. int newcount = count + len;
  865. if (newcount > value.length)
  866. expandCapacity(newcount);
  867. else if (shared)
  868. copy();
  869. System.arraycopy(value, offset, value, offset + len, count - offset);
  870. str.getChars(0, len, value, offset);
  871. count = newcount;
  872. return this;
  873. }
  874. /**
  875. * Inserts the string representation of the <code>char</code> array
  876. * argument into this string buffer.
  877. * <p>
  878. * The characters of the array argument are inserted into the
  879. * contents of this string buffer at the position indicated by
  880. * <code>offset</code>. The length of this string buffer increases by
  881. * the length of the argument.
  882. * <p>
  883. * The overall effect is exactly as if the argument were converted to
  884. * a string by the method {@link String#valueOf(char[])} and the
  885. * characters of that string were then
  886. * {@link #insert(int,String) inserted} into this
  887. * <code>StringBuffer</code> object at the position indicated by
  888. * <code>offset</code>.
  889. *
  890. * @param offset the offset.
  891. * @param str a character array.
  892. * @return a reference to this <code>StringBuffer</code> object.
  893. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  894. */
  895. public synchronized StringBuffer insert(int offset, char str[]) {
  896. if ((offset < 0) || (offset > count)) {
  897. throw new StringIndexOutOfBoundsException();
  898. }
  899. int len = str.length;
  900. int newcount = count + len;
  901. if (newcount > value.length)
  902. expandCapacity(newcount);
  903. else if (shared)
  904. copy();
  905. System.arraycopy(value, offset, value, offset + len, count - offset);
  906. System.arraycopy(str, 0, value, offset, len);
  907. count = newcount;
  908. return this;
  909. }
  910. /**
  911. * Inserts the string representation of the <code>boolean</code>
  912. * argument into this string buffer.
  913. * <p>
  914. * The second argument is converted to a string as if by the method
  915. * <code>String.valueOf</code>, and the characters of that
  916. * string are then inserted into this string buffer at the indicated
  917. * offset.
  918. * <p>
  919. * The offset argument must be greater than or equal to
  920. * <code>0</code>, and less than or equal to the length of this
  921. * string buffer.
  922. *
  923. * @param offset the offset.
  924. * @param b a <code>boolean</code>.
  925. * @return a reference to this <code>StringBuffer</code> object.
  926. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  927. * @see java.lang.String#valueOf(boolean)
  928. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  929. * @see java.lang.StringBuffer#length()
  930. */
  931. public StringBuffer insert(int offset, boolean b) {
  932. return insert(offset, String.valueOf(b));
  933. }
  934. /**
  935. * Inserts the string representation of the <code>char</code>
  936. * argument into this string buffer.
  937. * <p>
  938. * The second argument is inserted into the contents of this string
  939. * buffer at the position indicated by <code>offset</code>. The length
  940. * of this string buffer increases by one.
  941. * <p>
  942. * The overall effect is exactly as if the argument were converted to
  943. * a string by the method {@link String#valueOf(char)} and the character
  944. * in that string were then {@link #insert(int, String) inserted} into
  945. * this <code>StringBuffer</code> object at the position indicated by
  946. * <code>offset</code>.
  947. * <p>
  948. * The offset argument must be greater than or equal to
  949. * <code>0</code>, and less than or equal to the length of this
  950. * string buffer.
  951. *
  952. * @param offset the offset.
  953. * @param c a <code>char</code>.
  954. * @return a reference to this <code>StringBuffer</code> object.
  955. * @exception IndexOutOfBoundsException if the offset is invalid.
  956. * @see java.lang.StringBuffer#length()
  957. */
  958. public synchronized StringBuffer insert(int offset, char c) {
  959. int newcount = count + 1;
  960. if (newcount > value.length)
  961. expandCapacity(newcount);
  962. else if (shared)
  963. copy();
  964. System.arraycopy(value, offset, value, offset + 1, count - offset);
  965. value[offset] = c;
  966. count = newcount;
  967. return this;
  968. }
  969. /**
  970. * Inserts the string representation of the second <code>int</code>
  971. * argument into this string buffer.
  972. * <p>
  973. * The second argument is converted to a string as if by the method
  974. * <code>String.valueOf</code>, and the characters of that
  975. * string are then inserted into this string buffer at the indicated
  976. * offset.
  977. * <p>
  978. * The offset argument must be greater than or equal to
  979. * <code>0</code>, and less than or equal to the length of this
  980. * string buffer.
  981. *
  982. * @param offset the offset.
  983. * @param i an <code>int</code>.
  984. * @return a reference to this <code>StringBuffer</code> object.
  985. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  986. * @see java.lang.String#valueOf(int)
  987. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  988. * @see java.lang.StringBuffer#length()
  989. */
  990. public StringBuffer insert(int offset, int i) {
  991. return insert(offset, String.valueOf(i));
  992. }
  993. /**
  994. * Inserts the string representation of the <code>long</code>
  995. * argument into this string buffer.
  996. * <p>
  997. * The second argument is converted to a string as if by the method
  998. * <code>String.valueOf</code>, and the characters of that
  999. * string are then inserted into this string buffer at the position
  1000. * indicated by <code>offset</code>.
  1001. * <p>
  1002. * The offset argument must be greater than or equal to
  1003. * <code>0</code>, and less than or equal to the length of this
  1004. * string buffer.
  1005. *
  1006. * @param offset the offset.
  1007. * @param l a <code>long</code>.
  1008. * @return a reference to this <code>StringBuffer</code> object.
  1009. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  1010. * @see java.lang.String#valueOf(long)
  1011. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  1012. * @see java.lang.StringBuffer#length()
  1013. */
  1014. public StringBuffer insert(int offset, long l) {
  1015. return insert(offset, String.valueOf(l));
  1016. }
  1017. /**
  1018. * Inserts the string representation of the <code>float</code>
  1019. * argument into this string buffer.
  1020. * <p>
  1021. * The second argument is converted to a string as if by the method
  1022. * <code>String.valueOf</code>, and the characters of that
  1023. * string are then inserted into this string buffer at the indicated
  1024. * offset.
  1025. * <p>
  1026. * The offset argument must be greater than or equal to
  1027. * <code>0</code>, and less than or equal to the length of this
  1028. * string buffer.
  1029. *
  1030. * @param offset the offset.
  1031. * @param f a <code>float</code>.
  1032. * @return a reference to this <code>StringBuffer</code> object.
  1033. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  1034. * @see java.lang.String#valueOf(float)
  1035. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  1036. * @see java.lang.StringBuffer#length()
  1037. */
  1038. public StringBuffer insert(int offset, float f) {
  1039. return insert(offset, String.valueOf(f));
  1040. }
  1041. /**
  1042. * Inserts the string representation of the <code>double</code>
  1043. * argument into this string buffer.
  1044. * <p>
  1045. * The second argument is converted to a string as if by the method
  1046. * <code>String.valueOf</code>, and the characters of that
  1047. * string are then inserted into this string buffer at the indicated
  1048. * offset.
  1049. * <p>
  1050. * The offset argument must be greater than or equal to
  1051. * <code>0</code>, and less than or equal to the length of this
  1052. * string buffer.
  1053. *
  1054. * @param offset the offset.
  1055. * @param d a <code>double</code>.
  1056. * @return a reference to this <code>StringBuffer</code> object.
  1057. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  1058. * @see java.lang.String#valueOf(double)
  1059. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  1060. * @see java.lang.StringBuffer#length()
  1061. */
  1062. public StringBuffer insert(int offset, double d) {
  1063. return insert(offset, String.valueOf(d));
  1064. }
  1065. /**
  1066. * Returns the index within this string of the first occurrence of the
  1067. * specified substring. The integer returned is the smallest value
  1068. * <i>k</i> such that:
  1069. * <blockquote><pre>
  1070. * this.toString().startsWith(str, <i>k</i>)
  1071. * </pre></blockquote>
  1072. * is <code>true</code>.
  1073. *
  1074. * @param str any string.
  1075. * @return if the string argument occurs as a substring within this
  1076. * object, then the index of the first character of the first
  1077. * such substring is returned; if it does not occur as a
  1078. * substring, <code>-1</code> is returned.
  1079. * @exception java.lang.NullPointerException if <code>str</code> is
  1080. * <code>null</code>.
  1081. * @since 1.4
  1082. */
  1083. public int indexOf(String str) {
  1084. return indexOf(str, 0);
  1085. }
  1086. /**
  1087. * Returns the index within this string of the first occurrence of the
  1088. * specified substring, starting at the specified index. The integer
  1089. * returned is the smallest value <tt>k</tt> for which:
  1090. * <blockquote><pre>
  1091. * k >= Math.min(fromIndex, str.length()) &&
  1092. * this.toString().startsWith(str, k)
  1093. * </pre></blockquote>
  1094. * If no such value of <i>k</i> exists, then -1 is returned.
  1095. *
  1096. * @param str the substring for which to search.
  1097. * @param fromIndex the index from which to start the search.
  1098. * @return the index within this string of the first occurrence of the
  1099. * specified substring, starting at the specified index.
  1100. * @exception java.lang.NullPointerException if <code>str</code> is
  1101. * <code>null</code>.
  1102. * @since 1.4
  1103. */
  1104. public synchronized int indexOf(String str, int fromIndex) {
  1105. return String.indexOf(value, 0, count,
  1106. str.toCharArray(), 0, str.length(), fromIndex);
  1107. }
  1108. /**
  1109. * Returns the index within this string of the rightmost occurrence
  1110. * of the specified substring. The rightmost empty string "" is
  1111. * considered to occur at the index value <code>this.length()</code>.
  1112. * The returned index is the largest value <i>k</i> such that
  1113. * <blockquote><pre>
  1114. * this.toString().startsWith(str, k)
  1115. * </pre></blockquote>
  1116. * is true.
  1117. *
  1118. * @param str the substring to search for.
  1119. * @return if the string argument occurs one or more times as a substring
  1120. * within this object, then the index of the first character of
  1121. * the last such substring is returned. If it does not occur as
  1122. * a substring, <code>-1</code> is returned.
  1123. * @exception java.lang.NullPointerException if <code>str</code> is
  1124. * <code>null</code>.
  1125. * @since 1.4
  1126. */
  1127. public synchronized int lastIndexOf(String str) {
  1128. return lastIndexOf(str, count);
  1129. }
  1130. /**
  1131. * Returns the index within this string of the last occurrence of the
  1132. * specified substring. The integer returned is the largest value <i>k</i>
  1133. * such that:
  1134. * <blockquote><pre>
  1135. * k <= Math.min(fromIndex, str.length()) &&
  1136. * this.toString().startsWith(str, k)
  1137. * </pre></blockquote>
  1138. * If no such value of <i>k</i> exists, then -1 is returned.
  1139. *
  1140. * @param str the substring to search for.
  1141. * @param fromIndex the index to start the search from.
  1142. * @return the index within this string of the last occurrence of the
  1143. * specified substring.
  1144. * @exception java.lang.NullPointerException if <code>str</code> is
  1145. * <code>null</code>.
  1146. * @since 1.4
  1147. */
  1148. public synchronized int lastIndexOf(String str, int fromIndex) {
  1149. return String.lastIndexOf(value, 0, count,
  1150. str.toCharArray(), 0, str.length(), fromIndex);
  1151. }
  1152. /**
  1153. * The character sequence contained in this string buffer is
  1154. * replaced by the reverse of the sequence.
  1155. * <p>
  1156. * Let <i>n</i> be the length of the old character sequence, the one
  1157. * contained in the string buffer just prior to execution of the
  1158. * <code>reverse</code> method. Then the character at index <i>k</i> in
  1159. * the new character sequence is equal to the character at index
  1160. * <i>n-k-1</i> in the old character sequence.
  1161. *
  1162. * @return a reference to this <code>StringBuffer</code> object.
  1163. * @since JDK1.0.2
  1164. */
  1165. public synchronized StringBuffer reverse() {
  1166. if (shared) copy();
  1167. int n = count - 1;
  1168. for (int j = (n-1) >> 1; j >= 0; --j) {
  1169. char temp = value[j];
  1170. value[j] = value[n - j];
  1171. value[n - j] = temp;
  1172. }
  1173. return this;
  1174. }
  1175. /**
  1176. * Converts to a string representing the data in this string buffer.
  1177. * A new <code>String</code> object is allocated and initialized to
  1178. * contain the character sequence currently represented by this
  1179. * string buffer. This <code>String</code> is then returned. Subsequent
  1180. * changes to the string buffer do not affect the contents of the
  1181. * <code>String</code>.
  1182. * <p>
  1183. * Implementation advice: This method can be coded so as to create a new
  1184. * <code>String</code> object without allocating new memory to hold a
  1185. * copy of the character sequence. Instead, the string can share the
  1186. * memory used by the string buffer. Any subsequent operation that alters
  1187. * the content or capacity of the string buffer must then make a copy of
  1188. * the internal buffer at that time. This strategy is effective for
  1189. * reducing the amount of memory allocated by a string concatenation
  1190. * operation when it is implemented using a string buffer.
  1191. *
  1192. * @return a string representation of the string buffer.
  1193. */
  1194. public String toString() {
  1195. return new String(this);
  1196. }
  1197. //
  1198. // The following two methods are needed by String to efficiently
  1199. // convert a StringBuffer into a String. They are not public.
  1200. // They shouldn't be called by anyone but String.
  1201. final void setShared() { shared = true; }
  1202. final char[] getValue() { return value; }
  1203. /**
  1204. * readObject is called to restore the state of the StringBuffer from
  1205. * a stream.
  1206. */
  1207. private synchronized void readObject(java.io.ObjectInputStream s)
  1208. throws java.io.IOException, ClassNotFoundException {
  1209. s.defaultReadObject();
  1210. value = (char[]) value.clone();
  1211. shared = false;
  1212. }
  1213. }