KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > InputStream


1 /*
2  * @(#)InputStream.java 1.45 04/02/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * This abstract class is the superclass of all classes representing
12  * an input stream of bytes.
13  *
14  * <p> Applications that need to define a subclass of <code>InputStream</code>
15  * must always provide a method that returns the next byte of input.
16  *
17  * @author Arthur van Hoff
18  * @version 1.45, 02/19/04
19  * @see java.io.BufferedInputStream
20  * @see java.io.ByteArrayInputStream
21  * @see java.io.DataInputStream
22  * @see java.io.FilterInputStream
23  * @see java.io.InputStream#read()
24  * @see java.io.OutputStream
25  * @see java.io.PushbackInputStream
26  * @since JDK1.0
27  */

28 public abstract class InputStream implements Closeable JavaDoc {
29
30     // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
31
private static final int SKIP_BUFFER_SIZE = 2048;
32     // skipBuffer is initialized in skip(long), if needed.
33
private static byte[] skipBuffer;
34
35     /**
36      * Reads the next byte of data from the input stream. The value byte is
37      * returned as an <code>int</code> in the range <code>0</code> to
38      * <code>255</code>. If no byte is available because the end of the stream
39      * has been reached, the value <code>-1</code> is returned. This method
40      * blocks until input data is available, the end of the stream is detected,
41      * or an exception is thrown.
42      *
43      * <p> A subclass must provide an implementation of this method.
44      *
45      * @return the next byte of data, or <code>-1</code> if the end of the
46      * stream is reached.
47      * @exception IOException if an I/O error occurs.
48      */

49     public abstract int read() throws IOException JavaDoc;
50
51     /**
52      * Reads some number of bytes from the input stream and stores them into
53      * the buffer array <code>b</code>. The number of bytes actually read is
54      * returned as an integer. This method blocks until input data is
55      * available, end of file is detected, or an exception is thrown.
56      *
57      * <p> If <code>b</code> is <code>null</code>, a
58      * <code>NullPointerException</code> is thrown. If the length of
59      * <code>b</code> is zero, then no bytes are read and <code>0</code> is
60      * returned; otherwise, there is an attempt to read at least one byte. If
61      * no byte is available because the stream is at end of file, the value
62      * <code>-1</code> is returned; otherwise, at least one byte is read and
63      * stored into <code>b</code>.
64      *
65      * <p> The first byte read is stored into element <code>b[0]</code>, the
66      * next one into <code>b[1]</code>, and so on. The number of bytes read is,
67      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
68      * number of bytes actually read; these bytes will be stored in elements
69      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
70      * leaving elements <code>b[</code><i>k</i><code>]</code> through
71      * <code>b[b.length-1]</code> unaffected.
72      *
73      * <p> If the first byte cannot be read for any reason other than end of
74      * file, then an <code>IOException</code> is thrown. In particular, an
75      * <code>IOException</code> is thrown if the input stream has been closed.
76      *
77      * <p> The <code>read(b)</code> method for class <code>InputStream</code>
78      * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
79      *
80      * @param b the buffer into which the data is read.
81      * @return the total number of bytes read into the buffer, or
82      * <code>-1</code> is there is no more data because the end of
83      * the stream has been reached.
84      * @exception IOException if an I/O error occurs.
85      * @exception NullPointerException if <code>b</code> is <code>null</code>.
86      * @see java.io.InputStream#read(byte[], int, int)
87      */

88     public int read(byte b[]) throws IOException JavaDoc {
89     return read(b, 0, b.length);
90     }
91
92     /**
93      * Reads up to <code>len</code> bytes of data from the input stream into
94      * an array of bytes. An attempt is made to read as many as
95      * <code>len</code> bytes, but a smaller number may be read.
96      * The number of bytes actually read is returned as an integer.
97      *
98      * <p> This method blocks until input data is available, end of file is
99      * detected, or an exception is thrown.
100      *
101      * <p> If <code>b</code> is <code>null</code>, a
102      * <code>NullPointerException</code> is thrown.
103      *
104      * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
105      * <code>off+len</code> is greater than the length of the array
106      * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
107      * thrown.
108      *
109      * <p> If <code>len</code> is zero, then no bytes are read and
110      * <code>0</code> is returned; otherwise, there is an attempt to read at
111      * least one byte. If no byte is available because the stream is at end of
112      * file, the value <code>-1</code> is returned; otherwise, at least one
113      * byte is read and stored into <code>b</code>.
114      *
115      * <p> The first byte read is stored into element <code>b[off]</code>, the
116      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
117      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
118      * bytes actually read; these bytes will be stored in elements
119      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
120      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
121      * <code>b[off+len-1]</code> unaffected.
122      *
123      * <p> In every case, elements <code>b[0]</code> through
124      * <code>b[off]</code> and elements <code>b[off+len]</code> through
125      * <code>b[b.length-1]</code> are unaffected.
126      *
127      * <p> If the first byte cannot be read for any reason other than end of
128      * file, then an <code>IOException</code> is thrown. In particular, an
129      * <code>IOException</code> is thrown if the input stream has been closed.
130      *
131      * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
132      * for class <code>InputStream</code> simply calls the method
133      * <code>read()</code> repeatedly. If the first such call results in an
134      * <code>IOException</code>, that exception is returned from the call to
135      * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If
136      * any subsequent call to <code>read()</code> results in a
137      * <code>IOException</code>, the exception is caught and treated as if it
138      * were end of file; the bytes read up to that point are stored into
139      * <code>b</code> and the number of bytes read before the exception
140      * occurred is returned. Subclasses are encouraged to provide a more
141      * efficient implementation of this method.
142      *
143      * @param b the buffer into which the data is read.
144      * @param off the start offset in array <code>b</code>
145      * at which the data is written.
146      * @param len the maximum number of bytes to read.
147      * @return the total number of bytes read into the buffer, or
148      * <code>-1</code> if there is no more data because the end of
149      * the stream has been reached.
150      * @exception IOException if an I/O error occurs.
151      * @exception NullPointerException if <code>b</code> is <code>null</code>.
152      * @see java.io.InputStream#read()
153      */

154     public int read(byte b[], int off, int len) throws IOException JavaDoc {
155     if (b == null) {
156         throw new NullPointerException JavaDoc();
157     } else if ((off < 0) || (off > b.length) || (len < 0) ||
158            ((off + len) > b.length) || ((off + len) < 0)) {
159         throw new IndexOutOfBoundsException JavaDoc();
160     } else if (len == 0) {
161         return 0;
162     }
163
164     int c = read();
165     if (c == -1) {
166         return -1;
167     }
168     b[off] = (byte)c;
169
170     int i = 1;
171     try {
172         for (; i < len ; i++) {
173         c = read();
174         if (c == -1) {
175             break;
176         }
177         if (b != null) {
178             b[off + i] = (byte)c;
179         }
180         }
181     } catch (IOException JavaDoc ee) {
182     }
183     return i;
184     }
185
186     /**
187      * Skips over and discards <code>n</code> bytes of data from this input
188      * stream. The <code>skip</code> method may, for a variety of reasons, end
189      * up skipping over some smaller number of bytes, possibly <code>0</code>.
190      * This may result from any of a number of conditions; reaching end of file
191      * before <code>n</code> bytes have been skipped is only one possibility.
192      * The actual number of bytes skipped is returned. If <code>n</code> is
193      * negative, no bytes are skipped.
194      *
195      * <p> The <code>skip</code> method of <code>InputStream</code> creates a
196      * byte array and then repeatedly reads into it until <code>n</code> bytes
197      * have been read or the end of the stream has been reached. Subclasses are
198      * encouraged to provide a more efficient implementation of this method.
199      *
200      * @param n the number of bytes to be skipped.
201      * @return the actual number of bytes skipped.
202      * @exception IOException if an I/O error occurs.
203      */

204     public long skip(long n) throws IOException JavaDoc {
205
206     long remaining = n;
207     int nr;
208     if (skipBuffer == null)
209         skipBuffer = new byte[SKIP_BUFFER_SIZE];
210
211     byte[] localSkipBuffer = skipBuffer;
212         
213     if (n <= 0) {
214         return 0;
215     }
216
217     while (remaining > 0) {
218         nr = read(localSkipBuffer, 0,
219               (int) Math.min(SKIP_BUFFER_SIZE, remaining));
220         if (nr < 0) {
221         break;
222         }
223         remaining -= nr;
224     }
225     
226     return n - remaining;
227     }
228
229     /**
230      * Returns the number of bytes that can be read (or skipped over) from
231      * this input stream without blocking by the next caller of a method for
232      * this input stream. The next caller might be the same thread or
233      * another thread.
234      *
235      * <p> The <code>available</code> method for class <code>InputStream</code>
236      * always returns <code>0</code>.
237      *
238      * <p> This method should be overridden by subclasses.
239      *
240      * @return the number of bytes that can be read from this input stream
241      * without blocking.
242      * @exception IOException if an I/O error occurs.
243      */

244     public int available() throws IOException JavaDoc {
245     return 0;
246     }
247
248     /**
249      * Closes this input stream and releases any system resources associated
250      * with the stream.
251      *
252      * <p> The <code>close</code> method of <code>InputStream</code> does
253      * nothing.
254      *
255      * @exception IOException if an I/O error occurs.
256      */

257     public void close() throws IOException JavaDoc {}
258
259     /**
260      * Marks the current position in this input stream. A subsequent call to
261      * the <code>reset</code> method repositions this stream at the last marked
262      * position so that subsequent reads re-read the same bytes.
263      *
264      * <p> The <code>readlimit</code> arguments tells this input stream to
265      * allow that many bytes to be read before the mark position gets
266      * invalidated.
267      *
268      * <p> The general contract of <code>mark</code> is that, if the method
269      * <code>markSupported</code> returns <code>true</code>, the stream somehow
270      * remembers all the bytes read after the call to <code>mark</code> and
271      * stands ready to supply those same bytes again if and whenever the method
272      * <code>reset</code> is called. However, the stream is not required to
273      * remember any data at all if more than <code>readlimit</code> bytes are
274      * read from the stream before <code>reset</code> is called.
275      *
276      * <p> The <code>mark</code> method of <code>InputStream</code> does
277      * nothing.
278      *
279      * @param readlimit the maximum limit of bytes that can be read before
280      * the mark position becomes invalid.
281      * @see java.io.InputStream#reset()
282      */

283     public synchronized void mark(int readlimit) {}
284
285     /**
286      * Repositions this stream to the position at the time the
287      * <code>mark</code> method was last called on this input stream.
288      *
289      * <p> The general contract of <code>reset</code> is:
290      *
291      * <p><ul>
292      *
293      * <li> If the method <code>markSupported</code> returns
294      * <code>true</code>, then:
295      *
296      * <ul><li> If the method <code>mark</code> has not been called since
297      * the stream was created, or the number of bytes read from the stream
298      * since <code>mark</code> was last called is larger than the argument
299      * to <code>mark</code> at that last call, then an
300      * <code>IOException</code> might be thrown.
301      *
302      * <li> If such an <code>IOException</code> is not thrown, then the
303      * stream is reset to a state such that all the bytes read since the
304      * most recent call to <code>mark</code> (or since the start of the
305      * file, if <code>mark</code> has not been called) will be resupplied
306      * to subsequent callers of the <code>read</code> method, followed by
307      * any bytes that otherwise would have been the next input data as of
308      * the time of the call to <code>reset</code>. </ul>
309      *
310      * <li> If the method <code>markSupported</code> returns
311      * <code>false</code>, then:
312      *
313      * <ul><li> The call to <code>reset</code> may throw an
314      * <code>IOException</code>.
315      *
316      * <li> If an <code>IOException</code> is not thrown, then the stream
317      * is reset to a fixed state that depends on the particular type of the
318      * input stream and how it was created. The bytes that will be supplied
319      * to subsequent callers of the <code>read</code> method depend on the
320      * particular type of the input stream. </ul></ul>
321      *
322      * <p>The method <code>reset</code> for class <code>InputStream</code>
323      * does nothing except throw an <code>IOException</code>.
324      *
325      * @exception IOException if this stream has not been marked or if the
326      * mark has been invalidated.
327      * @see java.io.InputStream#mark(int)
328      * @see java.io.IOException
329      */

330     public synchronized void reset() throws IOException JavaDoc {
331     throw new IOException JavaDoc("mark/reset not supported");
332     }
333
334     /**
335      * Tests if this input stream supports the <code>mark</code> and
336      * <code>reset</code> methods. Whether or not <code>mark</code> and
337      * <code>reset</code> are supported is an invariant property of a
338      * particular input stream instance. The <code>markSupported</code> method
339      * of <code>InputStream</code> returns <code>false</code>.
340      *
341      * @return <code>true</code> if this stream instance supports the mark
342      * and reset methods; <code>false</code> otherwise.
343      * @see java.io.InputStream#mark(int)
344      * @see java.io.InputStream#reset()
345      */

346     public boolean markSupported() {
347     return false;
348     }
349
350 }
351
Popular Tags