1. /*
  2. * @(#)Writer.java 1.19 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. * Abstract class for writing to character streams. The only methods that a
  13. * subclass must implement are write(char[], int, int), flush(), and close().
  14. * Most subclasses, however, will override some of the methods defined here in
  15. * order to provide higher efficiency, additional functionality, or both.
  16. *
  17. * @see Writer
  18. * @see BufferedWriter
  19. * @see CharArrayWriter
  20. * @see FilterWriter
  21. * @see OutputStreamWriter
  22. * @see FileWriter
  23. * @see PipedWriter
  24. * @see PrintWriter
  25. * @see StringWriter
  26. * @see Reader
  27. *
  28. * @version 1.19, 00/02/02
  29. * @author Mark Reinhold
  30. * @since JDK1.1
  31. */
  32. public abstract class Writer {
  33. /**
  34. * Temporary buffer used to hold writes of strings and single characters
  35. */
  36. private char[] writeBuffer;
  37. /**
  38. * Size of writeBuffer, must be >= 1
  39. */
  40. private final int writeBufferSize = 1024;
  41. /**
  42. * The object used to synchronize operations on this stream. For
  43. * efficiency, a character-stream object may use an object other than
  44. * itself to protect critical sections. A subclass should therefore use
  45. * the object in this field rather than <tt>this</tt> or a synchronized
  46. * method.
  47. */
  48. protected Object lock;
  49. /**
  50. * Create a new character-stream writer whose critical sections will
  51. * synchronize on the writer itself.
  52. */
  53. protected Writer() {
  54. this.lock = this;
  55. }
  56. /**
  57. * Create a new character-stream writer whose critical sections will
  58. * synchronize on the given object.
  59. *
  60. * @param lock Object to synchronize on.
  61. */
  62. protected Writer(Object lock) {
  63. if (lock == null) {
  64. throw new NullPointerException();
  65. }
  66. this.lock = lock;
  67. }
  68. /**
  69. * Write a single character. The character to be written is contained in
  70. * the 16 low-order bits of the given integer value; the 16 high-order bits
  71. * are ignored.
  72. *
  73. * <p> Subclasses that intend to support efficient single-character output
  74. * should override this method.
  75. *
  76. * @param c int specifying a character to be written.
  77. * @exception IOException If an I/O error occurs
  78. */
  79. public void write(int c) throws IOException {
  80. synchronized (lock) {
  81. if (writeBuffer == null){
  82. writeBuffer = new char[writeBufferSize];
  83. }
  84. writeBuffer[0] = (char) c;
  85. write(writeBuffer, 0, 1);
  86. }
  87. }
  88. /**
  89. * Write an array of characters.
  90. *
  91. * @param cbuf Array of characters to be written
  92. *
  93. * @exception IOException If an I/O error occurs
  94. */
  95. public void write(char cbuf[]) throws IOException {
  96. write(cbuf, 0, cbuf.length);
  97. }
  98. /**
  99. * Write a portion of an array of characters.
  100. *
  101. * @param cbuf Array of characters
  102. * @param off Offset from which to start writing characters
  103. * @param len Number of characters to write
  104. *
  105. * @exception IOException If an I/O error occurs
  106. */
  107. abstract public void write(char cbuf[], int off, int len) throws IOException;
  108. /**
  109. * Write a string.
  110. *
  111. * @param str String to be written
  112. *
  113. * @exception IOException If an I/O error occurs
  114. */
  115. public void write(String str) throws IOException {
  116. write(str, 0, str.length());
  117. }
  118. /**
  119. * Write a portion of a string.
  120. *
  121. * @param str A String
  122. * @param off Offset from which to start writing characters
  123. * @param len Number of characters to write
  124. *
  125. * @exception IOException If an I/O error occurs
  126. */
  127. public void write(String str, int off, int len) throws IOException {
  128. synchronized (lock) {
  129. char cbuf[];
  130. if (len <= writeBufferSize) {
  131. if (writeBuffer == null) {
  132. writeBuffer = new char[writeBufferSize];
  133. }
  134. cbuf = writeBuffer;
  135. } else { // Don't permanently allocate very large buffers.
  136. cbuf = new char[len];
  137. }
  138. str.getChars(off, (off + len), cbuf, 0);
  139. write(cbuf, 0, len);
  140. }
  141. }
  142. /**
  143. * Flush the stream. If the stream has saved any characters from the
  144. * various write() methods in a buffer, write them immediately to their
  145. * intended destination. Then, if that destination is another character or
  146. * byte stream, flush it. Thus one flush() invocation will flush all the
  147. * buffers in a chain of Writers and OutputStreams.
  148. *
  149. * @exception IOException If an I/O error occurs
  150. */
  151. abstract public void flush() throws IOException;
  152. /**
  153. * Close the stream, flushing it first. Once a stream has been closed,
  154. * further write() or flush() invocations will cause an IOException to be
  155. * thrown. Closing a previously-closed stream, however, has no effect.
  156. *
  157. * @exception IOException If an I/O error occurs
  158. */
  159. abstract public void close() throws IOException;
  160. }