PushbackReader.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* PushbackReader.java -- An character stream that can unread chars
  2. Copyright (C) 1998, 2000, 2001, 2003, 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. /**
  33. * This subclass of <code>FilterReader</code> provides the ability to
  34. * unread data from a stream. It maintains an internal buffer of unread
  35. * data that is supplied to the next read operation. This is conceptually
  36. * similar to mark/reset functionality, except that in this case the
  37. * position to reset the stream to does not need to be known in advance.
  38. * <p>
  39. * The default pushback buffer size one char, but this can be overridden
  40. * by the creator of the stream.
  41. *
  42. * @author Aaron M. Renn (arenn@urbanophile.com)
  43. * @author Warren Levy (warrenl@cygnus.com)
  44. */
  45. public class PushbackReader extends FilterReader
  46. {
  47. /**
  48. * This is the default buffer size
  49. */
  50. private static final int DEFAULT_BUFFER_SIZE = 1;
  51. /**
  52. * This is the buffer that is used to store the pushed back data
  53. */
  54. private char[] buf;
  55. /**
  56. * This is the position in the buffer from which the next char will be
  57. * read. Bytes are stored in reverse order in the buffer, starting from
  58. * <code>buf[buf.length - 1]</code> to <code>buf[0]</code>. Thus when
  59. * <code>pos</code> is 0 the buffer is full and <code>buf.length</code> when
  60. * it is empty
  61. */
  62. private int pos;
  63. /**
  64. * This method initializes a <code>PushbackReader</code> to read from the
  65. * specified subordinate <code>Reader</code> with a default pushback buffer
  66. * size of 1.
  67. *
  68. * @param in The subordinate stream to read from
  69. */
  70. public PushbackReader(Reader in)
  71. {
  72. this(in, DEFAULT_BUFFER_SIZE);
  73. }
  74. /**
  75. * This method initializes a <code>PushbackReader</code> to read from the
  76. * specified subordinate <code>Reader</code> with the specified buffer
  77. * size
  78. *
  79. * @param in The subordinate <code>Reader</code> to read from
  80. * @param bufsize The pushback buffer size to use
  81. */
  82. public PushbackReader(Reader in, int bufsize)
  83. {
  84. super(in);
  85. if (bufsize < 0)
  86. throw new IllegalArgumentException("buffer size must be positive");
  87. buf = new char[bufsize];
  88. pos = bufsize;
  89. }
  90. /**
  91. * This method closes the stream and frees any associated resources.
  92. *
  93. * @exception IOException If an error occurs.
  94. */
  95. public void close() throws IOException
  96. {
  97. synchronized (lock)
  98. {
  99. buf = null;
  100. super.close();
  101. }
  102. }
  103. /**
  104. * This method throws an exception when called since this class does
  105. * not support mark/reset.
  106. *
  107. * @param read_limit Not used.
  108. *
  109. * @exception IOException Always thrown to indicate mark/reset not supported.
  110. */
  111. public void mark(int read_limit) throws IOException
  112. {
  113. throw new IOException("mark not supported in this class");
  114. }
  115. /**
  116. * This method returns <code>false</code> to indicate that it does not support
  117. * mark/reset functionality.
  118. *
  119. * @return This method returns <code>false</code> to indicate that this
  120. * class does not support mark/reset functionality
  121. *
  122. */
  123. public boolean markSupported()
  124. {
  125. return(false);
  126. }
  127. /**
  128. * This method always throws an IOException in this class because
  129. * mark/reset functionality is not supported.
  130. *
  131. * @exception IOException Always thrown for this class
  132. */
  133. public void reset() throws IOException
  134. {
  135. throw new IOException("reset not supported in this class");
  136. }
  137. /**
  138. * This method determines whether or not this stream is ready to be read.
  139. * If it returns <code>false</code> to indicate that the stream is not
  140. * ready, any attempt to read from the stream could (but is not
  141. * guaranteed to) block.
  142. * <p>
  143. * This stream is ready to read if there are either chars waiting to be
  144. * read in the pushback buffer or if the underlying stream is ready to
  145. * be read.
  146. *
  147. * @return <code>true</code> if this stream is ready to be read,
  148. * <code>false</code> otherwise
  149. *
  150. * @exception IOException If an error occurs
  151. */
  152. public boolean ready() throws IOException
  153. {
  154. synchronized (lock)
  155. {
  156. if (buf == null)
  157. throw new IOException ("stream closed");
  158. if (((buf.length - pos) > 0) || super.ready())
  159. return(true);
  160. else
  161. return(false);
  162. }
  163. }
  164. // Don't delete this method just because the spec says it shouldn't be there!
  165. // See the CVS log for details.
  166. /**
  167. * This method skips the specified number of chars in the stream. It
  168. * returns the actual number of chars skipped, which may be less than the
  169. * requested amount.
  170. * <p>
  171. * This method first discards chars from the buffer, then calls the
  172. * <code>skip</code> method on the underlying <code>Reader</code> to
  173. * skip additional chars if necessary.
  174. *
  175. * @param num_chars The requested number of chars to skip
  176. *
  177. * @return The actual number of chars skipped.
  178. *
  179. * @exception IOException If an error occurs
  180. */
  181. public long skip(long num_chars) throws IOException
  182. {
  183. synchronized (lock)
  184. {
  185. if (num_chars <= 0)
  186. return(0);
  187. if ((buf.length - pos) >= num_chars)
  188. {
  189. pos += num_chars;
  190. return(num_chars);
  191. }
  192. int chars_discarded = buf.length - pos;
  193. pos = buf.length;
  194. long chars_skipped = in.skip(num_chars - chars_discarded);
  195. return(chars_discarded + chars_skipped);
  196. }
  197. }
  198. /**
  199. * This method reads an unsigned char from the input stream and returns it
  200. * as an int in the range of 0-65535. This method also will return -1 if
  201. * the end of the stream has been reached. The char returned will be read
  202. * from the pushback buffer, unless the buffer is empty, in which case
  203. * the char will be read from the underlying stream.
  204. * <p>
  205. * This method will block until the char can be read.
  206. *
  207. * @return The char read or -1 if end of stream
  208. *
  209. * @exception IOException If an error occurs
  210. */
  211. public int read() throws IOException
  212. {
  213. synchronized (lock)
  214. {
  215. if (buf == null)
  216. throw new IOException("stream closed");
  217. if (pos == buf.length)
  218. return(super.read());
  219. ++pos;
  220. return((buf[pos - 1] & 0xFFFF));
  221. }
  222. }
  223. /**
  224. * This method read chars from a stream and stores them into a caller
  225. * supplied buffer. It starts storing the data at index <code>offset</code>
  226. * into
  227. * the buffer and attempts to read <code>len</code> chars. This method can
  228. * return before reading the number of chars requested. The actual number
  229. * of chars read is returned as an int. A -1 is returned to indicate the
  230. * end of the stream.
  231. * <p>
  232. * This method will block until some data can be read.
  233. * <p>
  234. * This method first reads chars from the pushback buffer in order to
  235. * satisfy the read request. If the pushback buffer cannot provide all
  236. * of the chars requested, the remaining chars are read from the
  237. * underlying stream.
  238. *
  239. * @param buffer The array into which the chars read should be stored
  240. * @param offset The offset into the array to start storing chars
  241. * @param length The requested number of chars to read
  242. *
  243. * @return The actual number of chars read, or -1 if end of stream.
  244. *
  245. * @exception IOException If an error occurs.
  246. */
  247. public synchronized int read(char[] buffer, int offset, int length)
  248. throws IOException
  249. {
  250. synchronized (lock)
  251. {
  252. if (buf == null)
  253. throw new IOException("stream closed");
  254. if (offset < 0 || length < 0 || offset + length > buffer.length)
  255. throw new ArrayIndexOutOfBoundsException();
  256. int numBytes = Math.min(buf.length - pos, length);
  257. if (numBytes > 0)
  258. {
  259. System.arraycopy (buf, pos, buffer, offset, numBytes);
  260. pos += numBytes;
  261. return numBytes;
  262. }
  263. return super.read(buffer, offset, length);
  264. }
  265. }
  266. /**
  267. * This method pushes a single char of data into the pushback buffer.
  268. * The char pushed back is the one that will be returned as the first char
  269. * of the next read.
  270. * <p>
  271. * If the pushback buffer is full, this method throws an exception.
  272. * <p>
  273. * The argument to this method is an <code>int</code>. Only the low eight
  274. * bits of this value are pushed back.
  275. *
  276. * @param b The char to be pushed back, passed as an int
  277. *
  278. * @exception IOException If the pushback buffer is full.
  279. */
  280. public void unread(int b) throws IOException
  281. {
  282. synchronized (lock)
  283. {
  284. if (buf == null)
  285. throw new IOException("stream closed");
  286. if (pos == 0)
  287. throw new IOException("Pushback buffer is full");
  288. --pos;
  289. buf[pos] = (char)(b & 0xFFFF);
  290. }
  291. }
  292. /**
  293. * This method pushes all of the chars in the passed char array into
  294. * the pushback buffer. These chars are pushed in reverse order so that
  295. * the next char read from the stream after this operation will be
  296. * <code>buf[0]</code> followed by <code>buf[1]</code>, etc.
  297. * <p>
  298. * If the pushback buffer cannot hold all of the requested chars, an
  299. * exception is thrown.
  300. *
  301. * @param buf The char array to be pushed back
  302. *
  303. * @exception IOException If the pushback buffer is full
  304. */
  305. public synchronized void unread(char[] buf) throws IOException
  306. {
  307. unread(buf, 0, buf.length);
  308. }
  309. /**
  310. * This method pushed back chars from the passed in array into the pushback
  311. * buffer. The chars from <code>buf[offset]</code> to
  312. * <code>buf[offset + len]</code>
  313. * are pushed in reverse order so that the next char read from the stream
  314. * after this operation will be <code>buf[offset]</code> followed by
  315. * <code>buf[offset + 1]</code>, etc.
  316. * <p>
  317. * If the pushback buffer cannot hold all of the requested chars, an
  318. * exception is thrown.
  319. *
  320. * @param buffer The char array to be pushed back
  321. * @param offset The index into the array where the chars to be push start
  322. * @param length The number of chars to be pushed.
  323. *
  324. * @exception IOException If the pushback buffer is full
  325. */
  326. public synchronized void unread(char[] buffer, int offset, int length)
  327. throws IOException
  328. {
  329. synchronized (lock)
  330. {
  331. if (buf == null)
  332. throw new IOException("stream closed");
  333. if (pos < length)
  334. throw new IOException("Pushback buffer is full");
  335. // Note the order that these chars are being added is the opposite
  336. // of what would be done if they were added to the buffer one at a time.
  337. // See the Java Class Libraries book p. 1397.
  338. System.arraycopy(buffer, offset, buf, pos - length, length);
  339. // Don't put this into the arraycopy above, an exception might be thrown
  340. // and in that case we don't want to modify pos.
  341. pos -= length;
  342. }
  343. }
  344. }