Reader.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Reader.java -- base class of classes that read input as a stream of chars
  2. Copyright (C) 1998, 1999, 2000, 2003, 2004, 2005 Free Software Foundation
  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. import java.nio.CharBuffer;
  33. /* Written using "Java Class Libraries", 2nd edition, plus online
  34. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  35. * Status: Believed complete and correct.
  36. */
  37. /**
  38. * This abstract class forms the base of the hierarchy of classes that read
  39. * input as a stream of characters. It provides a common set of methods for
  40. * reading characters from streams. Subclasses implement and extend these
  41. * methods to read characters from a particular input source such as a file
  42. * or network connection.
  43. *
  44. * @author Per Bothner (bothner@cygnus.com)
  45. * @date April 21, 1998.
  46. * @author Aaron M. Renn (arenn@urbanophile.com)
  47. */
  48. public abstract class Reader implements Closeable, Readable
  49. {
  50. /**
  51. * This is the <code>Object</code> used for synchronizing critical code
  52. * sections. Subclasses should use this variable instead of a
  53. * synchronized method or an explicit synchronization on <code>this</code>
  54. */
  55. protected Object lock;
  56. /**
  57. * Unitializes a <code>Reader</code> that will use the object
  58. * itself for synchronization of critical code sections.
  59. */
  60. protected Reader()
  61. {
  62. this.lock = this;
  63. }
  64. /**
  65. * Initializes a <code>Reader</code> that will use the specified
  66. * <code>Object</code> for synchronization of critical code sections.
  67. *
  68. * @param lock The <code>Object</code> to use for synchronization
  69. */
  70. protected Reader(Object lock)
  71. {
  72. this.lock = lock;
  73. }
  74. /**
  75. * Read chars from a stream and stores them into a caller
  76. * supplied buffer. It starts storing the data at index <code>offset</code>
  77. * into the buffer and attempts to read <code>len</code> chars. This method
  78. * can return before reading the number of chars requested. The actual
  79. * number of chars read is returned as an int. A -1 is returned to indicate
  80. * the end of the stream.
  81. * <p>
  82. * This method will block until some data can be read.
  83. * <p>
  84. * This method operates by calling the single char <code>read()</code> method
  85. * in a loop until the desired number of chars are read. The read loop
  86. * stops short if the end of the stream is encountered or if an IOException
  87. * is encountered on any read operation except the first. If the first
  88. * attempt to read a chars fails, the IOException is allowed to propagate
  89. * upward. And subsequent IOException is caught and treated identically
  90. * to an end of stream condition. Subclasses can (and should if possible)
  91. * override this method to provide a more efficient implementation.
  92. *
  93. * @param buf The array into which the chars read should be stored
  94. * @param offset The offset into the array to start storing chars
  95. * @param count The requested number of chars to read
  96. *
  97. * @return The actual number of chars read, or -1 if end of stream.
  98. *
  99. * @exception IOException If an error occurs.
  100. */
  101. public abstract int read(char buf[], int offset, int count)
  102. throws IOException;
  103. /**
  104. * Reads chars from a stream and stores them into a caller
  105. * supplied buffer. This method attempts to completely fill the buffer,
  106. * but can return before doing so. The actual number of chars read is
  107. * returned as an int. A -1 is returned to indicate the end of the stream.
  108. * <p>
  109. * This method will block until some data can be read.
  110. * <p>
  111. * This method operates by calling an overloaded read method like so:
  112. * <code>read(buf, 0, buf.length)</code>
  113. *
  114. * @param buf The buffer into which the chars read will be stored.
  115. *
  116. * @return The number of chars read or -1 if end of stream.
  117. *
  118. * @exception IOException If an error occurs.
  119. */
  120. public int read(char buf[]) throws IOException
  121. {
  122. return read(buf, 0, buf.length);
  123. }
  124. /**
  125. * Reads an char from the input stream and returns it
  126. * as an int in the range of 0-65535. This method also will return -1 if
  127. * the end of the stream has been reached.
  128. * <p>
  129. * This method will block until the char can be read.
  130. *
  131. * @return The char read or -1 if end of stream
  132. *
  133. * @exception IOException If an error occurs
  134. */
  135. public int read() throws IOException
  136. {
  137. char[] buf = new char[1];
  138. int count = read(buf, 0, 1);
  139. return count > 0 ? buf[0] : -1;
  140. }
  141. /** @since 1.5 */
  142. public int read(CharBuffer buffer) throws IOException
  143. {
  144. // We want to call put(), so we don't manipulate the CharBuffer
  145. // directly.
  146. int rem = buffer.remaining();
  147. char[] buf = new char[rem];
  148. int result = read(buf, 0, rem);
  149. if (result != -1)
  150. buffer.put(buf, 0, result);
  151. return result;
  152. }
  153. /**
  154. * Closes the stream. Any futher attempts to read from the
  155. * stream may generate an <code>IOException</code>.
  156. *
  157. * @exception IOException If an error occurs
  158. */
  159. public abstract void close() throws IOException;
  160. /**
  161. * Returns a boolean that indicates whether the mark/reset
  162. * methods are supported in this class. Those methods can be used to
  163. * remember a specific point in the stream and reset the stream to that
  164. * point.
  165. * <p>
  166. * This method always returns <code>false</code> in this class, but
  167. * subclasses can override this method to return <code>true</code> if they
  168. * support mark/reset functionality.
  169. *
  170. * @return <code>true</code> if mark/reset functionality is supported,
  171. * <code>false</code> otherwise
  172. *
  173. */
  174. public boolean markSupported()
  175. {
  176. return false;
  177. }
  178. /**
  179. * Marks a position in the input to which the stream can be
  180. * "reset" by calling the <code>reset()</code> method. The parameter
  181. * <code>readlimit</code> is the number of chars that can be read from the
  182. * stream after setting the mark before the mark becomes invalid. For
  183. * example, if <code>mark()</code> is called with a read limit of 10, then
  184. * when 11 chars of data are read from the stream before the
  185. * <code>reset()</code> method is called, then the mark is invalid and the
  186. * stream object instance is not required to remember the mark.
  187. *
  188. * @param readLimit The number of chars that can be read before the mark
  189. * becomes invalid
  190. *
  191. * @exception IOException If an error occurs such as mark not being
  192. * supported for this class
  193. */
  194. public void mark(int readLimit) throws IOException
  195. {
  196. throw new IOException("mark not supported");
  197. }
  198. /**
  199. * Resets a stream to the point where the <code>mark()</code>
  200. * method was called. Any chars that were read after the mark point was
  201. * set will be re-read during subsequent reads.
  202. * <p>
  203. * This method always throws an IOException in this class, but subclasses
  204. * can override this method if they provide mark/reset functionality.
  205. *
  206. * @exception IOException Always thrown for this class
  207. */
  208. public void reset() throws IOException
  209. {
  210. throw new IOException("reset not supported");
  211. }
  212. /**
  213. * Determines whether or not this stream is ready to be
  214. * read. If it returns <code>false</code> the stream may block if a
  215. * read is attempted, but it is not guaranteed to do so.
  216. * <p>
  217. * This method always returns <code>false</code> in this class
  218. *
  219. * @return <code>true</code> if the stream is ready to be read,
  220. * <code>false</code> otherwise.
  221. *
  222. * @exception IOException If an error occurs
  223. */
  224. public boolean ready() throws IOException
  225. {
  226. return false;
  227. }
  228. /**
  229. * Skips the specified number of chars in the stream. It
  230. * returns the actual number of chars skipped, which may be less than the
  231. * requested amount.
  232. * <p>
  233. * This method reads and discards chars into a 256 char array until the
  234. * specified number of chars were skipped or until either the end of stream
  235. * is reached or a read attempt returns a short count. Subclasses can
  236. * override this method to provide a more efficient implementation where
  237. * one exists.
  238. *
  239. * @param count The requested number of chars to skip
  240. *
  241. * @return The actual number of chars skipped.
  242. *
  243. * @exception IOException If an error occurs
  244. */
  245. public long skip(long count) throws IOException
  246. {
  247. if (count <= 0)
  248. return 0;
  249. int bsize = count > 1024 ? 1024 : (int) count;
  250. char[] buffer = new char[bsize];
  251. long todo = count;
  252. synchronized (lock)
  253. {
  254. while (todo > 0)
  255. {
  256. int skipped = read(buffer, 0, bsize > todo ? (int) todo : bsize);
  257. if (skipped <= 0)
  258. break;
  259. todo -= skipped;
  260. }
  261. }
  262. return count - todo;
  263. }
  264. }