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