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