1. /*
  2. * @(#)CharArrayWriter.java 1.12 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. * This class implements a character buffer that can be used as an Writer.
  10. * The buffer automatically grows when data is written to the stream. The data
  11. * can be retrieved using toCharArray() and toString().
  12. *
  13. * @author Herb Jellinek
  14. * @version 1.12, 11/29/01
  15. * @since JDK1.1
  16. */
  17. public
  18. class CharArrayWriter extends Writer {
  19. /**
  20. * The buffer where data is stored.
  21. */
  22. protected char buf[];
  23. /**
  24. * The number of chars in the buffer.
  25. */
  26. protected int count;
  27. /**
  28. * Creates a new CharArrayWriter.
  29. */
  30. public CharArrayWriter() {
  31. this(32);
  32. }
  33. /**
  34. * Creates a new CharArrayWriter with the specified initial size.
  35. *
  36. * @exception IllegalArgumentException if initialSize is negative
  37. */
  38. public CharArrayWriter(int initialSize) {
  39. if (initialSize < 0) {
  40. throw new IllegalArgumentException("Negative initial size: "
  41. + initialSize);
  42. }
  43. buf = new char[initialSize];
  44. }
  45. /**
  46. * Writes a character to the buffer.
  47. */
  48. public void write(int c) {
  49. synchronized (lock) {
  50. int newcount = count + 1;
  51. if (newcount > buf.length) {
  52. char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  53. System.arraycopy(buf, 0, newbuf, 0, count);
  54. buf = newbuf;
  55. }
  56. buf[count] = (char)c;
  57. count = newcount;
  58. }
  59. }
  60. /**
  61. * Writes characters to the buffer.
  62. * @param c the data to be written
  63. * @param off the start offset in the data
  64. * @param len the number of chars that are written
  65. */
  66. public void write(char c[], int off, int len) {
  67. if ((off < 0) || (off > c.length) || (len < 0) ||
  68. ((off + len) > c.length) || ((off + len) < 0)) {
  69. throw new IndexOutOfBoundsException();
  70. } else if (len == 0) {
  71. return;
  72. }
  73. synchronized (lock) {
  74. int newcount = count + len;
  75. if (newcount > buf.length) {
  76. char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  77. System.arraycopy(buf, 0, newbuf, 0, count);
  78. buf = newbuf;
  79. }
  80. System.arraycopy(c, off, buf, count, len);
  81. count = newcount;
  82. }
  83. }
  84. /**
  85. * Write a portion of a string to the buffer.
  86. * @param str String to be written from
  87. * @param off Offset from which to start reading characters
  88. * @param len Number of characters to be written
  89. */
  90. public void write(String str, int off, int len) {
  91. synchronized (lock) {
  92. int newcount = count + len;
  93. if (newcount > buf.length) {
  94. char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  95. System.arraycopy(buf, 0, newbuf, 0, count);
  96. buf = newbuf;
  97. }
  98. str.getChars(off, off + len, buf, count);
  99. count = newcount;
  100. }
  101. }
  102. /**
  103. * Writes the contents of the buffer to another character stream.
  104. * @param out the output stream to write to
  105. */
  106. public void writeTo(Writer out) throws IOException {
  107. synchronized (lock) {
  108. out.write(buf, 0, count);
  109. }
  110. }
  111. /**
  112. * Resets the buffer so that you can use it again without
  113. * throwing away the already allocated buffer.
  114. */
  115. public void reset() {
  116. count = 0;
  117. }
  118. /**
  119. * Returns a copy of the input data.
  120. */
  121. public char toCharArray()[] {
  122. synchronized (lock) {
  123. char newbuf[] = new char[count];
  124. System.arraycopy(buf, 0, newbuf, 0, count);
  125. return newbuf;
  126. }
  127. }
  128. /**
  129. * Returns the current size of the buffer.
  130. */
  131. public int size() {
  132. return count;
  133. }
  134. /**
  135. * Converts input data to a string.
  136. * @return the string.
  137. */
  138. public String toString() {
  139. synchronized (lock) {
  140. return new String(toCharArray());
  141. }
  142. }
  143. /**
  144. * Flush the stream.
  145. */
  146. public void flush() { }
  147. /**
  148. * Close the stream. This method does not release the buffer, since its
  149. * contents might still be required.
  150. */
  151. public void close() { }
  152. }