CharArrayWriter.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* CharArrayWriter.java -- Write chars to a buffer
  2. Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /**
  33. * This class allows data to be written to a char array buffer and
  34. * and then retrieved by an application. The internal char array
  35. * buffer is dynamically resized to hold all the data written. Please
  36. * be aware that writing large amounts to data to this stream will
  37. * cause large amounts of memory to be allocated.
  38. * <p>
  39. * The size of the internal buffer defaults to 32 and it is resized
  40. * in increments of 1024 chars. This behavior can be over-ridden by using the
  41. * following two properties:
  42. * <p>
  43. * <ul>
  44. * <li><xmp>gnu.java.io.CharArrayWriter.initialBufferSize</xmp></li>
  45. * <li><xmp>gnu.java.io.CharArrayWriter.bufferIncrementSize</xmp></li>
  46. * </ul>
  47. * <p>
  48. * There is a constructor that specified the initial buffer size and
  49. * that is the preferred way to set that value because it it portable
  50. * across all Java class library implementations.
  51. * <p>
  52. *
  53. * @author Aaron M. Renn (arenn@urbanophile.com)
  54. * @author Tom Tromey (tromey@cygnus.com)
  55. */
  56. public class CharArrayWriter extends Writer
  57. {
  58. /**
  59. * The default initial buffer size
  60. */
  61. private static final int DEFAULT_INITIAL_BUFFER_SIZE = 32;
  62. /**
  63. * This method initializes a new <code>CharArrayWriter</code> with
  64. * the default buffer size of 32 chars. If a different initial
  65. * buffer size is desired, see the constructor
  66. * <code>CharArrayWriter(int size)</code>.
  67. */
  68. public CharArrayWriter ()
  69. {
  70. this (DEFAULT_INITIAL_BUFFER_SIZE);
  71. }
  72. /**
  73. * This method initializes a new <code>CharArrayWriter</code> with
  74. * a specified initial buffer size.
  75. *
  76. * @param size The initial buffer size in chars
  77. */
  78. public CharArrayWriter (int size)
  79. {
  80. super ();
  81. buf = new char[size];
  82. }
  83. /**
  84. * Closes the stream. This method is guaranteed not to free the contents
  85. * of the internal buffer, which can still be retrieved.
  86. */
  87. public void close ()
  88. {
  89. }
  90. /**
  91. * This method flushes all buffered chars to the stream.
  92. */
  93. public void flush ()
  94. {
  95. }
  96. /**
  97. * This method discards all of the chars that have been written to the
  98. * internal buffer so far by setting the <code>count</code> variable to
  99. * 0. The internal buffer remains at its currently allocated size.
  100. */
  101. public void reset ()
  102. {
  103. synchronized (lock)
  104. {
  105. count = 0;
  106. }
  107. }
  108. /**
  109. * This method returns the number of chars that have been written to
  110. * the buffer so far. This is the same as the value of the protected
  111. * <code>count</code> variable. If the <code>reset</code> method is
  112. * called, then this value is reset as well. Note that this method does
  113. * not return the length of the internal buffer, but only the number
  114. * of chars that have been written to it.
  115. *
  116. * @return The number of chars in the internal buffer
  117. *
  118. * @see #reset()
  119. */
  120. public int size ()
  121. {
  122. return count;
  123. }
  124. /**
  125. * This method returns a char array containing the chars that have been
  126. * written to this stream so far. This array is a copy of the valid
  127. * chars in the internal buffer and its length is equal to the number of
  128. * valid chars, not necessarily to the the length of the current
  129. * internal buffer. Note that since this method allocates a new array,
  130. * it should be used with caution when the internal buffer is very large.
  131. */
  132. public char[] toCharArray ()
  133. {
  134. synchronized (lock)
  135. {
  136. char[] nc = new char[count];
  137. System.arraycopy(buf, 0, nc, 0, count);
  138. return nc;
  139. }
  140. }
  141. /**
  142. * Returns the chars in the internal array as a <code>String</code>. The
  143. * chars in the buffer are converted to characters using the system default
  144. * encoding. There is an overloaded <code>toString()</code> method that
  145. * allows an application specified character encoding to be used.
  146. *
  147. * @return A <code>String</code> containing the data written to this
  148. * stream so far
  149. */
  150. public String toString ()
  151. {
  152. synchronized (lock)
  153. {
  154. return new String (buf, 0, count);
  155. }
  156. }
  157. /**
  158. * This method writes the writes the specified char into the internal
  159. * buffer.
  160. *
  161. * @param oneChar The char to be read passed as an int
  162. */
  163. public void write (int oneChar)
  164. {
  165. synchronized (lock)
  166. {
  167. resize (1);
  168. buf[count++] = (char) oneChar;
  169. }
  170. }
  171. /**
  172. * This method writes <code>len</code> chars from the passed in array
  173. * <code>buf</code> starting at index <code>offset</code> into that buffer
  174. *
  175. * @param buffer The char array to write data from
  176. * @param offset The index into the buffer to start writing data from
  177. * @param len The number of chars to write
  178. */
  179. public void write (char[] buffer, int offset, int len)
  180. {
  181. synchronized (lock)
  182. {
  183. if (len >= 0)
  184. resize (len);
  185. System.arraycopy(buffer, offset, buf, count, len);
  186. count += len;
  187. }
  188. }
  189. /**
  190. * This method writes <code>len</code> chars from the passed in
  191. * <code>String</code> <code>buf</code> starting at index
  192. * <code>offset</code> into the internal buffer.
  193. *
  194. * @param str The <code>String</code> to write data from
  195. * @param offset The index into the string to start writing data from
  196. * @param len The number of chars to write
  197. */
  198. public void write (String str, int offset, int len)
  199. {
  200. synchronized (lock)
  201. {
  202. if (len >= 0)
  203. resize (len);
  204. str.getChars(offset, offset + len, buf, count);
  205. count += len;
  206. }
  207. }
  208. /**
  209. * This method writes all the chars that have been written to this stream
  210. * from the internal buffer to the specified <code>Writer</code>.
  211. *
  212. * @param out The <code>Writer</code> to write to
  213. *
  214. * @exception IOException If an error occurs
  215. */
  216. public void writeTo (Writer out) throws IOException
  217. {
  218. synchronized (lock)
  219. {
  220. out.write(buf, 0, count);
  221. }
  222. }
  223. /**
  224. * Appends the Unicode character, <code>c</code>, to the output stream
  225. * underlying this writer. This is equivalent to <code>write(c)</code>.
  226. *
  227. * @param c the character to append.
  228. * @return a reference to this object.
  229. * @since 1.5
  230. */
  231. public CharArrayWriter append(char c)
  232. {
  233. write(c);
  234. return this;
  235. }
  236. /**
  237. * Appends the specified sequence of Unicode characters to the
  238. * output stream underlying this writer. This is equivalent to
  239. * appending the results of calling <code>toString()</code> on the
  240. * character sequence. As a result, the entire sequence may not be
  241. * appended, as it depends on the implementation of
  242. * <code>toString()</code> provided by the
  243. * <code>CharSequence</code>. For example, if the character
  244. * sequence is wrapped around an input buffer, the results will
  245. * depend on the current position and length of that buffer.
  246. *
  247. * @param cs the character sequence to append. If seq is null,
  248. * then the string "null" (the string representation of null)
  249. * is appended.
  250. * @return a reference to this object.
  251. * @since 1.5
  252. */
  253. public CharArrayWriter append(CharSequence cs)
  254. {
  255. try
  256. {
  257. write(cs == null ? "null" : cs.toString());
  258. }
  259. catch (IOException _)
  260. {
  261. // Can't happen.
  262. }
  263. return this;
  264. }
  265. /**
  266. * Appends the specified subsequence of Unicode characters to the
  267. * output stream underlying this writer, starting and ending at the
  268. * specified positions within the sequence. The behaviour of this
  269. * method matches the behaviour of writing the result of
  270. * <code>append(seq.subSequence(start,end))</code> when the sequence
  271. * is not null.
  272. *
  273. * @param cs the character sequence to append. If seq is null,
  274. * then the string "null" (the string representation of null)
  275. * is appended.
  276. * @param start the index of the first Unicode character to use from
  277. * the sequence.
  278. * @param end the index of the last Unicode character to use from the
  279. * sequence.
  280. * @return a reference to this object.
  281. * @throws IndexOutOfBoundsException if either of the indices are negative,
  282. * the start index occurs after the end index, or the end index is
  283. * beyond the end of the sequence.
  284. * @since 1.5
  285. */
  286. public CharArrayWriter append(CharSequence cs, int start, int end)
  287. {
  288. try
  289. {
  290. write(cs == null ? "null" : cs.subSequence(start, end).toString());
  291. }
  292. catch (IOException _)
  293. {
  294. // Can't happen.
  295. }
  296. return this;
  297. }
  298. /**
  299. * This private method makes the buffer bigger when we run out of room
  300. * by allocating a larger buffer and copying the valid chars from the
  301. * old array into it. This is obviously slow and should be avoided by
  302. * application programmers by setting their initial buffer size big
  303. * enough to hold everything if possible.
  304. */
  305. private void resize (int len)
  306. {
  307. if (count + len >= buf.length)
  308. {
  309. int newlen = buf.length * 2;
  310. if (count + len > newlen)
  311. newlen = count + len;
  312. char[] newbuf = new char[newlen];
  313. System.arraycopy(buf, 0, newbuf, 0, count);
  314. buf = newbuf;
  315. }
  316. }
  317. /**
  318. * The internal buffer where the data written is stored
  319. */
  320. protected char[] buf;
  321. /**
  322. * The number of chars that have been written to the buffer
  323. */
  324. protected int count;
  325. }