1. /*
  2. * @(#)Reader.java 1.21 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 reading character streams. The only methods that a
  13. * subclass must implement are read(char[], int, int) and close(). Most
  14. * subclasses, however, will override some of the methods defined here in order
  15. * to provide higher efficiency, additional functionality, or both.
  16. *
  17. *
  18. * @see BufferedReader
  19. * @see LineNumberReader
  20. * @see CharArrayReader
  21. * @see InputStreamReader
  22. * @see FileReader
  23. * @see FilterReader
  24. * @see PushbackReader
  25. * @see PipedReader
  26. * @see StringReader
  27. * @see Writer
  28. *
  29. * @version 1.21, 00/02/02
  30. * @author Mark Reinhold
  31. * @since JDK1.1
  32. */
  33. public abstract class Reader {
  34. /**
  35. * The object used to synchronize operations on this stream. For
  36. * efficiency, a character-stream object may use an object other than
  37. * itself to protect critical sections. A subclass should therefore use
  38. * the object in this field rather than <tt>this</tt> or a synchronized
  39. * method.
  40. */
  41. protected Object lock;
  42. /**
  43. * Create a new character-stream reader whose critical sections will
  44. * synchronize on the reader itself.
  45. */
  46. protected Reader() {
  47. this.lock = this;
  48. }
  49. /**
  50. * Create a new character-stream reader whose critical sections will
  51. * synchronize on the given object.
  52. *
  53. * @param lock The Object to synchronize on.
  54. */
  55. protected Reader(Object lock) {
  56. if (lock == null) {
  57. throw new NullPointerException();
  58. }
  59. this.lock = lock;
  60. }
  61. /**
  62. * Read a single character. This method will block until a character is
  63. * available, an I/O error occurs, or the end of the stream is reached.
  64. *
  65. * <p> Subclasses that intend to support efficient single-character input
  66. * should override this method.
  67. *
  68. * @return The character read, as an integer in the range 0 to 65535
  69. * (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
  70. * been reached
  71. *
  72. * @exception IOException If an I/O error occurs
  73. */
  74. public int read() throws IOException {
  75. char cb[] = new char[1];
  76. if (read(cb, 0, 1) == -1)
  77. return -1;
  78. else
  79. return cb[0];
  80. }
  81. /**
  82. * Read characters into an array. This method will block until some input
  83. * is available, an I/O error occurs, or the end of the stream is reached.
  84. *
  85. * @param cbuf Destination buffer
  86. *
  87. * @return The number of bytes read, or -1 if the end of the stream
  88. * has been reached
  89. *
  90. * @exception IOException If an I/O error occurs
  91. */
  92. public int read(char cbuf[]) throws IOException {
  93. return read(cbuf, 0, cbuf.length);
  94. }
  95. /**
  96. * Read characters into a portion of an array. This method will block
  97. * until some input is available, an I/O error occurs, or the end of the
  98. * stream is reached.
  99. *
  100. * @param cbuf Destination buffer
  101. * @param off Offset at which to start storing characters
  102. * @param len Maximum number of characters to read
  103. *
  104. * @return The number of characters read, or -1 if the end of the
  105. * stream has been reached
  106. *
  107. * @exception IOException If an I/O error occurs
  108. */
  109. abstract public int read(char cbuf[], int off, int len) throws IOException;
  110. /** Maximum skip-buffer size */
  111. private static final int maxSkipBufferSize = 8192;
  112. /** Skip buffer, null until allocated */
  113. private char skipBuffer[] = null;
  114. /**
  115. * Skip characters. This method will block until some characters are
  116. * available, an I/O error occurs, or the end of the stream is reached.
  117. *
  118. * @param n The number of characters to skip
  119. *
  120. * @return The number of characters actually skipped
  121. *
  122. * @exception IllegalArgumentException If <code>n</code> is negative.
  123. * @exception IOException If an I/O error occurs
  124. */
  125. public long skip(long n) throws IOException {
  126. if (n < 0L)
  127. throw new IllegalArgumentException("skip value is negative");
  128. int nn = (int) Math.min(n, maxSkipBufferSize);
  129. synchronized (lock) {
  130. if ((skipBuffer == null) || (skipBuffer.length < nn))
  131. skipBuffer = new char[nn];
  132. long r = n;
  133. while (r > 0) {
  134. int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
  135. if (nc == -1)
  136. break;
  137. r -= nc;
  138. }
  139. return n - r;
  140. }
  141. }
  142. /**
  143. * Tell whether this stream is ready to be read.
  144. *
  145. * @return True if the next read() is guaranteed not to block for input,
  146. * false otherwise. Note that returning false does not guarantee that the
  147. * next read will block.
  148. *
  149. * @exception IOException If an I/O error occurs
  150. */
  151. public boolean ready() throws IOException {
  152. return false;
  153. }
  154. /**
  155. * Tell whether this stream supports the mark() operation. The default
  156. * implementation always returns false. Subclasses should override this
  157. * method.
  158. *
  159. * @return true if and only if this stream supports the mark operation.
  160. */
  161. public boolean markSupported() {
  162. return false;
  163. }
  164. /**
  165. * Mark the present position in the stream. Subsequent calls to reset()
  166. * will attempt to reposition the stream to this point. Not all
  167. * character-input streams support the mark() operation.
  168. *
  169. * @param readAheadLimit Limit on the number of characters that may be
  170. * read while still preserving the mark. After
  171. * reading this many characters, attempting to
  172. * reset the stream may fail.
  173. *
  174. * @exception IOException If the stream does not support mark(),
  175. * or if some other I/O error occurs
  176. */
  177. public void mark(int readAheadLimit) throws IOException {
  178. throw new IOException("mark() not supported");
  179. }
  180. /**
  181. * Reset the stream. If the stream has been marked, then attempt to
  182. * reposition it at the mark. If the stream has not been marked, then
  183. * attempt to reset it in some way appropriate to the particular stream,
  184. * for example by repositioning it to its starting point. Not all
  185. * character-input streams support the reset() operation, and some support
  186. * reset() without supporting mark().
  187. *
  188. * @exception IOException If the stream has not been marked,
  189. * or if the mark has been invalidated,
  190. * or if the stream does not support reset(),
  191. * or if some other I/O error occurs
  192. */
  193. public void reset() throws IOException {
  194. throw new IOException("reset() not supported");
  195. }
  196. /**
  197. * Close the stream. Once a stream has been closed, further read(),
  198. * ready(), mark(), or reset() invocations will throw an IOException.
  199. * Closing a previously-closed stream, however, has no effect.
  200. *
  201. * @exception IOException If an I/O error occurs
  202. */
  203. abstract public void close() throws IOException;
  204. }