Writer.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Writer.java -- Base class for character output streams
  2. Copyright (C) 1998, 1999, 2001, 2003, 2004, 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. /* Written using "Java Class Libraries", 2nd edition, plus online
  33. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  34. * Status: Believed complete and correct.
  35. */
  36. /**
  37. * This abstract class forms the base of the hierarchy of classes that
  38. * write output as a stream of chars. It provides a common set of methods
  39. * for writing chars to stream. Subclasses implement and/or extend these
  40. * methods to write chars in a particular manner or to a particular
  41. * destination such as a file on disk or network connection.
  42. *
  43. * @author Aaron M. Renn (arenn@urbanophile.com)
  44. * @author Per Bothner (bothner@cygnus.com)
  45. */
  46. public abstract class Writer implements Appendable, Closeable, Flushable
  47. {
  48. /**
  49. * This is the object used to synchronize criticial code sections for
  50. * thread safety. Subclasses should use this field instead of using
  51. * synchronized methods or explicity synchronizations on <code>this</code>
  52. */
  53. protected Object lock;
  54. /**
  55. * This is the default no-argument constructor for this class. This method
  56. * will set up the class to synchronize criticial sections on itself.
  57. */
  58. protected Writer()
  59. {
  60. lock = this;
  61. }
  62. /**
  63. * This method initializes a <code>Writer</code> that will synchronize
  64. * on the specified <code>Object</code>.
  65. *
  66. * @param lock The <code>Object</code> to use for synchronizing critical
  67. * sections. Must not be null.
  68. */
  69. protected Writer(Object lock)
  70. {
  71. if (lock == null)
  72. throw new NullPointerException();
  73. this.lock = lock;
  74. }
  75. /**
  76. * This method forces any data that may have been buffered to be written
  77. * to the underlying output device. Please note that the host environment
  78. * might perform its own buffering unbeknowst to Java. In that case, a
  79. * write made (for example, to a disk drive) might be cached in OS
  80. * buffers instead of actually being written to disk.
  81. *
  82. * @exception IOException If an error occurs
  83. */
  84. public abstract void flush() throws IOException;
  85. /**
  86. * This method closes the stream. Any internal or native resources
  87. * associated
  88. * with this stream are freed. Any subsequent attempt to access the stream
  89. * might throw an exception.
  90. * <p>
  91. * This method in this class does nothing.
  92. *
  93. * @exception IOException If an error occurs
  94. */
  95. public abstract void close() throws IOException;
  96. /**
  97. * This method writes a single char to the output stream.
  98. *
  99. * @param b The char to be written to the output stream, passed as an int
  100. *
  101. * @exception IOException If an error occurs
  102. */
  103. public void write(int b) throws IOException
  104. {
  105. char[] buf = new char[1];
  106. buf[0] = (char)b;
  107. write(buf, 0, buf.length);
  108. }
  109. /**
  110. * This method all the writes char from the passed array to the output
  111. * stream. This method is equivalent to
  112. * <code>write(buf, 0, buf.length)</code> which
  113. * is exactly how it is implemented in this class.
  114. *
  115. * @param buf The array of char to write
  116. *
  117. * @exception IOException If an error occurs
  118. */
  119. public void write(char[] buf) throws IOException
  120. {
  121. write(buf, 0, buf.length);
  122. }
  123. /**
  124. * This method writes <code>len</code> char from the specified array
  125. * <code>buf</code> starting at index <code>offset</code> into the array.
  126. * <p>
  127. * Subclasses must provide an implementation of this abstract method.
  128. *
  129. * @param buf The array of char to write from
  130. * @param offset The index into the array to start writing from
  131. * @param len The number of char to write
  132. *
  133. * @exception IOException If an error occurs
  134. */
  135. public abstract void write(char[] buf, int offset, int len)
  136. throws IOException;
  137. /**
  138. * This method writes all the characters in a <code>String</code> to the
  139. * output.
  140. *
  141. * @param str The <code>String</code> whose chars are to be written.
  142. *
  143. * @exception IOException If an error occurs
  144. */
  145. public void write(String str) throws IOException
  146. {
  147. write(str, 0, str.length());
  148. }
  149. /**
  150. * This method writes <code>len</code> chars from the <code>String</code>
  151. * starting at position <code>offset</code>.
  152. *
  153. * @param str The <code>String</code> that is to be written
  154. * @param offset The character offset into the <code>String</code> to start
  155. * writing from
  156. * @param len The number of chars to write
  157. *
  158. * @exception IOException If an error occurs
  159. */
  160. public void write(String str, int offset, int len) throws IOException
  161. {
  162. // FIXME - for libgcj re-write using native code to not require
  163. // copied buffer.
  164. char[] buf = new char[len];
  165. str.getChars(offset, offset + len, buf, 0);
  166. write(buf, 0, len);
  167. }
  168. /** @since 1.5 */
  169. public Writer append(char c) throws IOException
  170. {
  171. write(c);
  172. return this;
  173. }
  174. /** @since 1.5 */
  175. public Writer append(CharSequence cs) throws IOException
  176. {
  177. write(cs == null ? "null" : cs.toString());
  178. return this;
  179. }
  180. /** @since 1.5 */
  181. public Writer append(CharSequence cs, int start, int end) throws IOException
  182. {
  183. write(cs == null ? "null" : cs.subSequence(start, end).toString());
  184. return this;
  185. }
  186. }