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