1 /* InputStream.java -- Base class for input
2 Copyright (C) 1998, 1999, 2001, 2004, 2005 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
38 //package java.io; //NO PACKAGES FOR CLASS FILES
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.
46 * @author Aaron M. Renn (arenn@urbanophile.com)
47 * @author Warren Levy (warrenl@cygnus.com)
50 public abstract class InputStream // implements Closeable //COMPILER CANNOT
54 * Default, no-arg, public constructor
56 public InputStream() {
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.
64 * This method always returns 0 in this class
66 * @return The number of bytes that can be read before blocking could occur
68 * @exception IOException
72 public int available() throws IOException {
77 * This method closes the stream. Any futher attempts to read from the stream
78 * may generate an <code>IOException</code>
80 * This method does nothing in this class, but subclasses may override this
81 * method in order to provide additional functionality.
83 * @exception IOException
84 * If an error occurs, which can only happen in a subclass
86 public void close() throws IOException {
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.
100 * This method does nothing in this class, but subclasses may override it to
101 * provide mark/reset functionality.
104 * The number of bytes that can be read before the mark becomes
107 public void mark(int readLimit) {
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.
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.
120 * @return <code>true</code> if mark/reset functionality is supported,
121 * <code>false</code> otherwise
124 public boolean markSupported() {
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.
133 * This method will block until the byte can be read.
135 * @return The byte read or -1 if end of stream
137 * @exception IOException
140 public abstract int read() throws IOException;
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.
148 * This method will block until some data can be read.
150 * This method operates by calling an overloaded read method like so:
151 * <code>read(b, 0, b.length)</code>
154 * The buffer into which the bytes read will be stored.
156 * @return The number of bytes read or -1 if end of stream.
158 * @exception IOException
159 * If an error occurs.
162 public int read(byte[] b) throws IOException {
163 return read(b, 0, b.length);
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
174 * This method will block until some data can be read.
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.
186 * The array into which the bytes read should be stored
188 * The offset into the array to start storing bytes
190 * The requested number of bytes to read
192 * @return The actual number of bytes read, or -1 if end of stream.
194 * @exception IOException
195 * If an error occurs.
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();
205 for (i = 0; i < len; ++i)
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.
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.
225 * This method always throws an IOException in this class, but subclasses can
226 * override this method if they provide mark/reset functionality.
228 * @exception IOException
229 * Always thrown for this class
231 public void reset() throws IOException {
232 throw new IOException("mark/reset not supported");
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
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.
246 * The requested number of bytes to skip
248 * @return The actual number of bytes skipped.
250 * @exception IOException
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;
262 int numread = read(tmpbuf, 0, n > buflen ? buflen : (int) n);