StringWriter.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* StringWriter.java -- Writes bytes to a StringBuffer
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. // Wow is this a dumb class. CharArrayWriter can do all this and
  33. // more. I would redirect all calls to one in fact, but the javadocs say
  34. // use a StringBuffer so I will comply.
  35. /**
  36. * This class writes chars to an internal <code>StringBuffer</code> that
  37. * can then be used to retrieve a <code>String</code>.
  38. *
  39. * @author Aaron M. Renn (arenn@urbanophile.com)
  40. * @author Tom Tromey (tromey@cygnus.com)
  41. */
  42. public class StringWriter extends Writer
  43. {
  44. /**
  45. * This is the default size of the buffer if the user doesn't specify it.
  46. * @specnote The JCL Volume 1 says that 16 is the default size.
  47. */
  48. private static final int DEFAULT_BUFFER_SIZE = 16;
  49. /**
  50. * This method closes the stream. The contents of the internal buffer
  51. * can still be retrieved, but future writes are not guaranteed to work.
  52. *
  53. * @exception IOException If an error orrurs.
  54. */
  55. public void close () throws IOException
  56. {
  57. // JCL says this does nothing. This seems to violate the Writer
  58. // contract, in that other methods should still throw an
  59. // IOException after a close. Still, we just follow JCL.
  60. }
  61. /**
  62. * This method flushes any buffered characters to the underlying output.
  63. * It does nothing in this class.
  64. */
  65. public void flush ()
  66. {
  67. }
  68. /**
  69. * This method returns the <code>StringBuffer</code> object that this
  70. * object is writing to. Note that this is the actual internal buffer, so
  71. * any operations performed on it will affect this stream object.
  72. *
  73. * @return The <code>StringBuffer</code> object being written to
  74. */
  75. public StringBuffer getBuffer ()
  76. {
  77. return buffer;
  78. }
  79. /**
  80. * This method initializes a new <code>StringWriter</code> to write to a
  81. * <code>StringBuffer</code> initially sized to a default size of 16
  82. * chars.
  83. */
  84. public StringWriter ()
  85. {
  86. this (DEFAULT_BUFFER_SIZE);
  87. }
  88. /**
  89. * This method initializes a new <code>StringWriter</code> to write to a
  90. * <code>StringBuffer</code> with the specified initial size.
  91. *
  92. * @param size The initial size to make the <code>StringBuffer</code>
  93. */
  94. public StringWriter (int size)
  95. {
  96. super ();
  97. buffer = new StringBuffer (size);
  98. lock = buffer;
  99. }
  100. /**
  101. * This method returns the contents of the internal <code>StringBuffer</code>
  102. * as a <code>String</code>.
  103. *
  104. * @return A <code>String</code> representing the chars written to
  105. * this stream.
  106. */
  107. public String toString ()
  108. {
  109. return buffer.toString();
  110. }
  111. /**
  112. * This method writes a single character to the output, storing it in
  113. * the internal buffer.
  114. *
  115. * @param oneChar The <code>char</code> to write, passed as an int.
  116. */
  117. public void write (int oneChar)
  118. {
  119. buffer.append((char) (oneChar & 0xFFFF));
  120. }
  121. /**
  122. * This method writes <code>len</code> chars from the specified
  123. * array starting at index <code>offset</code> in that array to this
  124. * stream by appending the chars to the end of the internal buffer.
  125. *
  126. * @param chars The array of chars to write
  127. * @param offset The index into the array to start writing from
  128. * @param len The number of chars to write
  129. */
  130. public void write (char[] chars, int offset, int len)
  131. {
  132. buffer.append(chars, offset, len);
  133. }
  134. /**
  135. * This method writes the characters in the specified <code>String</code>
  136. * to the stream by appending them to the end of the internal buffer.
  137. *
  138. * @param str The <code>String</code> to write to the stream.
  139. */
  140. public void write (String str)
  141. {
  142. buffer.append(str);
  143. }
  144. /**
  145. * This method writes out <code>len</code> characters of the specified
  146. * <code>String</code> to the stream starting at character position
  147. * <code>offset</code> into the stream. This is done by appending the
  148. * characters to the internal buffer.
  149. *
  150. * @param str The <code>String</code> to write characters from
  151. * @param offset The character position to start writing from
  152. * @param len The number of characters to write.
  153. */
  154. public void write (String str, int offset, int len)
  155. {
  156. // char[] tmpbuf = new char[len];
  157. // str.getChars(offset, offset+len, tmpbuf, 0);
  158. // buf.append(tmpbuf, 0, tmpbuf.length);
  159. // This implementation assumes that String.substring is more
  160. // efficient than using String.getChars and copying the data
  161. // twice. For libgcj, this is true. For Classpath, it is not.
  162. // FIXME.
  163. buffer.append(str.substring(offset, offset + len));
  164. }
  165. /** @since 1.5 */
  166. public StringWriter append(char c)
  167. {
  168. write(c);
  169. return this;
  170. }
  171. /** @since 1.5 */
  172. public StringWriter append(CharSequence cs)
  173. {
  174. write(cs == null ? "null" : cs.toString());
  175. return this;
  176. }
  177. /** @since 1.5 */
  178. public StringWriter append(CharSequence cs, int start, int end)
  179. {
  180. write(cs == null ? "null" : cs.subSequence(start, end).toString());
  181. return this;
  182. }
  183. /**
  184. * This is the <code>StringBuffer</code> that we use to store bytes that
  185. * are written.
  186. */
  187. private StringBuffer buffer;
  188. }