changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / SSJavaInfer / InputStream.java
1 /* InputStream.java -- Base class for input
2    Copyright (C) 1998, 1999, 2001, 2004, 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; //NO PACKAGES FOR CLASS FILES
39
40 /**
41  * This abstract class forms the base of the hierarchy of classes that read
42  * input as a stream of bytes. It provides a common set of methods for reading
43  * bytes from streams. Subclasses implement and extend these methods to read
44  * bytes from a particular input source such as a file or network connection.
45  * 
46  * @author Aaron M. Renn (arenn@urbanophile.com)
47  * @author Warren Levy (warrenl@cygnus.com)
48  */
49
50 public abstract class InputStream // implements Closeable //COMPILER CANNOT
51                                   // HANDLE IMPLEMENTS
52 {
53   /**
54    * Default, no-arg, public constructor
55    */
56   public InputStream() {
57   }
58
59   /**
60    * This method returns the number of bytes that can be read from this stream
61    * before a read can block. A return of 0 indicates that blocking might (or
62    * might not) occur on the very next read attempt.
63    * <p>
64    * This method always returns 0 in this class
65    * 
66    * @return The number of bytes that can be read before blocking could occur
67    * 
68    * @exception IOException
69    *              If an error occurs
70    */
71
72   public int available() throws IOException {
73     return 0;
74   }
75
76   /**
77    * This method closes the stream. Any futher attempts to read from the stream
78    * may generate an <code>IOException</code>
79    * <p>
80    * This method does nothing in this class, but subclasses may override this
81    * method in order to provide additional functionality.
82    * 
83    * @exception IOException
84    *              If an error occurs, which can only happen in a subclass
85    */
86   public void close() throws IOException {
87     // Do nothing
88   }
89
90   /**
91    * This method marks a position in the input to which the stream can be
92    * "reset" by calling the <code>reset()</code> method. The parameter
93    * @code{readlimit} is the number of bytes that can be read from the stream
94    * after setting the mark before the mark becomes invalid. For example, if
95    * <code>mark()</code> is called with a read limit of 10, then when 11 bytes
96    * of data are read from the stream before the <code>reset()</code> method is
97    * called, then the mark is invalid and the stream object instance is not
98    * required to remember the mark.
99    * <p>
100    * This method does nothing in this class, but subclasses may override it to
101    * provide mark/reset functionality.
102    * 
103    * @param readLimit
104    *          The number of bytes that can be read before the mark becomes
105    *          invalid
106    */
107   public void mark(int readLimit) {
108     // Do nothing
109   }
110
111   /**
112    * This method returns a boolean that indicates whether the mark/reset methods
113    * are supported in this class. Those methods can be used to remember a
114    * specific point in the stream and reset the stream to that point.
115    * <p>
116    * This method always returns <code>false</code> in this class, but subclasses
117    * can override this method to return <code>true</code> if they support
118    * mark/reset functionality.
119    * 
120    * @return <code>true</code> if mark/reset functionality is supported,
121    *         <code>false</code> otherwise
122    */
123
124   public boolean markSupported() {
125     return false;
126   }
127
128   /**
129    * This method reads an unsigned byte from the input stream and returns it as
130    * an int in the range of 0-255. This method also will return -1 if the end of
131    * the stream has been reached.
132    * <p>
133    * This method will block until the byte can be read.
134    * 
135    * @return The byte read or -1 if end of stream
136    * 
137    * @exception IOException
138    *              If an error occurs
139    */
140   public abstract int read() throws IOException;
141
142   /**
143    * This method reads bytes from a stream and stores them into a caller
144    * supplied buffer. This method attempts to completely fill the buffer, but
145    * can return before doing so. The actual number of bytes read is returned as
146    * an int. A -1 is returned to indicate the end of the stream.
147    * <p>
148    * This method will block until some data can be read.
149    * <p>
150    * This method operates by calling an overloaded read method like so:
151    * <code>read(b, 0, b.length)</code>
152    * 
153    * @param b
154    *          The buffer into which the bytes read will be stored.
155    * 
156    * @return The number of bytes read or -1 if end of stream.
157    * 
158    * @exception IOException
159    *              If an error occurs.
160    */
161
162   public int read(byte[] b) throws IOException {
163     return read(b, 0, b.length);
164   }
165
166   /**
167    * This method read bytes from a stream and stores them into a caller supplied
168    * buffer. It starts storing the data at index <code>off</code> into the
169    * buffer and attempts to read <code>len</code> bytes. This method can return
170    * before reading the number of bytes requested. The actual number of bytes
171    * read is returned as an int. A -1 is returned to indicate the end of the
172    * stream.
173    * <p>
174    * This method will block until some data can be read.
175    * <p>
176    * This method operates by calling the single byte <code>read()</code> method
177    * in a loop until the desired number of bytes are read. The read loop stops
178    * short if the end of the stream is encountered or if an IOException is
179    * encountered on any read operation except the first. If the first attempt to
180    * read a bytes fails, the IOException is allowed to propagate upward. And
181    * subsequent IOException is caught and treated identically to an end of
182    * stream condition. Subclasses can (and should if possible) override this
183    * method to provide a more efficient implementation.
184    * 
185    * @param b
186    *          The array into which the bytes read should be stored
187    * @param off
188    *          The offset into the array to start storing bytes
189    * @param len
190    *          The requested number of bytes to read
191    * 
192    * @return The actual number of bytes read, or -1 if end of stream.
193    * 
194    * @exception IOException
195    *              If an error occurs.
196    */
197
198   public int read(byte[] b, int off, int len) throws IOException {
199     if (off < 0 || len < 0 || b.length - off < len)
200       throw new IndexOutOfBoundsException();
201
202     int i;
203     int ch;
204
205     for (i = 0; i < len; ++i)
206       try {
207         if ((ch = read()) < 0)
208           return i == 0 ? -1 : i; // EOF
209         b[off + i] = (byte) ch;
210       } catch (IOException ex) {
211         // Only reading the first byte should cause an IOException.
212         if (i == 0)
213           throw ex;
214         return i;
215       }
216
217     return i;
218   }
219
220   /**
221    * This method resets a stream to the point where the <code>mark()</code>
222    * method was called. Any bytes that were read after the mark point was set
223    * will be re-read during subsequent reads.
224    * <p>
225    * This method always throws an IOException in this class, but subclasses can
226    * override this method if they provide mark/reset functionality.
227    * 
228    * @exception IOException
229    *              Always thrown for this class
230    */
231   public void reset() throws IOException {
232     throw new IOException("mark/reset not supported");
233   }
234
235   /**
236    * This method skips the specified number of bytes in the stream. It returns
237    * the actual number of bytes skipped, which may be less than the requested
238    * amount.
239    * <p>
240    * This method reads and discards bytes into a byte array until the specified
241    * number of bytes were skipped or until either the end of stream is reached
242    * or a read attempt returns a short count. Subclasses can override this
243    * method to provide a more efficient implementation where one exists.
244    * 
245    * @param n
246    *          The requested number of bytes to skip
247    * 
248    * @return The actual number of bytes skipped.
249    * 
250    * @exception IOException
251    *              If an error occurs
252    */
253
254   public long skip(long n) throws IOException {
255     // Throw away n bytes by reading them into a temp byte[].
256     // Limit the temp array to 2Kb so we don't grab too much memory.
257     final int buflen = n > 2048 ? 2048 : (int) n;
258     byte[] tmpbuf = new byte[buflen];
259     final long origN = n;
260
261     while (n > 0) {
262       int numread = read(tmpbuf, 0, n > buflen ? buflen : (int) n);
263       if (numread <= 0)
264         break;
265       n -= numread;
266     }
267
268     return origN - n;
269   }
270 }