1. /*
  2. * @(#)StringWriter.java 1.24 04/07/16
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * A character stream that collects its output in a string buffer, which can
  10. * then be used to construct a string.
  11. * <p>
  12. * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
  13. * can be called after the stream has been closed without generating an
  14. * <tt>IOException</tt>.
  15. *
  16. * @version 1.24, 04/07/16
  17. * @author Mark Reinhold
  18. * @since JDK1.1
  19. */
  20. public class StringWriter extends Writer {
  21. private StringBuffer buf;
  22. /**
  23. * Create a new string writer, using the default initial string-buffer
  24. * size.
  25. */
  26. public StringWriter() {
  27. buf = new StringBuffer();
  28. lock = buf;
  29. }
  30. /**
  31. * Create a new string writer, using the specified initial string-buffer
  32. * size.
  33. *
  34. * @param initialSize an int specifying the initial size of the buffer.
  35. */
  36. public StringWriter(int initialSize) {
  37. if (initialSize < 0) {
  38. throw new IllegalArgumentException("Negative buffer size");
  39. }
  40. buf = new StringBuffer(initialSize);
  41. lock = buf;
  42. }
  43. /**
  44. * Write a single character.
  45. */
  46. public void write(int c) {
  47. buf.append((char) c);
  48. }
  49. /**
  50. * Write a portion of an array of characters.
  51. *
  52. * @param cbuf Array of characters
  53. * @param off Offset from which to start writing characters
  54. * @param len Number of characters to write
  55. */
  56. public void write(char cbuf[], int off, int len) {
  57. if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  58. ((off + len) > cbuf.length) || ((off + len) < 0)) {
  59. throw new IndexOutOfBoundsException();
  60. } else if (len == 0) {
  61. return;
  62. }
  63. buf.append(cbuf, off, len);
  64. }
  65. /**
  66. * Write a string.
  67. */
  68. public void write(String str) {
  69. buf.append(str);
  70. }
  71. /**
  72. * Write a portion of a string.
  73. *
  74. * @param str String to be written
  75. * @param off Offset from which to start writing characters
  76. * @param len Number of characters to write
  77. */
  78. public void write(String str, int off, int len) {
  79. buf.append(str.substring(off, off + len));
  80. }
  81. /**
  82. * Appends the specified character sequence to this writer.
  83. *
  84. * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
  85. * behaves in exactly the same way as the invocation
  86. *
  87. * <pre>
  88. * out.write(csq.toString()) </pre>
  89. *
  90. * <p> Depending on the specification of <tt>toString</tt> for the
  91. * character sequence <tt>csq</tt>, the entire sequence may not be
  92. * appended. For instance, invoking the <tt>toString</tt> method of a
  93. * character buffer will return a subsequence whose content depends upon
  94. * the buffer's position and limit.
  95. *
  96. * @param csq
  97. * The character sequence to append. If <tt>csq</tt> is
  98. * <tt>null</tt>, then the four characters <tt>"null"</tt> are
  99. * appended to this writer.
  100. *
  101. * @return This writer
  102. *
  103. * @since 1.5
  104. */
  105. public StringWriter append(CharSequence csq) {
  106. if (csq == null)
  107. write("null");
  108. else
  109. write(csq.toString());
  110. return this;
  111. }
  112. /**
  113. * Appends a subsequence of the specified character sequence to this writer.
  114. *
  115. * <p> An invocation of this method of the form <tt>out.append(csq, start,
  116. * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
  117. * exactly the same way as the invocation
  118. *
  119. * <pre>
  120. * out.write(csq.subSequence(start, end).toString()) </pre>
  121. *
  122. * @param csq
  123. * The character sequence from which a subsequence will be
  124. * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
  125. * will be appended as if <tt>csq</tt> contained the four
  126. * characters <tt>"null"</tt>.
  127. *
  128. * @param start
  129. * The index of the first character in the subsequence
  130. *
  131. * @param end
  132. * The index of the character following the last character in the
  133. * subsequence
  134. *
  135. * @return This writer
  136. *
  137. * @throws IndexOutOfBoundsException
  138. * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
  139. * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
  140. * <tt>csq.length()</tt>
  141. *
  142. * @since 1.5
  143. */
  144. public StringWriter append(CharSequence csq, int start, int end) {
  145. CharSequence cs = (csq == null ? "null" : csq);
  146. write(cs.subSequence(start, end).toString());
  147. return this;
  148. }
  149. /**
  150. * Appends the specified character to this writer.
  151. *
  152. * <p> An invocation of this method of the form <tt>out.append(c)</tt>
  153. * behaves in exactly the same way as the invocation
  154. *
  155. * <pre>
  156. * out.write(c) </pre>
  157. *
  158. * @param c
  159. * The 16-bit character to append
  160. *
  161. * @return This writer
  162. *
  163. * @since 1.5
  164. */
  165. public StringWriter append(char c) {
  166. write(c);
  167. return this;
  168. }
  169. /**
  170. * Return the buffer's current value as a string.
  171. */
  172. public String toString() {
  173. return buf.toString();
  174. }
  175. /**
  176. * Return the string buffer itself.
  177. *
  178. * @return StringBuffer holding the current buffer value.
  179. */
  180. public StringBuffer getBuffer() {
  181. return buf;
  182. }
  183. /**
  184. * Flush the stream.
  185. */
  186. public void flush() {
  187. }
  188. /**
  189. * Closing a <tt>StringWriter</tt> has no effect. The methods in this
  190. * class can be called after the stream has been closed without generating
  191. * an <tt>IOException</tt>.
  192. */
  193. public void close() throws IOException {
  194. }
  195. }