StringBuffer.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /* StringBuffer.java -- Growable strings
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.lang;
  33. import java.io.Serializable;
  34. /**
  35. * <code>StringBuffer</code> represents a changeable <code>String</code>.
  36. * It provides the operations required to modify the
  37. * <code>StringBuffer</code>, including insert, replace, delete, append,
  38. * and reverse. It is thread-safe; meaning that all modifications to a buffer
  39. * are in synchronized methods.
  40. *
  41. * <p><code>StringBuffer</code>s are variable-length in nature, so even if
  42. * you initialize them to a certain size, they can still grow larger than
  43. * that. <em>Capacity</em> indicates the number of characters the
  44. * <code>StringBuffer</code> can have in it before it has to grow (growing
  45. * the char array is an expensive operation involving <code>new</code>).
  46. *
  47. * <p>Incidentally, compilers often implement the String operator "+"
  48. * by using a <code>StringBuffer</code> operation:<br>
  49. * <code>a + b</code><br>
  50. * is the same as<br>
  51. * <code>new StringBuffer().append(a).append(b).toString()</code>.
  52. *
  53. * <p>Classpath's StringBuffer is capable of sharing memory with Strings for
  54. * efficiency. This will help when a StringBuffer is converted to a String
  55. * and the StringBuffer is not changed after that (quite common when performing
  56. * string concatenation).
  57. *
  58. * @author Paul Fisher
  59. * @author John Keiser
  60. * @author Tom Tromey
  61. * @author Eric Blake (ebb9@email.byu.edu)
  62. * @see String
  63. * @since 1.0
  64. * @status updated to 1.4
  65. */
  66. public final class StringBuffer
  67. extends AbstractStringBuffer
  68. implements Serializable, CharSequence, Appendable
  69. {
  70. // Implementation note: if you change this class, you usually will
  71. // want to change StringBuilder as well.
  72. /**
  73. * Compatible with JDK 1.0+.
  74. */
  75. private static final long serialVersionUID = 3388685877147921107L;
  76. /**
  77. * True if the buffer is shared with another object (StringBuffer or
  78. * String); this means the buffer must be copied before writing to it again.
  79. * Note that this has permissions set this way so that String can get the
  80. * value.
  81. *
  82. * @serial whether the buffer is shared
  83. */
  84. boolean shared;
  85. /**
  86. * Create a new StringBuffer with default capacity 16.
  87. */
  88. public StringBuffer()
  89. {
  90. super();
  91. }
  92. /**
  93. * Create an empty <code>StringBuffer</code> with the specified initial
  94. * capacity.
  95. *
  96. * @param capacity the initial capacity
  97. * @throws NegativeArraySizeException if capacity is negative
  98. */
  99. public StringBuffer(int capacity)
  100. {
  101. super(capacity);
  102. }
  103. /**
  104. * Create a new <code>StringBuffer</code> with the characters in the
  105. * specified <code>String</code>. Initial capacity will be the size of the
  106. * String plus 16.
  107. *
  108. * @param str the <code>String</code> to convert
  109. * @throws NullPointerException if str is null
  110. */
  111. public StringBuffer(String str)
  112. {
  113. // Unfortunately, because the size is 16 larger, we cannot share.
  114. super(str);
  115. }
  116. /**
  117. * Create a new <code>StringBuffer</code> with the characters in the
  118. * specified <code>CharSequence</code>. Initial capacity will be the
  119. * length of the sequence plus 16; if the sequence reports a length
  120. * less than or equal to 0, then the initial capacity will be 16.
  121. *
  122. * @param seq the initializing <code>CharSequence</code>
  123. * @throws NullPointerException if str is null
  124. * @since 1.5
  125. */
  126. public StringBuffer(CharSequence seq)
  127. {
  128. super(seq);
  129. }
  130. /**
  131. * Get the length of the <code>String</code> this <code>StringBuffer</code>
  132. * would create. Not to be confused with the <em>capacity</em> of the
  133. * <code>StringBuffer</code>.
  134. *
  135. * @return the length of this <code>StringBuffer</code>
  136. * @see #capacity()
  137. * @see #setLength(int)
  138. */
  139. public synchronized int length()
  140. {
  141. return count;
  142. }
  143. /**
  144. * Get the total number of characters this <code>StringBuffer</code> can
  145. * support before it must be grown. Not to be confused with <em>length</em>.
  146. *
  147. * @return the capacity of this <code>StringBuffer</code>
  148. * @see #length()
  149. * @see #ensureCapacity(int)
  150. */
  151. public synchronized int capacity()
  152. {
  153. return value.length;
  154. }
  155. /**
  156. * Increase the capacity of this <code>StringBuffer</code>. This will
  157. * ensure that an expensive growing operation will not occur until
  158. * <code>minimumCapacity</code> is reached. The buffer is grown to the
  159. * larger of <code>minimumCapacity</code> and
  160. * <code>capacity() * 2 + 2</code>, if it is not already large enough.
  161. *
  162. * @param minimumCapacity the new capacity
  163. * @see #capacity()
  164. */
  165. public synchronized void ensureCapacity(int minimumCapacity)
  166. {
  167. ensureCapacity_unsynchronized(minimumCapacity);
  168. }
  169. /**
  170. * Set the length of this StringBuffer. If the new length is greater than
  171. * the current length, all the new characters are set to '\0'. If the new
  172. * length is less than the current length, the first <code>newLength</code>
  173. * characters of the old array will be preserved, and the remaining
  174. * characters are truncated.
  175. *
  176. * @param newLength the new length
  177. * @throws IndexOutOfBoundsException if the new length is negative
  178. * (while unspecified, this is a StringIndexOutOfBoundsException)
  179. * @see #length()
  180. */
  181. public synchronized void setLength(int newLength)
  182. {
  183. super.setLength(newLength);
  184. }
  185. /**
  186. * Get the character at the specified index.
  187. *
  188. * @param index the index of the character to get, starting at 0
  189. * @return the character at the specified index
  190. * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
  191. * (while unspecified, this is a StringIndexOutOfBoundsException)
  192. */
  193. public synchronized char charAt(int index)
  194. {
  195. return super.charAt(index);
  196. }
  197. /**
  198. * Get the code point at the specified index. This is like #charAt(int),
  199. * but if the character is the start of a surrogate pair, and the
  200. * following character completes the pair, then the corresponding
  201. * supplementary code point is returned.
  202. * @param index the index of the codepoint to get, starting at 0
  203. * @return the codepoint at the specified index
  204. * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
  205. * @since 1.5
  206. */
  207. public synchronized int codePointAt(int index)
  208. {
  209. return super.codePointAt(index);
  210. }
  211. /**
  212. * Get the code point before the specified index. This is like
  213. * #codePointAt(int), but checks the characters at <code>index-1</code> and
  214. * <code>index-2</code> to see if they form a supplementary code point.
  215. * @param index the index just past the codepoint to get, starting at 0
  216. * @return the codepoint at the specified index
  217. * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
  218. * @since 1.5
  219. */
  220. public synchronized int codePointBefore(int index)
  221. {
  222. return super.codePointBefore(index);
  223. }
  224. /**
  225. * Get the specified array of characters. <code>srcOffset - srcEnd</code>
  226. * characters will be copied into the array you pass in.
  227. *
  228. * @param srcOffset the index to start copying from (inclusive)
  229. * @param srcEnd the index to stop copying from (exclusive)
  230. * @param dst the array to copy into
  231. * @param dstOffset the index to start copying into
  232. * @throws NullPointerException if dst is null
  233. * @throws IndexOutOfBoundsException if any source or target indices are
  234. * out of range (while unspecified, source problems cause a
  235. * StringIndexOutOfBoundsException, and dest problems cause an
  236. * ArrayIndexOutOfBoundsException)
  237. * @see System#arraycopy(Object, int, Object, int, int)
  238. */
  239. public synchronized void getChars(int srcOffset, int srcEnd,
  240. char[] dst, int dstOffset)
  241. {
  242. super.getChars(srcOffset, srcEnd, dst, dstOffset);
  243. }
  244. /**
  245. * Set the character at the specified index.
  246. *
  247. * @param index the index of the character to set starting at 0
  248. * @param ch the value to set that character to
  249. * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
  250. * (while unspecified, this is a StringIndexOutOfBoundsException)
  251. */
  252. public synchronized void setCharAt(int index, char ch)
  253. {
  254. super.setCharAt(index, ch);
  255. }
  256. /**
  257. * Append the <code>String</code> value of the argument to this
  258. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  259. * to <code>String</code>.
  260. *
  261. * @param obj the <code>Object</code> to convert and append
  262. * @return this <code>StringBuffer</code>
  263. * @see String#valueOf(Object)
  264. * @see #append(String)
  265. */
  266. public synchronized StringBuffer append(Object obj)
  267. {
  268. super.append(obj);
  269. return this;
  270. }
  271. /**
  272. * Append the <code>String</code> to this <code>StringBuffer</code>. If
  273. * str is null, the String "null" is appended.
  274. *
  275. * @param str the <code>String</code> to append
  276. * @return this <code>StringBuffer</code>
  277. */
  278. public synchronized StringBuffer append(String str)
  279. {
  280. super.append(str);
  281. return this;
  282. }
  283. /**
  284. * Append the <code>StringBuffer</code> value of the argument to this
  285. * <code>StringBuffer</code>. This behaves the same as
  286. * <code>append((Object) stringBuffer)</code>, except it is more efficient.
  287. *
  288. * @param stringBuffer the <code>StringBuffer</code> to convert and append
  289. * @return this <code>StringBuffer</code>
  290. * @see #append(Object)
  291. * @since 1.4
  292. */
  293. public synchronized StringBuffer append(StringBuffer stringBuffer)
  294. {
  295. super.append(stringBuffer);
  296. return this;
  297. }
  298. /**
  299. * Append the <code>char</code> array to this <code>StringBuffer</code>.
  300. * This is similar (but more efficient) than
  301. * <code>append(new String(data))</code>, except in the case of null.
  302. *
  303. * @param data the <code>char[]</code> to append
  304. * @return this <code>StringBuffer</code>
  305. * @throws NullPointerException if <code>str</code> is <code>null</code>
  306. * @see #append(char[], int, int)
  307. */
  308. public synchronized StringBuffer append(char[] data)
  309. {
  310. super.append(data, 0, data.length);
  311. return this;
  312. }
  313. /**
  314. * Append part of the <code>char</code> array to this
  315. * <code>StringBuffer</code>. This is similar (but more efficient) than
  316. * <code>append(new String(data, offset, count))</code>, except in the case
  317. * of null.
  318. *
  319. * @param data the <code>char[]</code> to append
  320. * @param offset the start location in <code>str</code>
  321. * @param count the number of characters to get from <code>str</code>
  322. * @return this <code>StringBuffer</code>
  323. * @throws NullPointerException if <code>str</code> is <code>null</code>
  324. * @throws IndexOutOfBoundsException if offset or count is out of range
  325. * (while unspecified, this is a StringIndexOutOfBoundsException)
  326. */
  327. public synchronized StringBuffer append(char[] data, int offset, int count)
  328. {
  329. super.append(data, offset, count);
  330. return this;
  331. }
  332. /**
  333. * Append the <code>String</code> value of the argument to this
  334. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  335. * to <code>String</code>.
  336. *
  337. * @param bool the <code>boolean</code> to convert and append
  338. * @return this <code>StringBuffer</code>
  339. * @see String#valueOf(boolean)
  340. */
  341. public synchronized StringBuffer append(boolean bool)
  342. {
  343. super.append(bool);
  344. return this;
  345. }
  346. /**
  347. * Append the <code>char</code> to this <code>StringBuffer</code>.
  348. *
  349. * @param ch the <code>char</code> to append
  350. * @return this <code>StringBuffer</code>
  351. */
  352. public synchronized StringBuffer append(char ch)
  353. {
  354. super.append(ch);
  355. return this;
  356. }
  357. /**
  358. * Append the characters in the <code>CharSequence</code> to this
  359. * buffer.
  360. *
  361. * @param seq the <code>CharSequence</code> providing the characters
  362. * @return this <code>StringBuffer</code>
  363. * @since 1.5
  364. */
  365. public synchronized StringBuffer append(CharSequence seq)
  366. {
  367. super.append(seq, 0, seq.length());
  368. return this;
  369. }
  370. /**
  371. * Append some characters from the <code>CharSequence</code> to this
  372. * buffer. If the argument is null, the four characters "null" are
  373. * appended.
  374. *
  375. * @param seq the <code>CharSequence</code> providing the characters
  376. * @param start the starting index
  377. * @param end one past the final index
  378. * @return this <code>StringBuffer</code>
  379. * @since 1.5
  380. */
  381. public synchronized StringBuffer append(CharSequence seq, int start, int end)
  382. {
  383. super.append(seq, start, end);
  384. return this;
  385. }
  386. /**
  387. * Append the <code>String</code> value of the argument to this
  388. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  389. * to <code>String</code>.
  390. *
  391. * @param inum the <code>int</code> to convert and append
  392. * @return this <code>StringBuffer</code>
  393. * @see String#valueOf(int)
  394. */
  395. // This is native in libgcj, for efficiency.
  396. public synchronized StringBuffer append(int inum)
  397. {
  398. super.append(inum);
  399. return this;
  400. }
  401. /**
  402. * Append the <code>String</code> value of the argument to this
  403. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  404. * to <code>String</code>.
  405. *
  406. * @param lnum the <code>long</code> to convert and append
  407. * @return this <code>StringBuffer</code>
  408. * @see String#valueOf(long)
  409. */
  410. public synchronized StringBuffer append(long lnum)
  411. {
  412. super.append(lnum);
  413. return this;
  414. }
  415. /**
  416. * Append the <code>String</code> value of the argument to this
  417. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  418. * to <code>String</code>.
  419. *
  420. * @param fnum the <code>float</code> to convert and append
  421. * @return this <code>StringBuffer</code>
  422. * @see String#valueOf(float)
  423. */
  424. public synchronized StringBuffer append(float fnum)
  425. {
  426. super.append(fnum);
  427. return this;
  428. }
  429. /**
  430. * Append the <code>String</code> value of the argument to this
  431. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  432. * to <code>String</code>.
  433. *
  434. * @param dnum the <code>double</code> to convert and append
  435. * @return this <code>StringBuffer</code>
  436. * @see String#valueOf(double)
  437. */
  438. public synchronized StringBuffer append(double dnum)
  439. {
  440. super.append(dnum);
  441. return this;
  442. }
  443. /**
  444. * Append the code point to this <code>StringBuffer</code>.
  445. * This is like #append(char), but will append two characters
  446. * if a supplementary code point is given.
  447. *
  448. * @param code the code point to append
  449. * @return this <code>StringBuffer</code>
  450. * @see Character#toChars(int, char[], int)
  451. * @since 1.5
  452. */
  453. public synchronized StringBuffer appendCodePoint(int code)
  454. {
  455. super.appendCodePoint(code);
  456. return this;
  457. }
  458. /**
  459. * Delete characters from this <code>StringBuffer</code>.
  460. * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is
  461. * harmless for end to be larger than length().
  462. *
  463. * @param start the first character to delete
  464. * @param end the index after the last character to delete
  465. * @return this <code>StringBuffer</code>
  466. * @throws StringIndexOutOfBoundsException if start or end are out of bounds
  467. * @since 1.2
  468. */
  469. public synchronized StringBuffer delete(int start, int end)
  470. {
  471. // This will unshare if required.
  472. super.delete(start, end);
  473. return this;
  474. }
  475. /**
  476. * Delete a character from this <code>StringBuffer</code>.
  477. *
  478. * @param index the index of the character to delete
  479. * @return this <code>StringBuffer</code>
  480. * @throws StringIndexOutOfBoundsException if index is out of bounds
  481. * @since 1.2
  482. */
  483. public synchronized StringBuffer deleteCharAt(int index)
  484. {
  485. super.deleteCharAt(index);
  486. return this;
  487. }
  488. /**
  489. * Replace characters between index <code>start</code> (inclusive) and
  490. * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
  491. * is larger than the size of this StringBuffer, all characters after
  492. * <code>start</code> are replaced.
  493. *
  494. * @param start the beginning index of characters to delete (inclusive)
  495. * @param end the ending index of characters to delete (exclusive)
  496. * @param str the new <code>String</code> to insert
  497. * @return this <code>StringBuffer</code>
  498. * @throws StringIndexOutOfBoundsException if start or end are out of bounds
  499. * @throws NullPointerException if str is null
  500. * @since 1.2
  501. */
  502. public synchronized StringBuffer replace(int start, int end, String str)
  503. {
  504. super.replace(start, end, str);
  505. return this;
  506. }
  507. /**
  508. * Creates a substring of this StringBuffer, starting at a specified index
  509. * and ending at the end of this StringBuffer.
  510. *
  511. * @param beginIndex index to start substring (base 0)
  512. * @return new String which is a substring of this StringBuffer
  513. * @throws StringIndexOutOfBoundsException if beginIndex is out of bounds
  514. * @see #substring(int, int)
  515. * @since 1.2
  516. */
  517. public String substring(int beginIndex)
  518. {
  519. return substring(beginIndex, count);
  520. }
  521. /**
  522. * Creates a substring of this StringBuffer, starting at a specified index
  523. * and ending at one character before a specified index. This is implemented
  524. * the same as <code>substring(beginIndex, endIndex)</code>, to satisfy
  525. * the CharSequence interface.
  526. *
  527. * @param beginIndex index to start at (inclusive, base 0)
  528. * @param endIndex index to end at (exclusive)
  529. * @return new String which is a substring of this StringBuffer
  530. * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of
  531. * bounds
  532. * @see #substring(int, int)
  533. * @since 1.4
  534. */
  535. public CharSequence subSequence(int beginIndex, int endIndex)
  536. {
  537. return substring(beginIndex, endIndex);
  538. }
  539. /**
  540. * Creates a substring of this StringBuffer, starting at a specified index
  541. * and ending at one character before a specified index.
  542. *
  543. * @param beginIndex index to start at (inclusive, base 0)
  544. * @param endIndex index to end at (exclusive)
  545. * @return new String which is a substring of this StringBuffer
  546. * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out
  547. * of bounds
  548. * @since 1.2
  549. */
  550. public synchronized String substring(int beginIndex, int endIndex)
  551. {
  552. int len = endIndex - beginIndex;
  553. if (beginIndex < 0 || endIndex > count || endIndex < beginIndex)
  554. throw new StringIndexOutOfBoundsException();
  555. if (len == 0)
  556. return "";
  557. // Don't copy unless substring is smaller than 1/4 of the buffer.
  558. boolean share_buffer = ((len << 2) >= value.length);
  559. if (share_buffer)
  560. this.shared = true;
  561. // Package constructor avoids an array copy.
  562. return new String(value, beginIndex, len, share_buffer);
  563. }
  564. /**
  565. * Insert a subarray of the <code>char[]</code> argument into this
  566. * <code>StringBuffer</code>.
  567. *
  568. * @param offset the place to insert in this buffer
  569. * @param str the <code>char[]</code> to insert
  570. * @param str_offset the index in <code>str</code> to start inserting from
  571. * @param len the number of characters to insert
  572. * @return this <code>StringBuffer</code>
  573. * @throws NullPointerException if <code>str</code> is <code>null</code>
  574. * @throws StringIndexOutOfBoundsException if any index is out of bounds
  575. * @since 1.2
  576. */
  577. public synchronized StringBuffer insert(int offset,
  578. char[] str, int str_offset, int len)
  579. {
  580. super.insert(offset, str, str_offset, len);
  581. return this;
  582. }
  583. /**
  584. * Insert the <code>String</code> value of the argument into this
  585. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  586. * to <code>String</code>.
  587. *
  588. * @param offset the place to insert in this buffer
  589. * @param obj the <code>Object</code> to convert and insert
  590. * @return this <code>StringBuffer</code>
  591. * @exception StringIndexOutOfBoundsException if offset is out of bounds
  592. * @see String#valueOf(Object)
  593. */
  594. public synchronized StringBuffer insert(int offset, Object obj)
  595. {
  596. super.insert(offset, obj);
  597. return this;
  598. }
  599. /**
  600. * Insert the <code>String</code> argument into this
  601. * <code>StringBuffer</code>. If str is null, the String "null" is used
  602. * instead.
  603. *
  604. * @param offset the place to insert in this buffer
  605. * @param str the <code>String</code> to insert
  606. * @return this <code>StringBuffer</code>
  607. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  608. */
  609. public synchronized StringBuffer insert(int offset, String str)
  610. {
  611. super.insert(offset, str);
  612. return this;
  613. }
  614. /**
  615. * Insert the <code>CharSequence</code> argument into this
  616. * <code>StringBuffer</code>. If the sequence is null, the String
  617. * "null" is used instead.
  618. *
  619. * @param offset the place to insert in this buffer
  620. * @param sequence the <code>CharSequence</code> to insert
  621. * @return this <code>StringBuffer</code>
  622. * @throws IndexOutOfBoundsException if offset is out of bounds
  623. * @since 1.5
  624. */
  625. public synchronized StringBuffer insert(int offset, CharSequence sequence)
  626. {
  627. super.insert(offset, sequence);
  628. return this;
  629. }
  630. /**
  631. * Insert a subsequence of the <code>CharSequence</code> argument into this
  632. * <code>StringBuffer</code>. If the sequence is null, the String
  633. * "null" is used instead.
  634. *
  635. * @param offset the place to insert in this buffer
  636. * @param sequence the <code>CharSequence</code> to insert
  637. * @param start the starting index of the subsequence
  638. * @param end one past the ending index of the subsequence
  639. * @return this <code>StringBuffer</code>
  640. * @throws IndexOutOfBoundsException if offset, start,
  641. * or end are out of bounds
  642. * @since 1.5
  643. */
  644. public synchronized StringBuffer insert(int offset, CharSequence sequence,
  645. int start, int end)
  646. {
  647. super.insert(offset, sequence, start, end);
  648. return this;
  649. }
  650. /**
  651. * Insert the <code>char[]</code> argument into this
  652. * <code>StringBuffer</code>.
  653. *
  654. * @param offset the place to insert in this buffer
  655. * @param data the <code>char[]</code> to insert
  656. * @return this <code>StringBuffer</code>
  657. * @throws NullPointerException if <code>data</code> is <code>null</code>
  658. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  659. * @see #insert(int, char[], int, int)
  660. */
  661. public synchronized StringBuffer insert(int offset, char[] data)
  662. {
  663. super.insert(offset, data, 0, data.length);
  664. return this;
  665. }
  666. /**
  667. * Insert the <code>String</code> value of the argument into this
  668. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  669. * to <code>String</code>.
  670. *
  671. * @param offset the place to insert in this buffer
  672. * @param bool the <code>boolean</code> to convert and insert
  673. * @return this <code>StringBuffer</code>
  674. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  675. * @see String#valueOf(boolean)
  676. */
  677. public synchronized StringBuffer insert(int offset, boolean bool)
  678. {
  679. super.insert(offset, bool);
  680. return this;
  681. }
  682. /**
  683. * Insert the <code>char</code> argument into this <code>StringBuffer</code>.
  684. *
  685. * @param offset the place to insert in this buffer
  686. * @param ch the <code>char</code> to insert
  687. * @return this <code>StringBuffer</code>
  688. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  689. */
  690. public synchronized StringBuffer insert(int offset, char ch)
  691. {
  692. super.insert(offset, ch);
  693. return this;
  694. }
  695. /**
  696. * Insert the <code>String</code> value of the argument into this
  697. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  698. * to <code>String</code>.
  699. *
  700. * @param offset the place to insert in this buffer
  701. * @param inum the <code>int</code> to convert and insert
  702. * @return this <code>StringBuffer</code>
  703. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  704. * @see String#valueOf(int)
  705. */
  706. public synchronized StringBuffer insert(int offset, int inum)
  707. {
  708. super.insert(offset, inum);
  709. return this;
  710. }
  711. /**
  712. * Insert the <code>String</code> value of the argument into this
  713. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  714. * to <code>String</code>.
  715. *
  716. * @param offset the place to insert in this buffer
  717. * @param lnum the <code>long</code> to convert and insert
  718. * @return this <code>StringBuffer</code>
  719. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  720. * @see String#valueOf(long)
  721. */
  722. public synchronized StringBuffer insert(int offset, long lnum)
  723. {
  724. super.insert(offset, lnum);
  725. return this;
  726. }
  727. /**
  728. * Insert the <code>String</code> value of the argument into this
  729. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  730. * to <code>String</code>.
  731. *
  732. * @param offset the place to insert in this buffer
  733. * @param fnum the <code>float</code> to convert and insert
  734. * @return this <code>StringBuffer</code>
  735. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  736. * @see String#valueOf(float)
  737. */
  738. public synchronized StringBuffer insert(int offset, float fnum)
  739. {
  740. super.insert(offset, fnum);
  741. return this;
  742. }
  743. /**
  744. * Insert the <code>String</code> value of the argument into this
  745. * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
  746. * to <code>String</code>.
  747. *
  748. * @param offset the place to insert in this buffer
  749. * @param dnum the <code>double</code> to convert and insert
  750. * @return this <code>StringBuffer</code>
  751. * @throws StringIndexOutOfBoundsException if offset is out of bounds
  752. * @see String#valueOf(double)
  753. */
  754. public synchronized StringBuffer insert(int offset, double dnum)
  755. {
  756. super.insert(offset, dnum);
  757. return this;
  758. }
  759. /**
  760. * Finds the first instance of a substring in this StringBuffer.
  761. *
  762. * @param str String to find
  763. * @return location (base 0) of the String, or -1 if not found
  764. * @throws NullPointerException if str is null
  765. * @see #indexOf(String, int)
  766. * @since 1.4
  767. */
  768. public synchronized int indexOf(String str)
  769. {
  770. return super.indexOf(str, 0);
  771. }
  772. /**
  773. * Finds the first instance of a String in this StringBuffer, starting at
  774. * a given index. If starting index is less than 0, the search starts at
  775. * the beginning of this String. If the starting index is greater than the
  776. * length of this String, or the substring is not found, -1 is returned.
  777. *
  778. * @param str String to find
  779. * @param fromIndex index to start the search
  780. * @return location (base 0) of the String, or -1 if not found
  781. * @throws NullPointerException if str is null
  782. * @since 1.4
  783. */
  784. public synchronized int indexOf(String str, int fromIndex)
  785. {
  786. return super.indexOf(str, fromIndex);
  787. }
  788. /**
  789. * Finds the last instance of a substring in this StringBuffer.
  790. *
  791. * @param str String to find
  792. * @return location (base 0) of the String, or -1 if not found
  793. * @throws NullPointerException if str is null
  794. * @see #lastIndexOf(String, int)
  795. * @since 1.4
  796. */
  797. public synchronized int lastIndexOf(String str)
  798. {
  799. return super.lastIndexOf(str, count - str.count);
  800. }
  801. /**
  802. * Finds the last instance of a String in this StringBuffer, starting at a
  803. * given index. If starting index is greater than the maximum valid index,
  804. * then the search begins at the end of this String. If the starting index
  805. * is less than zero, or the substring is not found, -1 is returned.
  806. *
  807. * @param str String to find
  808. * @param fromIndex index to start the search
  809. * @return location (base 0) of the String, or -1 if not found
  810. * @throws NullPointerException if str is null
  811. * @since 1.4
  812. */
  813. public synchronized int lastIndexOf(String str, int fromIndex)
  814. {
  815. return super.lastIndexOf(str, fromIndex);
  816. }
  817. /**
  818. * Reverse the characters in this StringBuffer. The same sequence of
  819. * characters exists, but in the reverse index ordering.
  820. *
  821. * @return this <code>StringBuffer</code>
  822. */
  823. public synchronized StringBuffer reverse()
  824. {
  825. super.reverse();
  826. return this;
  827. }
  828. /**
  829. * Convert this <code>StringBuffer</code> to a <code>String</code>. The
  830. * String is composed of the characters currently in this StringBuffer. Note
  831. * that the result is a copy, and that future modifications to this buffer
  832. * do not affect the String.
  833. *
  834. * @return the characters in this StringBuffer
  835. */
  836. public String toString()
  837. {
  838. // The string will set this.shared = true.
  839. return new String(this);
  840. }
  841. /**
  842. * This may reduce the amount of memory used by the StringBuffer,
  843. * by resizing the internal array to remove unused space. However,
  844. * this method is not required to resize, so this behavior cannot
  845. * be relied upon.
  846. * @since 1.5
  847. */
  848. public synchronized void trimToSize()
  849. {
  850. super.trimToSize();
  851. }
  852. /**
  853. * Return the number of code points between two indices in the
  854. * <code>StringBuffer</code>. An unpaired surrogate counts as a
  855. * code point for this purpose. Characters outside the indicated
  856. * range are not examined, even if the range ends in the middle of a
  857. * surrogate pair.
  858. *
  859. * @param start the starting index
  860. * @param end one past the ending index
  861. * @return the number of code points
  862. * @since 1.5
  863. */
  864. public synchronized int codePointCount(int start, int end)
  865. {
  866. return super.codePointCount(start, end);
  867. }
  868. /**
  869. * Starting at the given index, this counts forward by the indicated
  870. * number of code points, and then returns the resulting index. An
  871. * unpaired surrogate counts as a single code point for this
  872. * purpose.
  873. *
  874. * @param start the starting index
  875. * @param codePoints the number of code points
  876. * @return the resulting index
  877. * @since 1.5
  878. */
  879. public synchronized int offsetByCodePoints(int start, int codePoints)
  880. {
  881. return super.offsetByCodePoints(start, codePoints);
  882. }
  883. /**
  884. * An unsynchronized version of ensureCapacity, used internally to avoid
  885. * the cost of a second lock on the same object. This also has the side
  886. * effect of duplicating the array, if it was shared (to form copy-on-write
  887. * semantics).
  888. *
  889. * @param minimumCapacity the minimum capacity
  890. * @see #ensureCapacity(int)
  891. */
  892. void ensureCapacity_unsynchronized(int minimumCapacity)
  893. {
  894. if (shared || minimumCapacity > value.length)
  895. {
  896. // We don't want to make a larger vector when `shared' is
  897. // set. If we do, then setLength becomes very inefficient
  898. // when repeatedly reusing a StringBuffer in a loop.
  899. int max = (minimumCapacity > value.length
  900. ? value.length * 2 + 2
  901. : value.length);
  902. minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
  903. char[] nb = new char[minimumCapacity];
  904. VMSystem.arraycopy(value, 0, nb, 0, count);
  905. value = nb;
  906. shared = false;
  907. }
  908. }
  909. }