1 /* PushbackInputStream.java -- An input stream that can unread bytes
2 Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 * This subclass of <code>FilterInputStream</code> provides the ability to
42 * unread data from a stream. It maintains an internal buffer of unread data
43 * that is supplied to the next read operation. This is conceptually similar to
44 * mark/reset functionality, except that in this case the position to reset the
45 * stream to does not need to be known in advance.
47 * The default pushback buffer size one byte, but this can be overridden by the
48 * creator of the stream.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Warren Levy (warrenl@cygnus.com)
54 @LATTICE("IN<T,IN<POS,POS<SH,SH<F,SH*,POS*")
55 @METHODDEFAULT("OUT<SH,SH<IN,SH*,THISLOC=OUT,GLOBALLOC=OUT")
56 public class PushbackInputStream extends FilterInputStream {
58 * This is the default buffer size
61 private static final int DEFAULT_BUFFER_SIZE = 1;
64 * This is the buffer that is used to store the pushed back data
70 * This is the position in the buffer from which the next byte will be read.
71 * Bytes are stored in reverse order in the buffer, starting from
72 * <code>buf[buf.length - 1]</code> to <code>buf[0]</code>. Thus when
73 * <code>pos</code> is 0 the buffer is full and <code>buf.length</code> when
80 * This method initializes a <code>PushbackInputStream</code> to read from the
81 * specified subordinate <code>InputStream</code> with a default pushback
85 * The subordinate stream to read from
87 public PushbackInputStream(InputStream in) {
88 this(in, DEFAULT_BUFFER_SIZE);
92 * This method initializes a <code>PushbackInputStream</code> to read from the
93 * specified subordinate <code>InputStream</code> with the specified buffer
97 * The subordinate <code>InputStream</code> to read from
99 * The pushback buffer size to use
101 public PushbackInputStream(@LOC("IN") InputStream in, @LOC("IN") int size) {
104 throw new IllegalArgumentException();
105 buf = new byte[size];
110 * This method returns the number of bytes that can be read from this stream
111 * before a read can block. A return of 0 indicates that blocking might (or
112 * might not) occur on the very next read attempt.
114 * This method will return the number of bytes available from the pushback
115 * buffer plus the number of bytes available from the underlying stream.
117 * @return The number of bytes that can be read before blocking could occur
119 * @exception IOException
122 public int available() throws IOException {
124 return (buf.length - pos) + super.available();
125 } catch (NullPointerException npe) {
126 throw new IOException("Stream closed");
131 * This method closes the stream and releases any associated resources.
133 * @exception IOException
134 * If an error occurs.
136 public synchronized void close() throws IOException {
142 * This method returns <code>false</code> to indicate that it does not support
143 * mark/reset functionality.
145 * @return This method returns <code>false</code> to indicate that this class
146 * does not support mark/reset functionality
148 public boolean markSupported() {
153 * This method always throws an IOException in this class because mark/reset
154 * functionality is not supported.
156 * @exception IOException
157 * Always thrown for this class
159 public void reset() throws IOException {
160 throw new IOException("Mark not supported in this class");
164 * This method reads an unsigned byte from the input stream and returns it as
165 * an int in the range of 0-255. This method also will return -1 if the end of
166 * the stream has been reached. The byte returned will be read from the
167 * pushback buffer, unless the buffer is empty, in which case the byte will be
168 * read from the underlying stream.
170 * This method will block until the byte can be read.
172 * @return The byte read or -1 if end of stream
174 * @exception IOException
177 public synchronized int read() throws IOException {
178 if (pos < buf.length)
179 return ((int) buf[pos++]) & 0xFF;
185 * This method read bytes from a stream and stores them into a caller supplied
186 * buffer. It starts storing the data at index <code>offset</code> into the
187 * buffer and attempts to read <code>len</code> bytes. This method can return
188 * before reading the number of bytes requested. The actual number of bytes
189 * read is returned as an int. A -1 is returned to indicate the end of the
192 * This method will block until some data can be read.
194 * This method first reads bytes from the pushback buffer in order to satisfy
195 * the read request. If the pushback buffer cannot provide all of the bytes
196 * requested, the remaining bytes are read from the underlying stream.
199 * The array into which the bytes read should be stored
201 * The offset into the array to start storing bytes
203 * The requested number of bytes to read
205 * @return The actual number of bytes read, or -1 if end of stream.
207 * @exception IOException
208 * If an error occurs.
210 @LATTICE("OUT<THIS,THISLOC=THIS")
212 public synchronized int read(@LOC("OUT") byte[] b, @LOC("THIS,PushbackInputStream.POS") int off,
213 @LOC("THIS,PushbackInputStream.POS") int len) throws IOException {
214 @LOC("THIS,PushbackInputStream.POS") int numBytes = Math.min(buf.length - pos, len);
215 System.out.println("numBytes="+numBytes+" buf.length="+buf.length+" pos="+pos);
218 System.out.println("buf[pos]="+buf[pos]);
220 System.arraycopy(buf, pos, b, off, numBytes);
227 System.out.println("len>0");
228 len = super.read(b, off, len);
229 if (len == -1) // EOF
230 return numBytes > 0 ? numBytes : -1;
237 * This method pushes a single byte of data into the pushback buffer. The byte
238 * pushed back is the one that will be returned as the first byte of the next
241 * If the pushback buffer is full, this method throws an exception.
243 * The argument to this method is an <code>int</code>. Only the low eight bits
244 * of this value are pushed back.
247 * The byte to be pushed back, passed as an int
249 * @exception IOException
250 * If the pushback buffer is full.
252 public synchronized void unread(int b) throws IOException {
254 throw new IOException("Insufficient space in pushback buffer");
256 buf[--pos] = (byte) b;
260 * This method pushes all of the bytes in the passed byte array into the
261 * pushback bfer. These bytes are pushed in reverse order so that the next
262 * byte read from the stream after this operation will be <code>b[0]</code>
263 * followed by <code>b[1]</code>, etc.
265 * If the pushback buffer cannot hold all of the requested bytes, an exception
269 * The byte array to be pushed back
271 * @exception IOException
272 * If the pushback buffer is full
274 public synchronized void unread(byte[] b) throws IOException {
275 unread(b, 0, b.length);
279 * This method pushed back bytes from the passed in array into the pushback
280 * buffer. The bytes from <code>b[offset]</code> to
281 * <code>b[offset + len]</code> are pushed in reverse order so that the next
282 * byte read from the stream after this operation will be
283 * <code>b[offset]</code> followed by <code>b[offset + 1]</code>, etc.
285 * If the pushback buffer cannot hold all of the requested bytes, an exception
289 * The byte array to be pushed back
291 * The index into the array where the bytes to be push start
293 * The number of bytes to be pushed.
295 * @exception IOException
296 * If the pushback buffer is full
298 public synchronized void unread(@LOC("IN") byte[] b, @LOC("IN") int off, @LOC("IN") int len)
301 throw new IOException("Insufficient space in pushback buffer");
303 // Note the order that these bytes are being added is the opposite
304 // of what would be done if they were added to the buffer one at a time.
305 // See the Java Class Libraries book p. 1390.
306 System.arraycopy(b, off, buf, pos - len, len);
308 // Don't put this into the arraycopy above, an exception might be thrown
309 // and in that case we don't want to modify pos.
314 * This method skips the specified number of bytes in the stream. It returns
315 * the actual number of bytes skipped, which may be less than the requested
318 * This method first discards bytes from the buffer, then calls the
319 * <code>skip</code> method on the underlying <code>InputStream</code> to skip
320 * additional bytes if necessary.
323 * The requested number of bytes to skip
325 * @return The actual number of bytes skipped.
327 * @exception IOException
332 public synchronized long skip(long n) throws IOException {
333 final long origN = n;
336 int numread = (int) Math.min((long) (buf.length - pos), n);