1 | /* PushbackReader.java -- An character stream that can unread chars
|
---|
2 | Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.io;
|
---|
40 |
|
---|
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.
|
---|
50 | *
|
---|
51 | * @version 0.0
|
---|
52 | *
|
---|
53 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
54 | * @author Warren Levy <warrenl@cygnus.com>
|
---|
55 | */
|
---|
56 | public class PushbackReader extends FilterReader
|
---|
57 | {
|
---|
58 | /**
|
---|
59 | * This is the default buffer size
|
---|
60 | */
|
---|
61 | private static final int DEFAULT_BUFFER_SIZE = 1;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * This is the buffer that is used to store the pushed back data
|
---|
65 | */
|
---|
66 | private char[] buf;
|
---|
67 |
|
---|
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
|
---|
74 | */
|
---|
75 | private int pos;
|
---|
76 |
|
---|
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.
|
---|
81 | *
|
---|
82 | * @code in The subordinate stream to read from
|
---|
83 | */
|
---|
84 | public PushbackReader(Reader in)
|
---|
85 | {
|
---|
86 | this(in, DEFAULT_BUFFER_SIZE);
|
---|
87 | }
|
---|
88 |
|
---|
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
|
---|
93 | *
|
---|
94 | * @param in The subordinate <code>Reader</code> to read from
|
---|
95 | * @param bufsize The pushback buffer size to use
|
---|
96 | */
|
---|
97 | public PushbackReader(Reader in, int bufsize)
|
---|
98 | {
|
---|
99 | super(in);
|
---|
100 |
|
---|
101 | if (bufsize < 0)
|
---|
102 | throw new IllegalArgumentException("buffer size must be positive");
|
---|
103 |
|
---|
104 | buf = new char[bufsize];
|
---|
105 | pos = bufsize;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * This method closes the stream and frees any associated resources.
|
---|
110 | *
|
---|
111 | * @exception IOException If an error occurs.
|
---|
112 | */
|
---|
113 | public void close() throws IOException
|
---|
114 | {
|
---|
115 | synchronized (lock)
|
---|
116 | {
|
---|
117 | buf = null;
|
---|
118 | super.close();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * This method throws an exception when called since this class does
|
---|
124 | * not support mark/reset.
|
---|
125 | *
|
---|
126 | * @param read_limit Not used.
|
---|
127 | *
|
---|
128 | * @exception IOException Always thrown to indicate mark/reset not supported.
|
---|
129 | */
|
---|
130 | public void mark(int read_limit) throws IOException
|
---|
131 | {
|
---|
132 | throw new IOException("mark not supported in this class");
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * This method returns <code>false</code> to indicate that it does not support
|
---|
137 | * mark/reset functionality.
|
---|
138 | *
|
---|
139 | * @return This method returns <code>false</code> to indicate that this class does not support mark/reset functionality
|
---|
140 | *
|
---|
141 | */
|
---|
142 | public boolean markSupported()
|
---|
143 | {
|
---|
144 | return(false);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * This method always throws an IOException in this class because
|
---|
149 | * mark/reset functionality is not supported.
|
---|
150 | *
|
---|
151 | * @exception IOException Always thrown for this class
|
---|
152 | */
|
---|
153 | public void reset() throws IOException
|
---|
154 | {
|
---|
155 | throw new IOException("reset not supported in this class");
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
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.
|
---|
167 | *
|
---|
168 | * @return <code>true</code> if this stream is ready to be read, <code>false</code> otherwise
|
---|
169 | *
|
---|
170 | * @exception IOException If an error occurs
|
---|
171 | */
|
---|
172 | public boolean ready() throws IOException
|
---|
173 | {
|
---|
174 | synchronized (lock)
|
---|
175 | {
|
---|
176 | if (buf == null)
|
---|
177 | throw new IOException ("stream closed");
|
---|
178 |
|
---|
179 | if (((buf.length - pos) > 0) || super.ready())
|
---|
180 | return(true);
|
---|
181 | else
|
---|
182 | return(false);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | // Don't delete this method just because the spec says it shouldn't be there!
|
---|
187 | // See the CVS log for details.
|
---|
188 | /**
|
---|
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.
|
---|
196 | *
|
---|
197 | * @param num_chars The requested number of chars to skip
|
---|
198 | *
|
---|
199 | * @return The actual number of chars skipped.
|
---|
200 | *
|
---|
201 | * @exception IOException If an error occurs
|
---|
202 | */
|
---|
203 | public long skip(long num_chars) throws IOException
|
---|
204 | {
|
---|
205 | synchronized (lock)
|
---|
206 | {
|
---|
207 | if (num_chars <= 0)
|
---|
208 | return(0);
|
---|
209 |
|
---|
210 | if ((buf.length - pos) >= num_chars)
|
---|
211 | {
|
---|
212 | pos += num_chars;
|
---|
213 | return(num_chars);
|
---|
214 | }
|
---|
215 |
|
---|
216 | int chars_discarded = buf.length - pos;
|
---|
217 | pos = buf.length;
|
---|
218 |
|
---|
219 | long chars_skipped = in.skip(num_chars - chars_discarded);
|
---|
220 |
|
---|
221 | return(chars_discarded + chars_skipped);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
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.
|
---|
233 | *
|
---|
234 | * @return The char read or -1 if end of stream
|
---|
235 | *
|
---|
236 | * @exception IOException If an error occurs
|
---|
237 | */
|
---|
238 | public int read() throws IOException
|
---|
239 | {
|
---|
240 | synchronized (lock)
|
---|
241 | {
|
---|
242 | if (buf == null)
|
---|
243 | throw new IOException("stream closed");
|
---|
244 |
|
---|
245 | if (pos == buf.length)
|
---|
246 | return(super.read());
|
---|
247 |
|
---|
248 | ++pos;
|
---|
249 | return((buf[pos - 1] & 0xFFFF));
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
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.
|
---|
267 | *
|
---|
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
|
---|
271 | *
|
---|
272 | * @return The actual number of chars read, or -1 if end of stream.
|
---|
273 | *
|
---|
274 | * @exception IOException If an error occurs.
|
---|
275 | */
|
---|
276 | public synchronized int read(char[] b, int offset, int len) throws IOException
|
---|
277 | {
|
---|
278 | synchronized (lock)
|
---|
279 | {
|
---|
280 | if (buf == null)
|
---|
281 | throw new IOException("stream closed");
|
---|
282 |
|
---|
283 | if (offset < 0 || len < 0 || offset + len > b.length)
|
---|
284 | throw new ArrayIndexOutOfBoundsException();
|
---|
285 |
|
---|
286 | int numBytes = Math.min(buf.length - pos, len);
|
---|
287 | if (numBytes > 0)
|
---|
288 | {
|
---|
289 | System.arraycopy (buf, pos, b, offset, numBytes);
|
---|
290 | pos += numBytes;
|
---|
291 | return numBytes;
|
---|
292 | }
|
---|
293 |
|
---|
294 | return super.read(b, offset, len);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
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.
|
---|
307 | *
|
---|
308 | * @param b The char to be pushed back, passed as an int
|
---|
309 | *
|
---|
310 | * @exception IOException If the pushback buffer is full.
|
---|
311 | */
|
---|
312 | public void unread(int b) throws IOException
|
---|
313 | {
|
---|
314 | synchronized (lock)
|
---|
315 | {
|
---|
316 | if (buf == null)
|
---|
317 | throw new IOException("stream closed");
|
---|
318 | if (pos == 0)
|
---|
319 | throw new IOException("Pushback buffer is full");
|
---|
320 |
|
---|
321 | --pos;
|
---|
322 | buf[pos] = (char)(b & 0xFFFF);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
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.
|
---|
334 | *
|
---|
335 | * @param buf The char array to be pushed back
|
---|
336 | *
|
---|
337 | * @exception IOException If the pushback buffer is full
|
---|
338 | */
|
---|
339 | public synchronized void unread(char[] buf) throws IOException
|
---|
340 | {
|
---|
341 | unread(buf, 0, buf.length);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
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.
|
---|
353 | *
|
---|
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.
|
---|
357 | *
|
---|
358 | * @exception IOException If the pushback buffer is full
|
---|
359 | */
|
---|
360 | public synchronized void unread(char[] b, int offset, int len)
|
---|
361 | throws IOException
|
---|
362 | {
|
---|
363 | synchronized (lock)
|
---|
364 | {
|
---|
365 | if (buf == null)
|
---|
366 | throw new IOException("stream closed");
|
---|
367 | if (pos < len)
|
---|
368 | throw new IOException("Pushback buffer is full");
|
---|
369 |
|
---|
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);
|
---|
374 |
|
---|
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;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|