PushbackInputStream.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* PushbackInputStream.java -- An input stream that can unread bytes
  2. Copyright (C) 1998, 1999, 2001, 2002, 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>FilterInputStream</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 byte, but this can be overridden
  40. * by the creator of the stream.
  41. * <p>
  42. *
  43. * @author Aaron M. Renn (arenn@urbanophile.com)
  44. * @author Warren Levy (warrenl@cygnus.com)
  45. */
  46. public class PushbackInputStream extends FilterInputStream
  47. {
  48. /**
  49. * This is the default buffer size
  50. */
  51. private static final int DEFAULT_BUFFER_SIZE = 1;
  52. /**
  53. * This is the buffer that is used to store the pushed back data
  54. */
  55. protected byte[] buf;
  56. /**
  57. * This is the position in the buffer from which the next byte will be
  58. * read. Bytes are stored in reverse order in the buffer, starting from
  59. * <code>buf[buf.length - 1]</code> to <code>buf[0]</code>. Thus when
  60. * <code>pos</code> is 0 the buffer is full and <code>buf.length</code> when
  61. * it is empty
  62. */
  63. protected int pos;
  64. /**
  65. * This method initializes a <code>PushbackInputStream</code> to
  66. * read from the specified subordinate <code>InputStream</code>
  67. * with a default pushback buffer size of 1.
  68. *
  69. * @param in The subordinate stream to read from
  70. */
  71. public PushbackInputStream(InputStream in)
  72. {
  73. this(in, DEFAULT_BUFFER_SIZE);
  74. }
  75. /**
  76. * This method initializes a <code>PushbackInputStream</code> to
  77. * read from the specified subordinate <code>InputStream</code> with
  78. * the specified buffer size
  79. *
  80. * @param in The subordinate <code>InputStream</code> to read from
  81. * @param size The pushback buffer size to use
  82. */
  83. public PushbackInputStream(InputStream in, int size)
  84. {
  85. super(in);
  86. if (size < 0)
  87. throw new IllegalArgumentException();
  88. buf = new byte[size];
  89. pos = buf.length;
  90. }
  91. /**
  92. * This method returns the number of bytes that can be read from this
  93. * stream before a read can block. A return of 0 indicates that blocking
  94. * might (or might not) occur on the very next read attempt.
  95. * <p>
  96. * This method will return the number of bytes available from the
  97. * pushback buffer plus the number of bytes available from the
  98. * underlying stream.
  99. *
  100. * @return The number of bytes that can be read before blocking could occur
  101. *
  102. * @exception IOException If an error occurs
  103. */
  104. public int available() throws IOException
  105. {
  106. try
  107. {
  108. return (buf.length - pos) + super.available();
  109. }
  110. catch (NullPointerException npe)
  111. {
  112. throw new IOException ("Stream closed");
  113. }
  114. }
  115. /**
  116. * This method closes the stream and releases any associated resources.
  117. *
  118. * @exception IOException If an error occurs.
  119. */
  120. public synchronized void close() throws IOException
  121. {
  122. buf = null;
  123. super.close();
  124. }
  125. /**
  126. * This method returns <code>false</code> to indicate that it does
  127. * not support mark/reset functionality.
  128. *
  129. * @return This method returns <code>false</code> to indicate that
  130. * this class does not support mark/reset functionality
  131. */
  132. public boolean markSupported()
  133. {
  134. return false;
  135. }
  136. /**
  137. * This method always throws an IOException in this class because
  138. * mark/reset functionality is not supported.
  139. *
  140. * @exception IOException Always thrown for this class
  141. */
  142. public void reset() throws IOException
  143. {
  144. throw new IOException("Mark not supported in this class");
  145. }
  146. /**
  147. * This method reads an unsigned byte from the input stream and returns it
  148. * as an int in the range of 0-255. This method also will return -1 if
  149. * the end of the stream has been reached. The byte returned will be read
  150. * from the pushback buffer, unless the buffer is empty, in which case
  151. * the byte will be read from the underlying stream.
  152. * <p>
  153. * This method will block until the byte can be read.
  154. *
  155. * @return The byte read or -1 if end of stream
  156. *
  157. * @exception IOException If an error occurs
  158. */
  159. public synchronized int read() throws IOException
  160. {
  161. if (pos < buf.length)
  162. return ((int) buf[pos++]) & 0xFF;
  163. return super.read();
  164. }
  165. /**
  166. * This method read bytes from a stream and stores them into a
  167. * caller supplied buffer. It starts storing the data at index
  168. * <code>offset</code> into the buffer and attempts to read
  169. * <code>len</code> bytes. This method can return before reading the
  170. * number of bytes requested. The actual number of bytes read is
  171. * returned as an int. A -1 is returned to indicate the end of the
  172. * stream.
  173. * <p>
  174. * This method will block until some data can be read.
  175. * <p>
  176. * This method first reads bytes from the pushback buffer in order to
  177. * satisfy the read request. If the pushback buffer cannot provide all
  178. * of the bytes requested, the remaining bytes are read from the
  179. * underlying stream.
  180. *
  181. * @param b The array into which the bytes read should be stored
  182. * @param off The offset into the array to start storing bytes
  183. * @param len The requested number of bytes to read
  184. *
  185. * @return The actual number of bytes read, or -1 if end of stream.
  186. *
  187. * @exception IOException If an error occurs.
  188. */
  189. public synchronized int read(byte[] b, int off, int len) throws IOException
  190. {
  191. int numBytes = Math.min(buf.length - pos, len);
  192. if (numBytes > 0)
  193. {
  194. System.arraycopy (buf, pos, b, off, numBytes);
  195. pos += numBytes;
  196. len -= numBytes;
  197. off += numBytes;
  198. }
  199. if (len > 0)
  200. {
  201. len = super.read(b, off, len);
  202. if (len == -1) //EOF
  203. return numBytes > 0 ? numBytes : -1;
  204. numBytes += len;
  205. }
  206. return numBytes;
  207. }
  208. /**
  209. * This method pushes a single byte of data into the pushback buffer.
  210. * The byte pushed back is the one that will be returned as the first byte
  211. * of the next read.
  212. * <p>
  213. * If the pushback buffer is full, this method throws an exception.
  214. * <p>
  215. * The argument to this method is an <code>int</code>. Only the low
  216. * eight bits of this value are pushed back.
  217. *
  218. * @param b The byte to be pushed back, passed as an int
  219. *
  220. * @exception IOException If the pushback buffer is full.
  221. */
  222. public synchronized void unread(int b) throws IOException
  223. {
  224. if (pos <= 0)
  225. throw new IOException("Insufficient space in pushback buffer");
  226. buf[--pos] = (byte) b;
  227. }
  228. /**
  229. * This method pushes all of the bytes in the passed byte array into
  230. * the pushback bfer. These bytes are pushed in reverse order so that
  231. * the next byte read from the stream after this operation will be
  232. * <code>b[0]</code> followed by <code>b[1]</code>, etc.
  233. * <p>
  234. * If the pushback buffer cannot hold all of the requested bytes, an
  235. * exception is thrown.
  236. *
  237. * @param b The byte array to be pushed back
  238. *
  239. * @exception IOException If the pushback buffer is full
  240. */
  241. public synchronized void unread(byte[] b) throws IOException
  242. {
  243. unread(b, 0, b.length);
  244. }
  245. /**
  246. * This method pushed back bytes from the passed in array into the
  247. * pushback buffer. The bytes from <code>b[offset]</code> to
  248. * <code>b[offset + len]</code> are pushed in reverse order so that
  249. * the next byte read from the stream after this operation will be
  250. * <code>b[offset]</code> followed by <code>b[offset + 1]</code>,
  251. * etc.
  252. * <p>
  253. * If the pushback buffer cannot hold all of the requested bytes, an
  254. * exception is thrown.
  255. *
  256. * @param b The byte array to be pushed back
  257. * @param off The index into the array where the bytes to be push start
  258. * @param len The number of bytes to be pushed.
  259. *
  260. * @exception IOException If the pushback buffer is full
  261. */
  262. public synchronized void unread(byte[] b, int off, int len)
  263. throws IOException
  264. {
  265. if (pos < len)
  266. throw new IOException("Insufficient space in pushback buffer");
  267. // Note the order that these bytes are being added is the opposite
  268. // of what would be done if they were added to the buffer one at a time.
  269. // See the Java Class Libraries book p. 1390.
  270. System.arraycopy(b, off, buf, pos - len, len);
  271. // Don't put this into the arraycopy above, an exception might be thrown
  272. // and in that case we don't want to modify pos.
  273. pos -= len;
  274. }
  275. /**
  276. * This method skips the specified number of bytes in the stream. It
  277. * returns the actual number of bytes skipped, which may be less than the
  278. * requested amount.
  279. * <p>
  280. * This method first discards bytes from the buffer, then calls the
  281. * <code>skip</code> method on the underlying <code>InputStream</code> to
  282. * skip additional bytes if necessary.
  283. *
  284. * @param n The requested number of bytes to skip
  285. *
  286. * @return The actual number of bytes skipped.
  287. *
  288. * @exception IOException If an error occurs
  289. *
  290. * @since 1.2
  291. */
  292. public synchronized long skip(long n) throws IOException
  293. {
  294. final long origN = n;
  295. if (n > 0L)
  296. {
  297. int numread = (int) Math.min((long) (buf.length - pos), n);
  298. pos += numread;
  299. n -= numread;
  300. if (n > 0)
  301. n -= super.skip(n);
  302. }
  303. return origN - n;
  304. }
  305. }