FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / io / PushbackReader.java
blob80e949a170ae55c1becbada940d9ad9460553c11
1 /* PushbackReader.java -- An character stream that can unread chars
2 Copyright (C) 1998, 2000, 2001 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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.io;
41 /**
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.
47 * <p>
48 * The default pushback buffer size one char, but this can be overridden
49 * by the creator of the stream.
51 * @version 0.0
53 * @author Aaron M. Renn (arenn@urbanophile.com)
54 * @author Warren Levy <warrenl@cygnus.com>
56 public class PushbackReader extends FilterReader
58 /**
59 * This is the default buffer size
61 private static final int DEFAULT_BUFFER_SIZE = 1;
63 /**
64 * This is the buffer that is used to store the pushed back data
66 private char[] buf;
68 /**
69 * This is the position in the buffer from which the next char will be
70 * read. Bytes are stored in reverse order in the buffer, starting from
71 * <code>buf[buf.length - 1]</code> to <code>buf[0]</code>. Thus when
72 * <code>pos</code> is 0 the buffer is full and <code>buf.length</code> when
73 * it is empty
75 private int pos;
77 /**
78 * This method initializes a <code>PushbackReader</code> to read from the
79 * specified subordinate <code>Reader</code> with a default pushback buffer
80 * size of 1.
82 * @code in The subordinate stream to read from
84 public PushbackReader(Reader in)
86 this(in, DEFAULT_BUFFER_SIZE);
89 /**
90 * This method initializes a <code>PushbackReader</code> to read from the
91 * specified subordinate <code>Reader</code> with the specified buffer
92 * size
94 * @param in The subordinate <code>Reader</code> to read from
95 * @param bufsize The pushback buffer size to use
97 public PushbackReader(Reader in, int bufsize)
99 super(in);
101 if (bufsize < 0)
102 throw new IllegalArgumentException("buffer size must be positive");
104 buf = new char[bufsize];
105 pos = bufsize;
109 * This method closes the stream and frees any associated resources.
111 * @exception IOException If an error occurs.
113 public void close() throws IOException
115 synchronized (lock)
117 buf = null;
118 super.close();
123 * This method throws an exception when called since this class does
124 * not support mark/reset.
126 * @param read_limit Not used.
128 * @exception IOException Always thrown to indicate mark/reset not supported.
130 public void mark(int read_limit) throws IOException
132 throw new IOException("mark not supported in this class");
136 * This method returns <code>false</code> to indicate that it does not support
137 * mark/reset functionality.
139 * @return This method returns <code>false</code> to indicate that this class does not support mark/reset functionality
142 public boolean markSupported()
144 return(false);
148 * This method always throws an IOException in this class because
149 * mark/reset functionality is not supported.
151 * @exception IOException Always thrown for this class
153 public void reset() throws IOException
155 throw new IOException("reset not supported in this class");
159 * This method determines whether or not this stream is ready to be read.
160 * If it returns <code>false</code> to indicate that the stream is not
161 * ready, any attempt to read from the stream could (but is not
162 * guaranteed to) block.
163 * <p>
164 * This stream is ready to read if there are either chars waiting to be
165 * read in the pushback buffer or if the underlying stream is ready to
166 * be read.
168 * @return <code>true</code> if this stream is ready to be read, <code>false</code> otherwise
170 * @exception IOException If an error occurs
172 public boolean ready() throws IOException
174 synchronized (lock)
176 if (buf == null)
177 throw new IOException ("stream closed");
179 if (((buf.length - pos) > 0) || super.ready())
180 return(true);
181 else
182 return(false);
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
191 * requested amount.
192 * <p>
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
205 synchronized (lock)
207 if (num_chars <= 0)
208 return(0);
210 if ((buf.length - pos) >= num_chars)
212 pos += num_chars;
213 return(num_chars);
216 int chars_discarded = buf.length - pos;
217 pos = buf.length;
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.
231 * <p>
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
240 synchronized (lock)
242 if (buf == null)
243 throw new IOException("stream closed");
245 if (pos == buf.length)
246 return(super.read());
248 ++pos;
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> into
256 * the buffer and attempts to read <code>len</code> chars. This method can
257 * return before reading the number of chars requested. The actual number
258 * of chars read is returned as an int. A -1 is returned to indicate the
259 * end of the stream.
260 * <p>
261 * This method will block until some data can be read.
262 * <p>
263 * This method first reads chars from the pushback buffer in order to
264 * satisfy the read request. If the pushback buffer cannot provide all
265 * of the chars requested, the remaining chars are read from the
266 * underlying stream.
268 * @param buf The array into which the chars read should be stored
269 * @param offset The offset into the array to start storing chars
270 * @param len The requested number of chars to read
272 * @return The actual number of chars read, or -1 if end of stream.
274 * @exception IOException If an error occurs.
276 public synchronized int read(char[] b, int offset, int len) throws IOException
278 synchronized (lock)
280 if (buf == null)
281 throw new IOException("stream closed");
283 if (offset < 0 || len < 0 || offset + len > b.length)
284 throw new ArrayIndexOutOfBoundsException();
286 int numBytes = Math.min(buf.length - pos, len);
287 if (numBytes > 0)
289 System.arraycopy (buf, pos, b, offset, numBytes);
290 pos += numBytes;
291 return numBytes;
294 return super.read(b, offset, len);
299 * This method pushes a single char of data into the pushback buffer.
300 * The char pushed back is the one that will be returned as the first char
301 * of the next read.
302 * <p>
303 * If the pushback buffer is full, this method throws an exception.
304 * <p>
305 * The argument to this method is an <code>int</code>. Only the low eight bits
306 * of this value are pushed back.
308 * @param b The char to be pushed back, passed as an int
310 * @exception IOException If the pushback buffer is full.
312 public void unread(int b) throws IOException
314 synchronized (lock)
316 if (buf == null)
317 throw new IOException("stream closed");
318 if (pos == 0)
319 throw new IOException("Pushback buffer is full");
321 --pos;
322 buf[pos] = (char)(b & 0xFFFF);
327 * This method pushes all of the chars in the passed char array into
328 * the pushback buffer. These chars are pushed in reverse order so that
329 * the next char read from the stream after this operation will be
330 * <code>buf[0]</code> followed by <code>buf[1]</code>, etc.
331 * <p>
332 * If the pushback buffer cannot hold all of the requested chars, an
333 * exception is thrown.
335 * @param buf The char array to be pushed back
337 * @exception IOException If the pushback buffer is full
339 public synchronized void unread(char[] buf) throws IOException
341 unread(buf, 0, buf.length);
345 * This method pushed back chars from the passed in array into the pushback
346 * buffer. The chars from <code>buf[offset]</code> to <code>buf[offset + len]</code>
347 * are pushed in reverse order so that the next char read from the stream
348 * after this operation will be <code>buf[offset]</code> followed by
349 * <code>buf[offset + 1]</code>, etc.
350 * <p>
351 * If the pushback buffer cannot hold all of the requested chars, an
352 * exception is thrown.
354 * @param buf The char array to be pushed back
355 * @param offset The index into the array where the chars to be push start
356 * @param len The number of chars to be pushed.
358 * @exception IOException If the pushback buffer is full
360 public synchronized void unread(char[] b, int offset, int len)
361 throws IOException
363 synchronized (lock)
365 if (buf == null)
366 throw new IOException("stream closed");
367 if (pos < len)
368 throw new IOException("Pushback buffer is full");
370 // Note the order that these chars are being added is the opposite
371 // of what would be done if they were added to the buffer one at a time.
372 // See the Java Class Libraries book p. 1397.
373 System.arraycopy(b, offset, buf, pos - len, len);
375 // Don't put this into the arraycopy above, an exception might be thrown
376 // and in that case we don't want to modify pos.
377 pos -= len;