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