1 /* PushbackReader.java -- An character stream that can unread chars
2 Copyright (C) 1998, 2000, 2001, 2003 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., 59 Temple Place, Suite 330, 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. */
42 * This subclass of <code>FilterReader</code> provides the ability to
43 * unread data from a stream. It maintains an internal buffer of unread
44 * data that is supplied to the next read operation. This is conceptually
45 * similar to mark/reset functionality, except that in this case the
46 * position to reset the stream to does not need to be known in advance.
48 * The default pushback buffer size one char, but this can be overridden
49 * by the creator of the stream.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Warren Levy <warrenl@cygnus.com>
54 public class PushbackReader
extends FilterReader
57 * This is the default buffer size
59 private static final int DEFAULT_BUFFER_SIZE
= 1;
62 * This is the buffer that is used to store the pushed back data
67 * This is the position in the buffer from which the next char will be
68 * read. Bytes are stored in reverse order in the buffer, starting from
69 * <code>buf[buf.length - 1]</code> to <code>buf[0]</code>. Thus when
70 * <code>pos</code> is 0 the buffer is full and <code>buf.length</code> when
76 * This method initializes a <code>PushbackReader</code> to read from the
77 * specified subordinate <code>Reader</code> with a default pushback buffer
80 * @param in The subordinate stream to read from
82 public PushbackReader(Reader in
)
84 this(in
, DEFAULT_BUFFER_SIZE
);
88 * This method initializes a <code>PushbackReader</code> to read from the
89 * specified subordinate <code>Reader</code> with the specified buffer
92 * @param in The subordinate <code>Reader</code> to read from
93 * @param bufsize The pushback buffer size to use
95 public PushbackReader(Reader in
, int bufsize
)
100 throw new IllegalArgumentException("buffer size must be positive");
102 buf
= new char[bufsize
];
107 * This method closes the stream and frees any associated resources.
109 * @exception IOException If an error occurs.
111 public void close() throws IOException
121 * This method throws an exception when called since this class does
122 * not support mark/reset.
124 * @param read_limit Not used.
126 * @exception IOException Always thrown to indicate mark/reset not supported.
128 public void mark(int read_limit
) throws IOException
130 throw new IOException("mark not supported in this class");
134 * This method returns <code>false</code> to indicate that it does not support
135 * mark/reset functionality.
137 * @return This method returns <code>false</code> to indicate that this
138 * class does not support mark/reset functionality
141 public boolean markSupported()
147 * This method always throws an IOException in this class because
148 * mark/reset functionality is not supported.
150 * @exception IOException Always thrown for this class
152 public void reset() throws IOException
154 throw new IOException("reset not supported in this class");
158 * This method determines whether or not this stream is ready to be read.
159 * If it returns <code>false</code> to indicate that the stream is not
160 * ready, any attempt to read from the stream could (but is not
161 * guaranteed to) block.
163 * This stream is ready to read if there are either chars waiting to be
164 * read in the pushback buffer or if the underlying stream is ready to
167 * @return <code>true</code> if this stream is ready to be read,
168 * <code>false</code> otherwise
170 * @exception IOException If an error occurs
172 public boolean ready() throws IOException
177 throw new IOException ("stream closed");
179 if (((buf
.length
- pos
) > 0) || super.ready())
186 // Don't delete this method just because the spec says it shouldn't be there!
187 // See the CVS log for details.
189 * This method skips the specified number of chars in the stream. It
190 * returns the actual number of chars skipped, which may be less than the
193 * This method first discards chars from the buffer, then calls the
194 * <code>skip</code> method on the underlying <code>Reader</code> to
195 * skip additional chars if necessary.
197 * @param num_chars The requested number of chars to skip
199 * @return The actual number of chars skipped.
201 * @exception IOException If an error occurs
203 public long skip(long num_chars
) throws IOException
210 if ((buf
.length
- pos
) >= num_chars
)
216 int chars_discarded
= buf
.length
- pos
;
219 long chars_skipped
= in
.skip(num_chars
- chars_discarded
);
221 return(chars_discarded
+ chars_skipped
);
226 * This method reads an unsigned char from the input stream and returns it
227 * as an int in the range of 0-65535. This method also will return -1 if
228 * the end of the stream has been reached. The char returned will be read
229 * from the pushback buffer, unless the buffer is empty, in which case
230 * the char will be read from the underlying stream.
232 * This method will block until the char can be read.
234 * @return The char read or -1 if end of stream
236 * @exception IOException If an error occurs
238 public int read() throws IOException
243 throw new IOException("stream closed");
245 if (pos
== buf
.length
)
246 return(super.read());
249 return((buf
[pos
- 1] & 0xFFFF));
254 * This method read chars from a stream and stores them into a caller
255 * supplied buffer. It starts storing the data at index <code>offset</code>
257 * the buffer and attempts to read <code>len</code> chars. This method can
258 * return before reading the number of chars requested. The actual number
259 * of chars read is returned as an int. A -1 is returned to indicate the
262 * This method will block until some data can be read.
264 * This method first reads chars from the pushback buffer in order to
265 * satisfy the read request. If the pushback buffer cannot provide all
266 * of the chars requested, the remaining chars are read from the
269 * @param buf The array into which the chars read should be stored
270 * @param offset The offset into the array to start storing chars
271 * @param len The requested number of chars to read
273 * @return The actual number of chars read, or -1 if end of stream.
275 * @exception IOException If an error occurs.
277 public synchronized int read(char[] b
, int offset
, int len
) throws IOException
282 throw new IOException("stream closed");
284 if (offset
< 0 || len
< 0 || offset
+ len
> b
.length
)
285 throw new ArrayIndexOutOfBoundsException();
287 int numBytes
= Math
.min(buf
.length
- pos
, len
);
290 System
.arraycopy (buf
, pos
, b
, offset
, numBytes
);
295 return super.read(b
, offset
, len
);
300 * This method pushes a single char of data into the pushback buffer.
301 * The char pushed back is the one that will be returned as the first char
304 * If the pushback buffer is full, this method throws an exception.
306 * The argument to this method is an <code>int</code>. Only the low eight
307 * bits of this value are pushed back.
309 * @param b The char to be pushed back, passed as an int
311 * @exception IOException If the pushback buffer is full.
313 public void unread(int b
) throws IOException
318 throw new IOException("stream closed");
320 throw new IOException("Pushback buffer is full");
323 buf
[pos
] = (char)(b
& 0xFFFF);
328 * This method pushes all of the chars in the passed char array into
329 * the pushback buffer. These chars are pushed in reverse order so that
330 * the next char read from the stream after this operation will be
331 * <code>buf[0]</code> followed by <code>buf[1]</code>, etc.
333 * If the pushback buffer cannot hold all of the requested chars, an
334 * exception is thrown.
336 * @param buf The char array to be pushed back
338 * @exception IOException If the pushback buffer is full
340 public synchronized void unread(char[] buf
) throws IOException
342 unread(buf
, 0, buf
.length
);
346 * This method pushed back chars from the passed in array into the pushback
347 * buffer. The chars from <code>buf[offset]</code> to
348 * <code>buf[offset + len]</code>
349 * are pushed in reverse order so that the next char read from the stream
350 * after this operation will be <code>buf[offset]</code> followed by
351 * <code>buf[offset + 1]</code>, etc.
353 * If the pushback buffer cannot hold all of the requested chars, an
354 * exception is thrown.
356 * @param buf The char array to be pushed back
357 * @param offset The index into the array where the chars to be push start
358 * @param len The number of chars to be pushed.
360 * @exception IOException If the pushback buffer is full
362 public synchronized void unread(char[] b
, int offset
, int len
)
368 throw new IOException("stream closed");
370 throw new IOException("Pushback buffer is full");
372 // Note the order that these chars are being added is the opposite
373 // of what would be done if they were added to the buffer one at a time.
374 // See the Java Class Libraries book p. 1397.
375 System
.arraycopy(b
, offset
, buf
, pos
- len
, len
);
377 // Don't put this into the arraycopy above, an exception might be thrown
378 // and in that case we don't want to modify pos.