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