1. /*
  2. * @(#)StringWriter.java 1.21 03/01/23
  3. *
  4. * Copyright 2003 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.21, 03/01/23
  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. * Return the buffer's current value as a string.
  83. */
  84. public String toString() {
  85. return buf.toString();
  86. }
  87. /**
  88. * Return the string buffer itself.
  89. *
  90. * @return StringBuffer holding the current buffer value.
  91. */
  92. public StringBuffer getBuffer() {
  93. return buf;
  94. }
  95. /**
  96. * Flush the stream.
  97. */
  98. public void flush() {
  99. }
  100. /**
  101. * Closing a <tt>StringWriter</tt> has no effect. The methods in this
  102. * class can be called after the stream has been closed without generating
  103. * an <tt>IOException</tt>.
  104. */
  105. public void close() throws IOException {
  106. }
  107. }