StringBuffer.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // This is a simplified copy of java.lang.StringBuffer with
  2. // `synchronized' removed.
  3. /* StringBuffer.java -- Growable strings
  4. Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  5. This file is part of GNU Classpath.
  6. GNU Classpath is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. GNU Classpath is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Classpath; see the file COPYING. If not, write to the
  16. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301 USA.
  18. Linking this library statically or dynamically with other modules is
  19. making a combined work based on this library. Thus, the terms and
  20. conditions of the GNU General Public License cover the whole
  21. combination.
  22. As a special exception, the copyright holders of this library give you
  23. permission to link this library with independent modules to produce an
  24. executable, regardless of the license terms of these independent
  25. modules, and to copy and distribute the resulting executable under
  26. terms of your choice, provided that you also meet, for each linked
  27. independent module, the terms and conditions of the license of that
  28. module. An independent module is a module which is not derived from
  29. or based on this library. If you modify this library, you may extend
  30. this exception to your version of the library, but you are not
  31. obligated to do so. If you do not wish to do so, delete this
  32. exception statement from your version. */
  33. package gnu.gcj.runtime;
  34. public final class StringBuffer
  35. {
  36. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  37. * Uses <code>String.valueOf()</code> to convert to
  38. * <code>String</code>.
  39. * @param bool the <code>boolean</code> to convert and append.
  40. * @return this <code>StringBuffer</code>.
  41. * @see java.lang.String#valueOf(boolean)
  42. */
  43. public StringBuffer append (boolean bool)
  44. {
  45. return append (bool ? "true" : "false");
  46. }
  47. /** Append the <code>char</code> to this <code>StringBuffer</code>.
  48. * @param c the <code>char</code> to append.
  49. * @return this <code>StringBuffer</code>.
  50. */
  51. public StringBuffer append (char ch)
  52. {
  53. ensureCapacity_unsynchronized (count + 1);
  54. value[count++] = ch;
  55. return this;
  56. }
  57. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  58. * Uses <code>String.valueOf()</code> to convert to
  59. * <code>String</code>.
  60. * @param inum the <code>int</code> to convert and append.
  61. * @return this <code>StringBuffer</code>.
  62. * @see java.lang.String#valueOf(int)
  63. */
  64. public native StringBuffer append (int inum);
  65. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  66. * Uses <code>String.valueOf()</code> to convert to
  67. * <code>String</code>.
  68. * @param lnum the <code>long</code> to convert and append.
  69. * @return this <code>StringBuffer</code>.
  70. * @see java.lang.String#valueOf(long)
  71. */
  72. public StringBuffer append (long lnum)
  73. {
  74. return append (Long.toString (lnum));
  75. }
  76. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  77. * Uses <code>String.valueOf()</code> to convert to
  78. * <code>String</code>.
  79. * @param fnum the <code>float</code> to convert and append.
  80. * @return this <code>StringBuffer</code>.
  81. * @see java.lang.String#valueOf(float)
  82. */
  83. public StringBuffer append (float fnum)
  84. {
  85. return append (Float.toString (fnum));
  86. }
  87. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  88. * Uses <code>String.valueOf()</code> to convert to
  89. * <code>String</code>.
  90. * @param dnum the <code>double</code> to convert and append.
  91. * @return this <code>StringBuffer</code>.
  92. * @see java.lang.String#valueOf(double)
  93. */
  94. public StringBuffer append (double dnum)
  95. {
  96. return append (Double.toString (dnum));
  97. }
  98. /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
  99. * Uses <code>String.valueOf()</code> to convert to
  100. * <code>String</code>.
  101. * @param obj the <code>Object</code> to convert and append.
  102. * @return this <code>StringBuffer</code>.
  103. * @see java.lang.String#valueOf(java.lang.Object)
  104. */
  105. public StringBuffer append (Object obj)
  106. {
  107. return append (String.valueOf(obj));
  108. }
  109. /** Append the <code>String</code> to this <code>StringBuffer</code>.
  110. * @param str the <code>String</code> to append.
  111. * @return this <code>StringBuffer</code>.
  112. */
  113. public StringBuffer append (String str)
  114. {
  115. if (str == null)
  116. str = "null";
  117. int len = str.length();
  118. ensureCapacity_unsynchronized (count + len);
  119. str.getChars(0, len, value, count);
  120. count += len;
  121. return this;
  122. }
  123. private void ensureCapacity_unsynchronized (int minimumCapacity)
  124. {
  125. if (minimumCapacity > value.length)
  126. {
  127. minimumCapacity = Math.max (minimumCapacity, value.length * 2 + 2);
  128. char[] nb = new char[minimumCapacity];
  129. System.arraycopy(value, 0, nb, 0, count);
  130. value = nb;
  131. }
  132. }
  133. /** Create a new StringBuffer with default capacity 16.
  134. * @see JLS 20.13.1
  135. */
  136. public StringBuffer ()
  137. {
  138. this (DEFAULT_CAPACITY);
  139. }
  140. /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
  141. * @param capacity the initial capacity.
  142. */
  143. public StringBuffer (int capacity)
  144. {
  145. count = 0;
  146. value = new char[capacity];
  147. }
  148. /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>.
  149. * Initial capacity will be the size of the String plus 16.
  150. * @param str the <code>String</code> to make a <code>StringBuffer</code> out of.
  151. */
  152. public StringBuffer (String str)
  153. {
  154. if (str == null)
  155. str = "null";
  156. count = str.length();
  157. // JLS: The initial capacity of the string buffer is 16 plus the
  158. // length of the argument string.
  159. value = new char[count + DEFAULT_CAPACITY];
  160. str.getChars(0, count, value, 0);
  161. }
  162. /** Convert this <code>StringBuffer</code> to a <code>String</code>.
  163. * @return the characters in this StringBuffer
  164. */
  165. // This is native because efficient implementation requires avoiding
  166. // the Java protection mechanism.
  167. public native String toString ();
  168. // Index of next available character. Note that this has
  169. // permissions set this way so that String can get the value.
  170. int count;
  171. // The buffer. Note that this has permissions set this way so that
  172. // String can get the value.
  173. char[] value;
  174. private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1
  175. }