1. /*
  2. * @(#)StringWriter.java 1.18 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.io;
  11. /**
  12. * A character stream that collects its output in a string buffer, which can
  13. * then be used to construct a string.
  14. *
  15. * @version 1.18, 00/02/02
  16. * @author Mark Reinhold
  17. * @since JDK1.1
  18. */
  19. public class StringWriter extends Writer {
  20. private StringBuffer buf;
  21. /**
  22. * Flag indicating whether the stream has been closed.
  23. */
  24. private boolean isClosed = false;
  25. /** Check to make sure that the stream has not been closed */
  26. private void ensureOpen() {
  27. /* This method does nothing for now. Once we add throws clauses
  28. * to the I/O methods in this class, it will throw an IOException
  29. * if the stream has been closed.
  30. */
  31. }
  32. /**
  33. * Create a new string writer, using the default initial string-buffer
  34. * size.
  35. */
  36. public StringWriter() {
  37. buf = new StringBuffer();
  38. lock = buf;
  39. }
  40. /**
  41. * Create a new string writer, using the specified initial string-buffer
  42. * size.
  43. *
  44. * @param initialSize an int specifying the initial size of the buffer.
  45. */
  46. public StringWriter(int initialSize) {
  47. if (initialSize < 0) {
  48. throw new IllegalArgumentException("Negative buffer size");
  49. }
  50. buf = new StringBuffer(initialSize);
  51. lock = buf;
  52. }
  53. /**
  54. * Write a single character.
  55. */
  56. public void write(int c) {
  57. ensureOpen();
  58. buf.append((char) c);
  59. }
  60. /**
  61. * Write a portion of an array of characters.
  62. *
  63. * @param cbuf Array of characters
  64. * @param off Offset from which to start writing characters
  65. * @param len Number of characters to write
  66. */
  67. public void write(char cbuf[], int off, int len) {
  68. ensureOpen();
  69. if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  70. ((off + len) > cbuf.length) || ((off + len) < 0)) {
  71. throw new IndexOutOfBoundsException();
  72. } else if (len == 0) {
  73. return;
  74. }
  75. buf.append(cbuf, off, len);
  76. }
  77. /**
  78. * Write a string.
  79. */
  80. public void write(String str) {
  81. ensureOpen();
  82. buf.append(str);
  83. }
  84. /**
  85. * Write a portion of a string.
  86. *
  87. * @param str String to be written
  88. * @param off Offset from which to start writing characters
  89. * @param len Number of characters to write
  90. */
  91. public void write(String str, int off, int len) {
  92. ensureOpen();
  93. buf.append(str.substring(off, off + len));
  94. }
  95. /**
  96. * Return the buffer's current value as a string.
  97. */
  98. public String toString() {
  99. return buf.toString();
  100. }
  101. /**
  102. * Return the string buffer itself.
  103. *
  104. * @return StringBuffer holding the current buffer value.
  105. */
  106. public StringBuffer getBuffer() {
  107. return buf;
  108. }
  109. /**
  110. * Flush the stream.
  111. */
  112. public void flush() {
  113. ensureOpen();
  114. }
  115. /**
  116. * Close the stream. This method does not release the buffer, since its
  117. * contents might still be required.
  118. */
  119. public void close() throws IOException {
  120. isClosed = true;
  121. }
  122. }