1. /*
  2. * @(#)StringBuffer.java 1.46 01/11/29
  3. *
  4. * Copyright 2002 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.46, 11/29/01
  64. * @see java.io.ByteArrayOutputStream
  65. * @see java.lang.String
  66. * @since JDK1.0
  67. */
  68. public final class StringBuffer implements java.io.Serializable {
  69. /**
  70. * The value is used for character storage.
  71. *
  72. * @serial
  73. */
  74. private char value[];
  75. /**
  76. * The count is the number of characters in the buffer.
  77. *
  78. * @serial
  79. */
  80. private int count;
  81. /**
  82. * A flag indicating whether the buffer is shared
  83. *
  84. * @serial
  85. */
  86. private boolean shared;
  87. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  88. static final long serialVersionUID = 3388685877147921107L;
  89. /**
  90. * Constructs a string buffer with no characters in it and an
  91. * initial capacity of 16 characters.
  92. */
  93. public StringBuffer() {
  94. this(16);
  95. }
  96. /**
  97. * Constructs a string buffer with no characters in it and an
  98. * initial capacity specified by the <code>length</code> argument.
  99. *
  100. * @param length the initial capacity.
  101. * @exception NegativeArraySizeException if the <code>length</code>
  102. * argument is less than <code>0</code>.
  103. */
  104. public StringBuffer(int length) {
  105. value = new char[length];
  106. shared = false;
  107. }
  108. /**
  109. * Constructs a string buffer so that it represents the same
  110. * sequence of characters as the string argument; in other
  111. * words, the initial contents of the string buffer is a copy of the
  112. * argument string. The initial capacity of the string buffer is
  113. * <code>16</code> plus the length of the string argument.
  114. *
  115. * @param str the initial contents of the buffer.
  116. */
  117. public StringBuffer(String str) {
  118. this(str.length() + 16);
  119. append(str);
  120. }
  121. /**
  122. * Returns the length (character count) of this string buffer.
  123. *
  124. * @return the length of the sequence of characters currently
  125. * represented by this string buffer.
  126. */
  127. public int length() {
  128. return count;
  129. }
  130. /**
  131. * Returns the current capacity of the String buffer. The capacity
  132. * is the amount of storage available for newly inserted
  133. * characters; beyond which an allocation will occur.
  134. *
  135. * @return the current capacity of this string buffer.
  136. */
  137. public int capacity() {
  138. return value.length;
  139. }
  140. /**
  141. * Copies the buffer value. This is normally only called when shared
  142. * is true. It should only be called from a synchronized method.
  143. */
  144. private final void copy() {
  145. char newValue[] = new char[value.length];
  146. System.arraycopy(value, 0, newValue, 0, count);
  147. value = newValue;
  148. shared = false;
  149. }
  150. /**
  151. * Ensures that the capacity of the buffer is at least equal to the
  152. * specified minimum.
  153. * If the current capacity of this string buffer is less than the
  154. * argument, then a new internal buffer is allocated with greater
  155. * capacity. The new capacity is the larger of:
  156. * <ul>
  157. * <li>The <code>minimumCapacity</code> argument.
  158. * <li>Twice the old capacity, plus <code>2</code>.
  159. * </ul>
  160. * If the <code>minimumCapacity</code> argument is nonpositive, this
  161. * method takes no action and simply returns.
  162. *
  163. * @param minimumCapacity the minimum desired capacity.
  164. */
  165. public synchronized void ensureCapacity(int minimumCapacity) {
  166. if (minimumCapacity > value.length) {
  167. expandCapacity(minimumCapacity);
  168. }
  169. }
  170. /**
  171. * This implements the expansion semantics of ensureCapacity but is
  172. * unsynchronized for use internally by methods which are already
  173. * synchronized.
  174. *
  175. * @see java.lang.StringBuffer#ensureCapacity(int)
  176. */
  177. private void expandCapacity(int minimumCapacity) {
  178. int newCapacity = (value.length + 1) * 2;
  179. if (minimumCapacity > newCapacity) {
  180. newCapacity = minimumCapacity;
  181. }
  182. char newValue[] = new char[newCapacity];
  183. System.arraycopy(value, 0, newValue, 0, count);
  184. value = newValue;
  185. shared = false;
  186. }
  187. /**
  188. * Sets the length of this String buffer.
  189. * This string buffer is altered to represent a new character sequence
  190. * whose length is specified by the argument. For every nonnegative
  191. * index <i>k</i> less than <code>newLength</code>, the character at
  192. * index <i>k</i> in the new character sequence is the same as the
  193. * character at index <i>k</i> in the old sequence if <i>k</i> is less
  194. * than the length of the old character sequence; otherwise, it is the
  195. * null character <code>'\u0000'</code>.
  196. *
  197. * In other words, if the <code>newLength</code> argument is less than
  198. * the current length of the string buffer, the string buffer is
  199. * truncated to contain exactly the number of characters given by the
  200. * <code>newLength</code> argument.
  201. * <p>
  202. * If the <code>newLength</code> argument is greater than or equal
  203. * to the current length, sufficient null characters
  204. * (<code>'\u0000'</code>) are appended to the string buffer so that
  205. * length becomes the <code>newLength</code> argument.
  206. * <p>
  207. * The <code>newLength</code> argument must be greater than or equal
  208. * to <code>0</code>.
  209. *
  210. * @param newLength the new length of the buffer.
  211. * @exception IndexOutOfBoundsException if the
  212. * <code>newLength</code> argument is negative.
  213. * @see java.lang.StringBuffer#length()
  214. */
  215. public synchronized void setLength(int newLength) {
  216. if (newLength < 0) {
  217. throw new StringIndexOutOfBoundsException(newLength);
  218. }
  219. if (newLength > value.length) {
  220. expandCapacity(newLength);
  221. }
  222. if (count < newLength) {
  223. if (shared) copy();
  224. for (; count < newLength; count++) {
  225. value[count] = '\0';
  226. }
  227. } else {
  228. count = newLength;
  229. if (shared) copy();
  230. }
  231. }
  232. /**
  233. * The specified character of the sequence currently represented by
  234. * the string buffer, as indicated by the <code>index</code> argument,
  235. * is returned. The first character of a string buffer is at index
  236. * <code>0</code>, the next at index <code>1</code>, and so on, for
  237. * array indexing.
  238. * <p>
  239. * The index argument must be greater than or equal to
  240. * <code>0</code>, and less than the length of this string buffer.
  241. *
  242. * @param index the index of the desired character.
  243. * @return the character at the specified index of this string buffer.
  244. * @exception IndexOutOfBoundsException if <code>index</code> is
  245. * negative or greater than or equal to <code>length()</code>.
  246. * @see java.lang.StringBuffer#length()
  247. */
  248. public synchronized char charAt(int index) {
  249. if ((index < 0) || (index >= count)) {
  250. throw new StringIndexOutOfBoundsException(index);
  251. }
  252. return value[index];
  253. }
  254. /**
  255. * Characters are copied from this string buffer into the
  256. * destination character array <code>dst</code>. The first character to
  257. * be copied is at index <code>srcBegin</code> the last character to
  258. * be copied is at index <code>srcEnd-1</code>. The total number of
  259. * characters to be copied is <code>srcEnd-srcBegin</code>. The
  260. * characters are copied into the subarray of <code>dst</code> starting
  261. * at index <code>dstBegin</code> and ending at index:
  262. * <p><blockquote><pre>
  263. * dstbegin + (srcEnd-srcBegin) - 1
  264. * </pre></blockquote>
  265. *
  266. * @param srcBegin start copying at this offset in the string buffer.
  267. * @param srcEnd stop copying at this offset in the string buffer.
  268. * @param dst the array to copy the data into.
  269. * @param dstBegin offset into <code>dst</code>.
  270. * @exception NullPointerException if <code>dst</code> is
  271. * <code>null</code>.
  272. * @exception IndexOutOfBoundsException if any of the following is true:
  273. * <ul><li><code>srcBegin</code> is negative
  274. * <li>the <code>srcBeing</code> argument is greater than
  275. * the <code>srcEnd</code> argument.
  276. * <li><code>srcEnd</code> is greater than
  277. * <code>this.length()</code>, the current length of this
  278. * string buffer.
  279. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
  280. * <code>dst.length</code></ul>
  281. */
  282. public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  283. if ((srcBegin < 0) || (srcBegin >= count)) {
  284. throw new StringIndexOutOfBoundsException(srcBegin);
  285. }
  286. if ((srcEnd < 0) || (srcEnd > count)) {
  287. throw new StringIndexOutOfBoundsException(srcEnd);
  288. }
  289. if (srcBegin < srcEnd) {
  290. System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  291. } else {
  292. if (srcBegin > srcEnd) {
  293. throw new StringIndexOutOfBoundsException
  294. ("StringBuffer.getChars(): begin > end");
  295. }
  296. /* We do nothing when srcBegin == srcEnd. */
  297. }
  298. }
  299. /**
  300. * The character at the specified index of this string buffer is set
  301. * to <code>ch</code>. The string buffer is altered to represent a new
  302. * character sequence that is identical to the old character sequence,
  303. * except that it contains the character <code>ch</code> at position
  304. * <code>index</code>.
  305. * <p>
  306. * The offset argument must be greater than or equal to
  307. * <code>0</code>, and less than the length of this string buffer.
  308. *
  309. * @param index the index of the character to modify.
  310. * @param ch the new character.
  311. * @exception IndexOutOfBoundsException if <code>index</code> is
  312. * negative or greater than or equal to <code>length()</code>.
  313. * @see java.lang.StringBuffer#length()
  314. */
  315. public synchronized void setCharAt(int index, char ch) {
  316. if ((index < 0) || (index >= count)) {
  317. throw new StringIndexOutOfBoundsException(index);
  318. }
  319. if (shared) copy();
  320. value[index] = ch;
  321. }
  322. /**
  323. * Appends the string representation of the <code>Object</code>
  324. * argument to this string buffer.
  325. * <p>
  326. * The argument is converted to a string as if by the method
  327. * <code>String.valueOf</code>, and the characters of that
  328. * string are then appended to this string buffer.
  329. *
  330. * @param obj an <code>Object</code>.
  331. * @return a reference to this <code>StringBuffer</code> object.
  332. * @see java.lang.String#valueOf(java.lang.Object)
  333. * @see java.lang.StringBuffer#append(java.lang.String)
  334. */
  335. public synchronized StringBuffer append(Object obj) {
  336. return append(String.valueOf(obj));
  337. }
  338. /**
  339. * Appends the string to this string buffer.
  340. * <p>
  341. * The characters of the <code>String</code> argument are appended, in
  342. * order, to the contents of this string buffer, increasing the
  343. * length of this string buffer by the length of the argument.
  344. * If <code>str</code> is <code>null</code>, then the four characters
  345. * <code>"null"</code> are appended to this string buffer.
  346. * <p>
  347. * Let <i>n</i> be the length of the old character sequence, the one
  348. * contained in the string buffer just prior to execution of the
  349. * <code>append</code> method. Then the character at index <i>k</i> in
  350. * the new character sequence is equal to the character at index <i>k</i>
  351. * in the old character sequence, if <i>k</i> is less than <i>n</i>
  352. * otherwise, it is equal to the character at index <i>k-n</i> in the
  353. * argument <code>str</code>.
  354. *
  355. * @param str a string.
  356. * @return a reference to this <code>StringBuffer</code>.
  357. */
  358. public synchronized StringBuffer append(String str) {
  359. if (str == null) {
  360. str = String.valueOf(str);
  361. }
  362. int len = str.length();
  363. int newcount = count + len;
  364. if (newcount > value.length)
  365. expandCapacity(newcount);
  366. str.getChars(0, len, value, count);
  367. count = newcount;
  368. return this;
  369. }
  370. /**
  371. * Appends the string representation of the <code>char</code> array
  372. * argument to this string buffer.
  373. * <p>
  374. * The characters of the array argument are appended, in order, to
  375. * the contents of this string buffer. The length of this string
  376. * buffer increases by the length of the argument.
  377. * <p>
  378. * The overall effect is exactly as if the argument were converted to
  379. * a string by the method {@link String#valueOf(char[])} and the
  380. * characters of that string were then {@link #append(String) appended}
  381. * to this <code>StringBuffer</code> object.
  382. *
  383. * @param str the characters to be appended.
  384. * @return a reference to this <code>StringBuffer</code> object.
  385. */
  386. public synchronized StringBuffer append(char str[]) {
  387. int len = str.length;
  388. int newcount = count + len;
  389. if (newcount > value.length)
  390. expandCapacity(newcount);
  391. System.arraycopy(str, 0, value, count, len);
  392. count = newcount;
  393. return this;
  394. }
  395. /**
  396. * Appends the string representation of a subarray of the
  397. * <code>char</code> array argument to this string buffer.
  398. * <p>
  399. * Characters of the character array <code>str</code>, starting at
  400. * index <code>offset</code>, are appended, in order, to the contents
  401. * of this string buffer. The length of this string buffer increases
  402. * by the value of <code>len</code>.
  403. * <p>
  404. * The overall effect is exactly as if the arguments were converted to
  405. * a string by the method {@link String#valueOf(char[],int,int)} and the
  406. * characters of that string were then {@link #append(String) appended}
  407. * to this <code>StringBuffer</code> object.
  408. *
  409. * @param str the characters to be appended.
  410. * @param offset the index of the first character to append.
  411. * @param len the number of characters to append.
  412. * @return a reference to this <code>StringBuffer</code> object.
  413. */
  414. public synchronized StringBuffer append(char str[], int offset, int len) {
  415. int newcount = count + len;
  416. if (newcount > value.length)
  417. expandCapacity(newcount);
  418. System.arraycopy(str, offset, value, count, len);
  419. count = newcount;
  420. return this;
  421. }
  422. /**
  423. * Appends the string representation of the <code>boolean</code>
  424. * argument to the string buffer.
  425. * <p>
  426. * The argument is converted to a string as if by the method
  427. * <code>String.valueOf</code>, and the characters of that
  428. * string are then appended to this string buffer.
  429. *
  430. * @param b a <code>boolean</code>.
  431. * @return a reference to this <code>StringBuffer</code>.
  432. * @see java.lang.String#valueOf(boolean)
  433. * @see java.lang.StringBuffer#append(java.lang.String)
  434. */
  435. public StringBuffer append(boolean b) {
  436. return append(String.valueOf(b));
  437. }
  438. /**
  439. * Appends the string representation of the <code>char</code>
  440. * argument to this string buffer.
  441. * <p>
  442. * The argument is appended to the contents of this string buffer.
  443. * The length of this string buffer increases by <code>1</code>.
  444. * <p>
  445. * The overall effect is exactly as if the argument were converted to
  446. * a string by the method {@link String#valueOf(char)} and the character
  447. * in that string were then {@link #append(String) appended} to this
  448. * <code>StringBuffer</code> object.
  449. *
  450. * @param ch a <code>char</code>.
  451. * @return a reference to this <code>StringBuffer</code> object.
  452. */
  453. public synchronized StringBuffer append(char c) {
  454. int newcount = count + 1;
  455. if (newcount > value.length)
  456. expandCapacity(newcount);
  457. value[count++] = c;
  458. return this;
  459. }
  460. /**
  461. * Appends the string representation of the <code>int</code>
  462. * argument to this string buffer.
  463. * <p>
  464. * The argument is converted to a string as if by the method
  465. * <code>String.valueOf</code>, and the characters of that
  466. * string are then appended to this string buffer.
  467. *
  468. * @param i an <code>int</code>.
  469. * @return a reference to this <code>StringBuffer</code> object.
  470. * @see java.lang.String#valueOf(int)
  471. * @see java.lang.StringBuffer#append(java.lang.String)
  472. */
  473. public StringBuffer append(int i) {
  474. return append(String.valueOf(i));
  475. }
  476. /**
  477. * Appends the string representation of the <code>long</code>
  478. * argument to this string buffer.
  479. * <p>
  480. * The argument is converted to a string as if by the method
  481. * <code>String.valueOf</code>, and the characters of that
  482. * string are then appended to this string buffer.
  483. *
  484. * @param l a <code>long</code>.
  485. * @return a referenct to this <code>StringBuffer</code> object.
  486. * @see java.lang.String#valueOf(long)
  487. * @see java.lang.StringBuffer#append(java.lang.String)
  488. */
  489. public StringBuffer append(long l) {
  490. return append(String.valueOf(l));
  491. }
  492. /**
  493. * Appends the string representation of the <code>float</code>
  494. * argument to this string buffer.
  495. * <p>
  496. * The argument is converted to a string as if by the method
  497. * <code>String.valueOf</code>, and the characters of that
  498. * string are then appended to this string buffer.
  499. *
  500. * @param f a <code>float</code>.
  501. * @return a reference to this <code>StringBuffer</code> object.
  502. * @see java.lang.String#valueOf(float)
  503. * @see java.lang.StringBuffer#append(java.lang.String)
  504. */
  505. public StringBuffer append(float f) {
  506. return append(String.valueOf(f));
  507. }
  508. /**
  509. * Appends the string representation of the <code>double</code>
  510. * argument to this string buffer.
  511. * <p>
  512. * The argument is converted to a string as if by the method
  513. * <code>String.valueOf</code>, and the characters of that
  514. * string are then appended to this string buffer.
  515. *
  516. * @param d a <code>double</code>.
  517. * @return a reference to this <code>StringBuffer</code> object.
  518. * @see java.lang.String#valueOf(double)
  519. * @see java.lang.StringBuffer#append(java.lang.String)
  520. */
  521. public StringBuffer append(double d) {
  522. return append(String.valueOf(d));
  523. }
  524. /**
  525. * Removes the characters in a substring of this <code>StringBuffer</code>.
  526. * The substring begins at the specified <code>start</code> and extends to
  527. * the character at index <code>end - 1</code> or to the end of the
  528. * <code>StringBuffer</code> if no such character exists. If
  529. * <code>start</code> is equal to <code>end</code>, no changes are made.
  530. *
  531. * @param start The beginning index, inclusive.
  532. * @param end The ending index, exclusive.
  533. * @return This string buffer.
  534. * @exception StringIndexOutOfBoundsException if <code>start</code>
  535. * is negative, greater than <code>length()</code>, or
  536. * greater than <code>end</code>.
  537. * @since JDK1.2
  538. */
  539. public synchronized StringBuffer delete(int start, int end) {
  540. if (start < 0)
  541. throw new StringIndexOutOfBoundsException(start);
  542. if (end > count)
  543. end = count;
  544. if (start > end)
  545. throw new StringIndexOutOfBoundsException();
  546. int len = end - start;
  547. if (len > 0) {
  548. if (shared)
  549. copy();
  550. System.arraycopy(value, start+len, value, start, count-end);
  551. count -= len;
  552. }
  553. return this;
  554. }
  555. /**
  556. * Removes the character at the specified position in this
  557. * <code>StringBuffer</code> (shortening the <code>StringBuffer</code>
  558. * by one character).
  559. *
  560. * @param index Index of character to remove
  561. * @return This string buffer.
  562. * @exception StringIndexOutOfBoundsException if the <code>index</code>
  563. * is negative or greater than or equal to
  564. * <code>length()</code>.
  565. * @since JDK1.2
  566. */
  567. public synchronized StringBuffer deleteCharAt(int index) {
  568. if ((index < 0) || (index >= count))
  569. throw new StringIndexOutOfBoundsException();
  570. if (shared)
  571. copy();
  572. System.arraycopy(value, index+1, value, index, count-index-1);
  573. count--;
  574. return this;
  575. }
  576. /**
  577. * Replaces the characters in a substring of this <code>StringBuffer</code>
  578. * with characters in the specified <code>String</code>. The substring
  579. * begins at the specified <code>start</code> and extends to the character
  580. * at index <code>end - 1</code> or to the end of the
  581. * <code>StringBuffer</code> if no such character exists. First the
  582. * characters in the substring are removed and then the specified
  583. * <code>String</code> is inserted at <code>start</code>. (The
  584. * <code>StringBuffer</code> will be lengthened to accommodate the
  585. * specified String if necessary.)
  586. *
  587. * @param start The beginning index, inclusive.
  588. * @param end The ending index, exclusive.
  589. * @param str String that will replace previous contents.
  590. * @return This string buffer.
  591. * @exception StringIndexOutOfBoundsException if <code>start</code>
  592. * is negative, greater than <code>length()</code>, or
  593. * greater than <code>end</code>.
  594. * @since JDK1.2
  595. */
  596. public synchronized StringBuffer replace(int start, int end, String str) {
  597. if (start < 0)
  598. throw new StringIndexOutOfBoundsException(start);
  599. if (end > count)
  600. end = count;
  601. if (start > end)
  602. throw new StringIndexOutOfBoundsException();
  603. int len = str.length();
  604. int newCount = count + len - (end - start);
  605. if (newCount > value.length)
  606. expandCapacity(newCount);
  607. else if (shared)
  608. copy();
  609. System.arraycopy(value, end, value, start + len, count - end);
  610. str.getChars(0, len, value, start);
  611. count = newCount;
  612. return this;
  613. }
  614. /**
  615. * Returns a new <code>String</code> that contains a subsequence of
  616. * characters currently contained in this <code>StringBuffer</code>.The
  617. * substring begins at the specified index and extends to the end of the
  618. * <code>StringBuffer</code>.
  619. *
  620. * @param start The beginning index, inclusive.
  621. * @return The new string.
  622. * @exception StringIndexOutOfBoundsException if <code>start</code> is
  623. * less than zero, or greater than the length of this
  624. * <code>StringBuffer</code>.
  625. * @since JDK1.2
  626. */
  627. public String substring(int start) {
  628. return substring(start, count);
  629. }
  630. /**
  631. * Returns a new <code>String</code> that contains a subsequence of
  632. * characters currently contained in this <code>StringBuffer</code>. The
  633. * substring begins at the specified <code>start</code> and
  634. * extends to the character at index <code>end - 1</code>. An
  635. * exception is thrown if
  636. *
  637. * @param start The beginning index, inclusive.
  638. * @param end The ending index, exclusive.
  639. * @return The new string.
  640. * @exception StringIndexOutOfBoundsException if <code>start</code>
  641. * or <code>end</code> are negative or greater than
  642. * <code>length()</code>, or <code>start</code> is
  643. * greater than <code>end</code>.
  644. * @since JDK1.2
  645. */
  646. public synchronized String substring(int start, int end) {
  647. if (start < 0)
  648. throw new StringIndexOutOfBoundsException(start);
  649. if (end > count)
  650. throw new StringIndexOutOfBoundsException(end);
  651. if (start > end)
  652. throw new StringIndexOutOfBoundsException(end - start);
  653. return new String(value, start, end - start);
  654. }
  655. /**
  656. * Inserts the string representation of a subarray of the <code>str</code>
  657. * array argument into this string buffer. The subarray begins at the
  658. * specified <code>offset</code> and extends <code>len</code> characters.
  659. * The characters of the subarray are inserted into this string buffer at
  660. * the position indicated by <code>index</code>. The length of this
  661. * <code>StringBuffer</code> increases by <code>len</code> characters.
  662. *
  663. * @param index position at which to insert subarray.
  664. * @param str A character array.
  665. * @param offset the index of the first character in subarray to
  666. * to be inserted.
  667. * @param len the number of characters in the subarray to
  668. * to be inserted.
  669. * @return This string buffer.
  670. * @exception StringIndexOutOfBoundsException if <code>index</code>
  671. * is negative or greater than <code>length()</code>, or
  672. * <code>offset</code> or <code>len</code> are negative, or
  673. * <code>(offset+len)</code> is greater than
  674. * <code>str.length</code>.
  675. * @since JDK1.2
  676. */
  677. public synchronized StringBuffer insert(int index, char str[], int offset,
  678. int len) {
  679. if ((index < 0) || (index > count))
  680. throw new StringIndexOutOfBoundsException();
  681. if ((offset < 0) || (offset + len > str.length))
  682. throw new StringIndexOutOfBoundsException(offset);
  683. if (len < 0)
  684. throw new StringIndexOutOfBoundsException(len);
  685. int newCount = count + len;
  686. if (newCount > value.length)
  687. expandCapacity(newCount);
  688. else if (shared)
  689. copy();
  690. System.arraycopy(value, index, value, index + len, count - index);
  691. System.arraycopy(str, offset, value, index, len);
  692. count = newCount;
  693. return this;
  694. }
  695. /**
  696. * Inserts the string representation of the <code>Object</code>
  697. * argument into this string buffer.
  698. * <p>
  699. * The second argument is converted to a string as if by the method
  700. * <code>String.valueOf</code>, and the characters of that
  701. * string are then inserted into this string buffer at the indicated
  702. * offset.
  703. * <p>
  704. * The offset argument must be greater than or equal to
  705. * <code>0</code>, and less than or equal to the length of this
  706. * string buffer.
  707. *
  708. * @param offset the offset.
  709. * @param b an <code>Object</code>.
  710. * @return a reference to this <code>StringBuffer</code> object.
  711. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  712. * @see java.lang.String#valueOf(java.lang.Object)
  713. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  714. * @see java.lang.StringBuffer#length()
  715. */
  716. public synchronized StringBuffer insert(int offset, Object obj) {
  717. return insert(offset, String.valueOf(obj));
  718. }
  719. /**
  720. * Inserts the string into this string buffer.
  721. * <p>
  722. * The characters of the <code>String</code> argument are inserted, in
  723. * order, into this string buffer at the indicated offset, moving up any
  724. * characters originally above that position and increasing the length
  725. * of this string buffer by the length of the argument. If
  726. * <code>str</code> is <code>null</code>, then the four characters
  727. * <code>"null"</code> are inserted into this string buffer.
  728. * <p>
  729. * The character at index <i>k</i> in the new character sequence is
  730. * equal to:
  731. * <ul>
  732. * <li>the character at index <i>k</i> in the old character sequence, if
  733. * <i>k</i> is less than <code>offset</code>
  734. * <li>the character at index <i>k</i><code>-offset</code> in the
  735. * argument <code>str</code>, if <i>k</i> is not less than
  736. * <code>offset</code> but is less than <code>offset+str.length()</code>
  737. * <li>the character at index <i>k</i><code>-str.length()</code> in the
  738. * old character sequence, if <i>k</i> is not less than
  739. * <code>offset+str.length()</code>
  740. * </ul><p>
  741. * The offset argument must be greater than or equal to
  742. * <code>0</code>, and less than or equal to the length of this
  743. * string buffer.
  744. *
  745. * @param offset the offset.
  746. * @param str a string.
  747. * @return a reference to this <code>StringBuffer</code> object.
  748. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  749. * @see java.lang.StringBuffer#length()
  750. */
  751. public synchronized StringBuffer insert(int offset, String str) {
  752. if ((offset < 0) || (offset > count)) {
  753. throw new StringIndexOutOfBoundsException();
  754. }
  755. if (str == null) {
  756. str = String.valueOf(str);
  757. }
  758. int len = str.length();
  759. int newcount = count + len;
  760. if (newcount > value.length)
  761. expandCapacity(newcount);
  762. else if (shared)
  763. copy();
  764. System.arraycopy(value, offset, value, offset + len, count - offset);
  765. str.getChars(0, len, value, offset);
  766. count = newcount;
  767. return this;
  768. }
  769. /**
  770. * Inserts the string representation of the <code>char</code> array
  771. * argument into this string buffer.
  772. * <p>
  773. * The characters of the array argument are inserted into the
  774. * contents of this string buffer at the position indicated by
  775. * <code>offset</code>. The length of this string buffer increases by
  776. * the length of the argument.
  777. * <p>
  778. * The overall effect is exactly as if the argument were converted to
  779. * a string by the method {@link String#valueOf(char[])} and the
  780. * characters of that string were then
  781. * {@link #insert(int,String) inserted} into this
  782. * <code>StringBuffer</code> object at the position indicated by
  783. * <code>offset</code>.
  784. *
  785. * @param offset the offset.
  786. * @param ch a character array.
  787. * @return a reference to this <code>StringBuffer</code> object.
  788. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  789. */
  790. public synchronized StringBuffer insert(int offset, char str[]) {
  791. if ((offset < 0) || (offset > count)) {
  792. throw new StringIndexOutOfBoundsException();
  793. }
  794. int len = str.length;
  795. int newcount = count + len;
  796. if (newcount > value.length)
  797. expandCapacity(newcount);
  798. else if (shared)
  799. copy();
  800. System.arraycopy(value, offset, value, offset + len, count - offset);
  801. System.arraycopy(str, 0, value, offset, len);
  802. count = newcount;
  803. return this;
  804. }
  805. /**
  806. * Inserts the string representation of the <code>boolean</code>
  807. * argument into this string buffer.
  808. * <p>
  809. * The second argument is converted to a string as if by the method
  810. * <code>String.valueOf</code>, and the characters of that
  811. * string are then inserted into this string buffer at the indicated
  812. * offset.
  813. * <p>
  814. * The offset argument must be greater than or equal to
  815. * <code>0</code>, and less than or equal to the length of this
  816. * string buffer.
  817. *
  818. * @param offset the offset.
  819. * @param b a <code>boolean</code>.
  820. * @return a reference to this <code>StringBuffer</code> object.
  821. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  822. * @see java.lang.String#valueOf(boolean)
  823. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  824. * @see java.lang.StringBuffer#length()
  825. */
  826. public StringBuffer insert(int offset, boolean b) {
  827. return insert(offset, String.valueOf(b));
  828. }
  829. /**
  830. * Inserts the string representation of the <code>char</code>
  831. * argument into this string buffer.
  832. * <p>
  833. * The second argument is inserted into the contents of this string
  834. * buffer at the position indicated by <code>offset</code>. The length
  835. * of this string buffer increases by one.
  836. * <p>
  837. * The overall effect is exactly as if the argument were converted to
  838. * a string by the method {@link String#valueOf(char)} and the character
  839. * in that string were then {@link #insert(int, String) inserted} into
  840. * this <code>StringBuffer</code> object at the position indicated by
  841. * <code>offset</code>.
  842. * <p>
  843. * The offset argument must be greater than or equal to
  844. * <code>0</code>, and less than or equal to the length of this
  845. * string buffer.
  846. *
  847. * @param offset the offset.
  848. * @param ch a <code>char</code>.
  849. * @return a reference to this <code>StringBuffer</code> object.
  850. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  851. * @see java.lang.StringBuffer#length()
  852. */
  853. public synchronized StringBuffer insert(int offset, char c) {
  854. int newcount = count + 1;
  855. if (newcount > value.length)
  856. expandCapacity(newcount);
  857. else if (shared)
  858. copy();
  859. System.arraycopy(value, offset, value, offset + 1, count - offset);
  860. value[offset] = c;
  861. count = newcount;
  862. return this;
  863. }
  864. /**
  865. * Inserts the string representation of the second <code>int</code>
  866. * argument into this string buffer.
  867. * <p>
  868. * The second argument is converted to a string as if by the method
  869. * <code>String.valueOf</code>, and the characters of that
  870. * string are then inserted into this string buffer at the indicated
  871. * offset.
  872. * <p>
  873. * The offset argument must be greater than or equal to
  874. * <code>0</code>, and less than or equal to the length of this
  875. * string buffer.
  876. *
  877. * @param offset the offset.
  878. * @param b an <code>int</code>.
  879. * @return a reference to this <code>StringBuffer</code> object.
  880. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  881. * @see java.lang.String#valueOf(int)
  882. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  883. * @see java.lang.StringBuffer#length()
  884. */
  885. public StringBuffer insert(int offset, int i) {
  886. return insert(offset, String.valueOf(i));
  887. }
  888. /**
  889. * Inserts the string representation of the <code>long</code>
  890. * argument into this string buffer.
  891. * <p>
  892. * The second argument is converted to a string as if by the method
  893. * <code>String.valueOf</code>, and the characters of that
  894. * string are then inserted into this string buffer at the position
  895. * indicated by <code>offset</code>.
  896. * <p>
  897. * The offset argument must be greater than or equal to
  898. * <code>0</code>, and less than or equal to the length of this
  899. * string buffer.
  900. *
  901. * @param offset the offset.
  902. * @param b a <code>long</code>.
  903. * @return a reference to this <code>StringBuffer</code> object.
  904. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  905. * @see java.lang.String#valueOf(long)
  906. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  907. * @see java.lang.StringBuffer#length()
  908. */
  909. public StringBuffer insert(int offset, long l) {
  910. return insert(offset, String.valueOf(l));
  911. }
  912. /**
  913. * Inserts the string representation of the <code>float</code>
  914. * argument into this string buffer.
  915. * <p>
  916. * The second argument is converted to a string as if by the method
  917. * <code>String.valueOf</code>, and the characters of that
  918. * string are then inserted into this string buffer at the indicated
  919. * offset.
  920. * <p>
  921. * The offset argument must be greater than or equal to
  922. * <code>0</code>, and less than or equal to the length of this
  923. * string buffer.
  924. *
  925. * @param offset the offset.
  926. * @param b a <code>float</code>.
  927. * @return a reference to this <code>StringBuffer</code> object.
  928. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  929. * @see java.lang.String#valueOf(float)
  930. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  931. * @see java.lang.StringBuffer#length()
  932. */
  933. public StringBuffer insert(int offset, float f) {
  934. return insert(offset, String.valueOf(f));
  935. }
  936. /**
  937. * Inserts the string representation of the <code>double</code>
  938. * argument into this string buffer.
  939. * <p>
  940. * The second argument is converted to a string as if by the method
  941. * <code>String.valueOf</code>, and the characters of that
  942. * string are then inserted into this string buffer at the indicated
  943. * offset.
  944. * <p>
  945. * The offset argument must be greater than or equal to
  946. * <code>0</code>, and less than or equal to the length of this
  947. * string buffer.
  948. *
  949. * @param offset the offset.
  950. * @param b a <code>double</code>.
  951. * @return a reference to this <code>StringBuffer</code> object.
  952. * @exception StringIndexOutOfBoundsException if the offset is invalid.
  953. * @see java.lang.String#valueOf(double)
  954. * @see java.lang.StringBuffer#insert(int, java.lang.String)
  955. * @see java.lang.StringBuffer#length()
  956. */
  957. public StringBuffer insert(int offset, double d) {
  958. return insert(offset, String.valueOf(d));
  959. }
  960. /**
  961. * The character sequence contained in this string buffer is
  962. * replaced by the reverse of the sequence.
  963. * <p>
  964. * Let <i>n</i> be the length of the old character sequence, the one
  965. * contained in the string buffer just prior to execution of the
  966. * <code>reverse</code> method. Then the character at index <i>k</i> in
  967. * the new character sequence is equal to the character at index
  968. * <i>n-k-1</i> in the old character sequence.
  969. *
  970. * @return a reference to this <codeStringBuffer</code> object..
  971. * @since JDK1.0.2
  972. */
  973. public synchronized StringBuffer reverse() {
  974. if (shared) copy();
  975. int n = count - 1;
  976. for (int j = (n-1) >> 1; j >= 0; --j) {
  977. char temp = value[j];
  978. value[j] = value[n - j];
  979. value[n - j] = temp;
  980. }
  981. return this;
  982. }
  983. /**
  984. * Converts to a string representing the data in this string buffer.
  985. * A new <code>String</code> object is allocated and initialized to
  986. * contain the character sequence currently represented by this
  987. * string buffer. This <code>String</code> is then returned. Subsequent
  988. * changes to the string buffer do not affect the contents of the
  989. * <code>String</code>.
  990. * <p>
  991. * Implementation advice: This method can be coded so as to create a new
  992. * <code>String</code> object without allocating new memory to hold a
  993. * copy of the character sequence. Instead, the string can share the
  994. * memory used by the string buffer. Any subsequent operation that alters
  995. * the content or capacity of the string buffer must then make a copy of
  996. * the internal buffer at that time. This strategy is effective for
  997. * reducing the amount of memory allocated by a string concatenation
  998. * operation when it is implemented using a string buffer.
  999. *
  1000. * @return a string representation of the string buffer.
  1001. */
  1002. public String toString() {
  1003. return new String(this);
  1004. }
  1005. //
  1006. // The following two methods are needed by String to efficiently
  1007. // convert a StringBuffer into a String. They are not public.
  1008. // They shouldn't be called by anyone but String.
  1009. final void setShared() { shared = true; }
  1010. final char[] getValue() { return value; }
  1011. /**
  1012. * readObject is called to restore the state of the StringBuffer from
  1013. * a stream.
  1014. */
  1015. private void readObject(java.io.ObjectInputStream s)
  1016. throws java.io.IOException, ClassNotFoundException {
  1017. s.defaultReadObject();
  1018. value = (char[]) value.clone();
  1019. shared = false;
  1020. }
  1021. }