BufferedWriter.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* BufferedWriter.java -- Buffer output into large blocks before writing
  2. Copyright (C) 1998, 1999, 2000, 2001 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. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  33. * "The Java Language Specification", ISBN 0-201-63451-1
  34. * Status: Complete to version 1.1.
  35. */
  36. /**
  37. * This class accumulates chars written in a buffer instead of immediately
  38. * writing the data to the underlying output sink. The chars are instead
  39. * as one large block when the buffer is filled, or when the stream is
  40. * closed or explicitly flushed. This mode operation can provide a more
  41. * efficient mechanism for writing versus doing numerous small unbuffered
  42. * writes.
  43. *
  44. * @author Aaron M. Renn (arenn@urbanophile.com)
  45. * @author Tom Tromey (tromey@cygnus.com)
  46. * @date September 25, 1998
  47. */
  48. public class BufferedWriter extends Writer
  49. {
  50. /**
  51. * This is the default buffer size
  52. */
  53. private static final int DEFAULT_BUFFER_SIZE = 8192;
  54. /**
  55. * This is the underlying <code>Writer</code> to which this object
  56. * sends its output.
  57. */
  58. private Writer out;
  59. /**
  60. * This is the internal char array used for buffering output before
  61. * writing it.
  62. */
  63. char[] buffer;
  64. /**
  65. * This is the number of chars that are currently in the buffer and
  66. * are waiting to be written to the underlying stream. It always points to
  67. * the index into the buffer where the next char of data will be stored
  68. */
  69. int count;
  70. /**
  71. * This method initializes a new <code>BufferedWriter</code> instance
  72. * that will write to the specified subordinate <code>Writer</code>
  73. * and which will use a default buffer size of 8192 chars.
  74. *
  75. * @param out The underlying <code>Writer</code> to write data to
  76. */
  77. public BufferedWriter (Writer out)
  78. {
  79. this (out, DEFAULT_BUFFER_SIZE);
  80. }
  81. /**
  82. * This method initializes a new <code>BufferedWriter</code> instance
  83. * that will write to the specified subordinate <code>Writer</code>
  84. * and which will use the specified buffer size
  85. *
  86. * @param out The underlying <code>Writer</code> to write data to
  87. * @param size The size of the internal buffer
  88. */
  89. public BufferedWriter (Writer out, int size)
  90. {
  91. super(out.lock);
  92. this.out = out;
  93. this.buffer = new char[size];
  94. this.count = 0;
  95. }
  96. /**
  97. * This method flushes any remaining buffered chars then closes the
  98. * underlying output stream. Any further attempts to write to this stream
  99. * may throw an exception
  100. *
  101. * @exception IOException If an error occurs.
  102. */
  103. public void close () throws IOException
  104. {
  105. synchronized (lock)
  106. {
  107. // It is safe to call localFlush even if the stream is already
  108. // closed.
  109. localFlush ();
  110. out.close();
  111. buffer = null;
  112. }
  113. }
  114. /**
  115. * This method causes any currently buffered chars to be immediately
  116. * written to the underlying output stream.
  117. *
  118. * @exception IOException If an error occurs
  119. */
  120. public void flush () throws IOException
  121. {
  122. synchronized (lock)
  123. {
  124. if (buffer == null)
  125. throw new IOException ("Stream closed");
  126. localFlush ();
  127. out.flush();
  128. }
  129. }
  130. /**
  131. * This method writes out a system depedent line separator sequence. The
  132. * actual value written is detemined from the <xmp>line.separator</xmp>
  133. * system property.
  134. *
  135. * @exception IOException If an error occurs
  136. */
  137. public void newLine () throws IOException
  138. {
  139. write (System.getProperty("line.separator"));
  140. }
  141. /**
  142. * This method writes a single char of data. This will be written to the
  143. * buffer instead of the underlying data source. However, if the buffer
  144. * is filled as a result of this write request, it will be flushed to the
  145. * underlying output stream.
  146. *
  147. * @param oneChar The char of data to be written, passed as an int
  148. *
  149. * @exception IOException If an error occurs
  150. */
  151. public void write (int oneChar) throws IOException
  152. {
  153. synchronized (lock)
  154. {
  155. if (buffer == null)
  156. throw new IOException ("Stream closed");
  157. buffer[count++] = (char) oneChar;
  158. if (count == buffer.length)
  159. localFlush ();
  160. }
  161. }
  162. /**
  163. * This method writes <code>len</code> chars from the char array
  164. * <code>buf</code> starting at position <code>offset</code> in the buffer.
  165. * These chars will be written to the internal buffer. However, if this
  166. * write operation fills the buffer, the buffer will be flushed to the
  167. * underlying output stream.
  168. *
  169. * @param buf The array of chars to write.
  170. * @param offset The index into the char array to start writing from.
  171. * @param len The number of chars to write.
  172. *
  173. * @exception IOException If an error occurs
  174. */
  175. public void write (char[] buf, int offset, int len) throws IOException
  176. {
  177. synchronized (lock)
  178. {
  179. if (buffer == null)
  180. throw new IOException ("Stream closed");
  181. // Bypass buffering if there is too much incoming data.
  182. if (count + len > buffer.length)
  183. {
  184. localFlush ();
  185. out.write(buf, offset, len);
  186. }
  187. else
  188. {
  189. System.arraycopy(buf, offset, buffer, count, len);
  190. count += len;
  191. if (count == buffer.length)
  192. localFlush ();
  193. }
  194. }
  195. }
  196. /**
  197. * This method writes <code>len</code> chars from the <code>String</code>
  198. * <code>str</code> starting at position <code>offset</code> in the string.
  199. * These chars will be written to the internal buffer. However, if this
  200. * write operation fills the buffer, the buffer will be flushed to the
  201. * underlying output stream.
  202. *
  203. * @param str The <code>String</code> to write.
  204. * @param offset The index into the string to start writing from.
  205. * @param len The number of chars to write.
  206. *
  207. * @exception IOException If an error occurs
  208. */
  209. public void write (String str, int offset, int len) throws IOException
  210. {
  211. synchronized (lock)
  212. {
  213. if (buffer == null)
  214. throw new IOException ("Stream closed");
  215. if (count + len > buffer.length)
  216. {
  217. localFlush ();
  218. out.write(str, offset, len);
  219. }
  220. else
  221. {
  222. str.getChars(offset, offset + len, buffer, count);
  223. count += len;
  224. if (count == buffer.length)
  225. localFlush ();
  226. }
  227. }
  228. }
  229. // This should only be called with the lock held.
  230. private void localFlush () throws IOException
  231. {
  232. if (count > 0)
  233. {
  234. out.write(buffer, 0, count);
  235. count = 0;
  236. }
  237. }
  238. }