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