View Javadoc
1   /*
2    * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package java.io;
27  
28  /**
29   * This abstract class is the superclass of all classes representing
30   * an input stream of bytes.
31   *
32   * <p> Applications that need to define a subclass of <code>InputStream</code>
33   * must always provide a method that returns the next byte of input.
34   *
35   * @author  Arthur van Hoff
36   * @see     java.io.BufferedInputStream
37   * @see     java.io.ByteArrayInputStream
38   * @see     java.io.DataInputStream
39   * @see     java.io.FilterInputStream
40   * @see     java.io.InputStream#read()
41   * @see     java.io.OutputStream
42   * @see     java.io.PushbackInputStream
43   * @since   JDK1.0
44   */
45  public abstract class InputStream implements Closeable {
46  
47      // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
48      // use when skipping.
49      private static final int MAX_SKIP_BUFFER_SIZE = 2048;
50  
51      /**
52       * Reads the next byte of data from the input stream. The value byte is
53       * returned as an <code>int</code> in the range <code>0</code> to
54       * <code>255</code>. If no byte is available because the end of the stream
55       * has been reached, the value <code>-1</code> is returned. This method
56       * blocks until input data is available, the end of the stream is detected,
57       * or an exception is thrown.
58       *
59       * <p> A subclass must provide an implementation of this method.
60       *
61       * @return     the next byte of data, or <code>-1</code> if the end of the
62       *             stream is reached.
63       * @exception  IOException  if an I/O error occurs.
64       */
65      public abstract int read() throws IOException;
66  
67      /**
68       * Reads some number of bytes from the input stream and stores them into
69       * the buffer array <code>b</code>. The number of bytes actually read is
70       * returned as an integer.  This method blocks until input data is
71       * available, end of file is detected, or an exception is thrown.
72       *
73       * <p> If the length of <code>b</code> is zero, then no bytes are read and
74       * <code>0</code> is returned; otherwise, there is an attempt to read at
75       * least one byte. If no byte is available because the stream is at the
76       * end of the file, the value <code>-1</code> is returned; otherwise, at
77       * least one byte is read and stored into <code>b</code>.
78       *
79       * <p> The first byte read is stored into element <code>b[0]</code>, the
80       * next one into <code>b[1]</code>, and so on. The number of bytes read is,
81       * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
82       * number of bytes actually read; these bytes will be stored in elements
83       * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
84       * leaving elements <code>b[</code><i>k</i><code>]</code> through
85       * <code>b[b.length-1]</code> unaffected.
86       *
87       * <p> The <code>read(b)</code> method for class <code>InputStream</code>
88       * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
89       *
90       * @param      b   the buffer into which the data is read.
91       * @return     the total number of bytes read into the buffer, or
92       *             <code>-1</code> if there is no more data because the end of
93       *             the stream has been reached.
94       * @exception  IOException  If the first byte cannot be read for any reason
95       * other than the end of the file, if the input stream has been closed, or
96       * if some other I/O error occurs.
97       * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
98       * @see        java.io.InputStream#read(byte[], int, int)
99       */
100     public int read(byte b[]) throws IOException {
101         return read(b, 0, b.length);
102     }
103 
104     /**
105      * Reads up to <code>len</code> bytes of data from the input stream into
106      * an array of bytes.  An attempt is made to read as many as
107      * <code>len</code> bytes, but a smaller number may be read.
108      * The number of bytes actually read is returned as an integer.
109      *
110      * <p> This method blocks until input data is available, end of file is
111      * detected, or an exception is thrown.
112      *
113      * <p> If <code>len</code> is zero, then no bytes are read and
114      * <code>0</code> is returned; otherwise, there is an attempt to read at
115      * least one byte. If no byte is available because the stream is at end of
116      * file, the value <code>-1</code> is returned; otherwise, at least one
117      * byte is read and stored into <code>b</code>.
118      *
119      * <p> The first byte read is stored into element <code>b[off]</code>, the
120      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
121      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
122      * bytes actually read; these bytes will be stored in elements
123      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
124      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
125      * <code>b[off+len-1]</code> unaffected.
126      *
127      * <p> In every case, elements <code>b[0]</code> through
128      * <code>b[off]</code> and elements <code>b[off+len]</code> through
129      * <code>b[b.length-1]</code> are unaffected.
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. The default implementation of this method blocks
141      * until the requested amount of input data <code>len</code> has been read,
142      * end of file is detected, or an exception is thrown. Subclasses are encouraged
143      * to provide a more efficient implementation of this method.
144      *
145      * @param      b     the buffer into which the data is read.
146      * @param      off   the start offset in array <code>b</code>
147      *                   at which the data is written.
148      * @param      len   the maximum number of bytes to read.
149      * @return     the total number of bytes read into the buffer, or
150      *             <code>-1</code> if there is no more data because the end of
151      *             the stream has been reached.
152      * @exception  IOException If the first byte cannot be read for any reason
153      * other than end of file, or if the input stream has been closed, or if
154      * some other I/O error occurs.
155      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
156      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
157      * <code>len</code> is negative, or <code>len</code> is greater than
158      * <code>b.length - off</code>
159      * @see        java.io.InputStream#read()
160      */
161     public int read(byte b[], int off, int len) throws IOException {
162         if (b == null) {
163             throw new NullPointerException();
164         } else if (off < 0 || len < 0 || len > b.length - off) {
165             throw new IndexOutOfBoundsException();
166         } else if (len == 0) {
167             return 0;
168         }
169 
170         int c = read();
171         if (c == -1) {
172             return -1;
173         }
174         b[off] = (byte)c;
175 
176         int i = 1;
177         try {
178             for (; i < len ; i++) {
179                 c = read();
180                 if (c == -1) {
181                     break;
182                 }
183                 b[off + i] = (byte)c;
184             }
185         } catch (IOException ee) {
186         }
187         return i;
188     }
189 
190     /**
191      * Skips over and discards <code>n</code> bytes of data from this input
192      * stream. The <code>skip</code> method may, for a variety of reasons, end
193      * up skipping over some smaller number of bytes, possibly <code>0</code>.
194      * This may result from any of a number of conditions; reaching end of file
195      * before <code>n</code> bytes have been skipped is only one possibility.
196      * The actual number of bytes skipped is returned. If {@code n} is
197      * negative, the {@code skip} method for class {@code InputStream} always
198      * returns 0, and no bytes are skipped. Subclasses may handle the negative
199      * value differently.
200      *
201      * <p> The <code>skip</code> method of this class creates a
202      * byte array and then repeatedly reads into it until <code>n</code> bytes
203      * have been read or the end of the stream has been reached. Subclasses are
204      * encouraged to provide a more efficient implementation of this method.
205      * For instance, the implementation may depend on the ability to seek.
206      *
207      * @param      n   the number of bytes to be skipped.
208      * @return     the actual number of bytes skipped.
209      * @exception  IOException  if the stream does not support seek,
210      *                          or if some other I/O error occurs.
211      */
212     public long skip(long n) throws IOException {
213 
214         long remaining = n;
215         int nr;
216 
217         if (n <= 0) {
218             return 0;
219         }
220 
221         int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
222         byte[] skipBuffer = new byte[size];
223         while (remaining > 0) {
224             nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
225             if (nr < 0) {
226                 break;
227             }
228             remaining -= nr;
229         }
230 
231         return n - remaining;
232     }
233 
234     /**
235      * Returns an estimate of the number of bytes that can be read (or
236      * skipped over) from this input stream without blocking by the next
237      * invocation of a method for this input stream. The next invocation
238      * might be the same thread or another thread.  A single read or skip of this
239      * many bytes will not block, but may read or skip fewer bytes.
240      *
241      * <p> Note that while some implementations of {@code InputStream} will return
242      * the total number of bytes in the stream, many will not.  It is
243      * never correct to use the return value of this method to allocate
244      * a buffer intended to hold all data in this stream.
245      *
246      * <p> A subclass' implementation of this method may choose to throw an
247      * {@link IOException} if this input stream has been closed by
248      * invoking the {@link #close()} method.
249      *
250      * <p> The {@code available} method for class {@code InputStream} always
251      * returns {@code 0}.
252      *
253      * <p> This method should be overridden by subclasses.
254      *
255      * @return     an estimate of the number of bytes that can be read (or skipped
256      *             over) from this input stream without blocking or {@code 0} when
257      *             it reaches the end of the input stream.
258      * @exception  IOException if an I/O error occurs.
259      */
260     public int available() throws IOException {
261         return 0;
262     }
263 
264     /**
265      * Closes this input stream and releases any system resources associated
266      * with the stream.
267      *
268      * <p> The <code>close</code> method of <code>InputStream</code> does
269      * nothing.
270      *
271      * @exception  IOException  if an I/O error occurs.
272      */
273     public void close() throws IOException {}
274 
275     /**
276      * Marks the current position in this input stream. A subsequent call to
277      * the <code>reset</code> method repositions this stream at the last marked
278      * position so that subsequent reads re-read the same bytes.
279      *
280      * <p> The <code>readlimit</code> arguments tells this input stream to
281      * allow that many bytes to be read before the mark position gets
282      * invalidated.
283      *
284      * <p> The general contract of <code>mark</code> is that, if the method
285      * <code>markSupported</code> returns <code>true</code>, the stream somehow
286      * remembers all the bytes read after the call to <code>mark</code> and
287      * stands ready to supply those same bytes again if and whenever the method
288      * <code>reset</code> is called.  However, the stream is not required to
289      * remember any data at all if more than <code>readlimit</code> bytes are
290      * read from the stream before <code>reset</code> is called.
291      *
292      * <p> Marking a closed stream should not have any effect on the stream.
293      *
294      * <p> The <code>mark</code> method of <code>InputStream</code> does
295      * nothing.
296      *
297      * @param   readlimit   the maximum limit of bytes that can be read before
298      *                      the mark position becomes invalid.
299      * @see     java.io.InputStream#reset()
300      */
301     public synchronized void mark(int readlimit) {}
302 
303     /**
304      * Repositions this stream to the position at the time the
305      * <code>mark</code> method was last called on this input stream.
306      *
307      * <p> The general contract of <code>reset</code> is:
308      *
309      * <ul>
310      * <li> If the method <code>markSupported</code> returns
311      * <code>true</code>, then:
312      *
313      *     <ul><li> If the method <code>mark</code> has not been called since
314      *     the stream was created, or the number of bytes read from the stream
315      *     since <code>mark</code> was last called is larger than the argument
316      *     to <code>mark</code> at that last call, then an
317      *     <code>IOException</code> might be thrown.
318      *
319      *     <li> If such an <code>IOException</code> is not thrown, then the
320      *     stream is reset to a state such that all the bytes read since the
321      *     most recent call to <code>mark</code> (or since the start of the
322      *     file, if <code>mark</code> has not been called) will be resupplied
323      *     to subsequent callers of the <code>read</code> method, followed by
324      *     any bytes that otherwise would have been the next input data as of
325      *     the time of the call to <code>reset</code>. </ul>
326      *
327      * <li> If the method <code>markSupported</code> returns
328      * <code>false</code>, then:
329      *
330      *     <ul><li> The call to <code>reset</code> may throw an
331      *     <code>IOException</code>.
332      *
333      *     <li> If an <code>IOException</code> is not thrown, then the stream
334      *     is reset to a fixed state that depends on the particular type of the
335      *     input stream and how it was created. The bytes that will be supplied
336      *     to subsequent callers of the <code>read</code> method depend on the
337      *     particular type of the input stream. </ul></ul>
338      *
339      * <p>The method <code>reset</code> for class <code>InputStream</code>
340      * does nothing except throw an <code>IOException</code>.
341      *
342      * @exception  IOException  if this stream has not been marked or if the
343      *               mark has been invalidated.
344      * @see     java.io.InputStream#mark(int)
345      * @see     java.io.IOException
346      */
347     public synchronized void reset() throws IOException {
348         throw new IOException("mark/reset not supported");
349     }
350 
351     /**
352      * Tests if this input stream supports the <code>mark</code> and
353      * <code>reset</code> methods. Whether or not <code>mark</code> and
354      * <code>reset</code> are supported is an invariant property of a
355      * particular input stream instance. The <code>markSupported</code> method
356      * of <code>InputStream</code> returns <code>false</code>.
357      *
358      * @return  <code>true</code> if this stream instance supports the mark
359      *          and reset methods; <code>false</code> otherwise.
360      * @see     java.io.InputStream#mark(int)
361      * @see     java.io.InputStream#reset()
362      */
363     public boolean markSupported() {
364         return false;
365     }
366 
367 }