src/java.base/share/classes/java/io/InputStream.java
changeset 58288 48e480e56aad
parent 58242 94bb65cb37d3
child 58679 9c3209ff7550
equal deleted inserted replaced
58287:a7f16447085e 58288:48e480e56aad
    32 
    32 
    33 /**
    33 /**
    34  * This abstract class is the superclass of all classes representing
    34  * This abstract class is the superclass of all classes representing
    35  * an input stream of bytes.
    35  * an input stream of bytes.
    36  *
    36  *
    37  * <p> Applications that need to define a subclass of <code>InputStream</code>
    37  * <p> Applications that need to define a subclass of {@code InputStream}
    38  * must always provide a method that returns the next byte of input.
    38  * must always provide a method that returns the next byte of input.
    39  *
    39  *
    40  * @author  Arthur van Hoff
    40  * @author  Arthur van Hoff
    41  * @see     java.io.BufferedInputStream
    41  * @see     java.io.BufferedInputStream
    42  * @see     java.io.ByteArrayInputStream
    42  * @see     java.io.ByteArrayInputStream
   165         };
   165         };
   166     }
   166     }
   167 
   167 
   168     /**
   168     /**
   169      * Reads the next byte of data from the input stream. The value byte is
   169      * Reads the next byte of data from the input stream. The value byte is
   170      * returned as an <code>int</code> in the range <code>0</code> to
   170      * returned as an {@code int} in the range {@code 0} to
   171      * <code>255</code>. If no byte is available because the end of the stream
   171      * {@code 255}. If no byte is available because the end of the stream
   172      * has been reached, the value <code>-1</code> is returned. This method
   172      * has been reached, the value {@code -1} is returned. This method
   173      * blocks until input data is available, the end of the stream is detected,
   173      * blocks until input data is available, the end of the stream is detected,
   174      * or an exception is thrown.
   174      * or an exception is thrown.
   175      *
   175      *
   176      * <p> A subclass must provide an implementation of this method.
   176      * <p> A subclass must provide an implementation of this method.
   177      *
   177      *
   178      * @return     the next byte of data, or <code>-1</code> if the end of the
   178      * @return     the next byte of data, or {@code -1} if the end of the
   179      *             stream is reached.
   179      *             stream is reached.
   180      * @throws     IOException  if an I/O error occurs.
   180      * @throws     IOException  if an I/O error occurs.
   181      */
   181      */
   182     public abstract int read() throws IOException;
   182     public abstract int read() throws IOException;
   183 
   183 
   184     /**
   184     /**
   185      * Reads some number of bytes from the input stream and stores them into
   185      * Reads some number of bytes from the input stream and stores them into
   186      * the buffer array <code>b</code>. The number of bytes actually read is
   186      * the buffer array {@code b}. The number of bytes actually read is
   187      * returned as an integer.  This method blocks until input data is
   187      * returned as an integer.  This method blocks until input data is
   188      * available, end of file is detected, or an exception is thrown.
   188      * available, end of file is detected, or an exception is thrown.
   189      *
   189      *
   190      * <p> If the length of <code>b</code> is zero, then no bytes are read and
   190      * <p> If the length of {@code b} is zero, then no bytes are read and
   191      * <code>0</code> is returned; otherwise, there is an attempt to read at
   191      * {@code 0} is returned; otherwise, there is an attempt to read at
   192      * least one byte. If no byte is available because the stream is at the
   192      * least one byte. If no byte is available because the stream is at the
   193      * end of the file, the value <code>-1</code> is returned; otherwise, at
   193      * end of the file, the value {@code -1} is returned; otherwise, at
   194      * least one byte is read and stored into <code>b</code>.
   194      * least one byte is read and stored into {@code b}.
   195      *
   195      *
   196      * <p> The first byte read is stored into element <code>b[0]</code>, the
   196      * <p> The first byte read is stored into element {@code b[0]}, the
   197      * next one into <code>b[1]</code>, and so on. The number of bytes read is,
   197      * next one into {@code b[1]}, and so on. The number of bytes read is,
   198      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
   198      * at most, equal to the length of {@code b}. Let <i>k</i> be the
   199      * number of bytes actually read; these bytes will be stored in elements
   199      * number of bytes actually read; these bytes will be stored in elements
   200      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
   200      * {@code b[0]} through {@code b[}<i>k</i>{@code -1]},
   201      * leaving elements <code>b[</code><i>k</i><code>]</code> through
   201      * leaving elements {@code b[}<i>k</i>{@code ]} through
   202      * <code>b[b.length-1]</code> unaffected.
   202      * {@code b[b.length-1]} unaffected.
   203      *
   203      *
   204      * <p> The <code>read(b)</code> method for class <code>InputStream</code>
   204      * <p> The {@code read(b)} method for class {@code InputStream}
   205      * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
   205      * has the same effect as: <pre>{@code  read(b, 0, b.length) }</pre>
   206      *
   206      *
   207      * @param      b   the buffer into which the data is read.
   207      * @param      b   the buffer into which the data is read.
   208      * @return     the total number of bytes read into the buffer, or
   208      * @return     the total number of bytes read into the buffer, or
   209      *             <code>-1</code> if there is no more data because the end of
   209      *             {@code -1} if there is no more data because the end of
   210      *             the stream has been reached.
   210      *             the stream has been reached.
   211      * @throws     IOException  If the first byte cannot be read for any reason
   211      * @throws     IOException  If the first byte cannot be read for any reason
   212      *             other than the end of the file, if the input stream has been
   212      *             other than the end of the file, if the input stream has been
   213      *             closed, or if some other I/O error occurs.
   213      *             closed, or if some other I/O error occurs.
   214      * @throws     NullPointerException  if <code>b</code> is <code>null</code>.
   214      * @throws     NullPointerException  if {@code b} is {@code null}.
   215      * @see        java.io.InputStream#read(byte[], int, int)
   215      * @see        java.io.InputStream#read(byte[], int, int)
   216      */
   216      */
   217     public int read(byte b[]) throws IOException {
   217     public int read(byte b[]) throws IOException {
   218         return read(b, 0, b.length);
   218         return read(b, 0, b.length);
   219     }
   219     }
   220 
   220 
   221     /**
   221     /**
   222      * Reads up to <code>len</code> bytes of data from the input stream into
   222      * Reads up to {@code len} bytes of data from the input stream into
   223      * an array of bytes.  An attempt is made to read as many as
   223      * an array of bytes.  An attempt is made to read as many as
   224      * <code>len</code> bytes, but a smaller number may be read.
   224      * {@code len} bytes, but a smaller number may be read.
   225      * The number of bytes actually read is returned as an integer.
   225      * The number of bytes actually read is returned as an integer.
   226      *
   226      *
   227      * <p> This method blocks until input data is available, end of file is
   227      * <p> This method blocks until input data is available, end of file is
   228      * detected, or an exception is thrown.
   228      * detected, or an exception is thrown.
   229      *
   229      *
   230      * <p> If <code>len</code> is zero, then no bytes are read and
   230      * <p> If {@code len} is zero, then no bytes are read and
   231      * <code>0</code> is returned; otherwise, there is an attempt to read at
   231      * {@code 0} is returned; otherwise, there is an attempt to read at
   232      * least one byte. If no byte is available because the stream is at end of
   232      * least one byte. If no byte is available because the stream is at end of
   233      * file, the value <code>-1</code> is returned; otherwise, at least one
   233      * file, the value {@code -1} is returned; otherwise, at least one
   234      * byte is read and stored into <code>b</code>.
   234      * byte is read and stored into {@code b}.
   235      *
   235      *
   236      * <p> The first byte read is stored into element <code>b[off]</code>, the
   236      * <p> The first byte read is stored into element {@code b[off]}, the
   237      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
   237      * next one into {@code b[off+1]}, and so on. The number of bytes read
   238      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
   238      * is, at most, equal to {@code len}. Let <i>k</i> be the number of
   239      * bytes actually read; these bytes will be stored in elements
   239      * bytes actually read; these bytes will be stored in elements
   240      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
   240      * {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},
   241      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
   241      * leaving elements {@code b[off+}<i>k</i>{@code ]} through
   242      * <code>b[off+len-1]</code> unaffected.
   242      * {@code b[off+len-1]} unaffected.
   243      *
   243      *
   244      * <p> In every case, elements <code>b[0]</code> through
   244      * <p> In every case, elements {@code b[0]} through
   245      * <code>b[off-1]</code> and elements <code>b[off+len]</code> through
   245      * {@code b[off-1]} and elements {@code b[off+len]} through
   246      * <code>b[b.length-1]</code> are unaffected.
   246      * {@code b[b.length-1]} are unaffected.
   247      *
   247      *
   248      * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
   248      * <p> The {@code read(b, off, len)} method
   249      * for class <code>InputStream</code> simply calls the method
   249      * for class {@code InputStream} simply calls the method
   250      * <code>read()</code> repeatedly. If the first such call results in an
   250      * {@code read()} repeatedly. If the first such call results in an
   251      * <code>IOException</code>, that exception is returned from the call to
   251      * {@code IOException}, that exception is returned from the call to
   252      * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
   252      * the {@code read(b,} {@code off,} {@code len)} method.  If
   253      * any subsequent call to <code>read()</code> results in a
   253      * any subsequent call to {@code read()} results in a
   254      * <code>IOException</code>, the exception is caught and treated as if it
   254      * {@code IOException}, the exception is caught and treated as if it
   255      * were end of file; the bytes read up to that point are stored into
   255      * were end of file; the bytes read up to that point are stored into
   256      * <code>b</code> and the number of bytes read before the exception
   256      * {@code b} and the number of bytes read before the exception
   257      * occurred is returned. The default implementation of this method blocks
   257      * occurred is returned. The default implementation of this method blocks
   258      * until the requested amount of input data <code>len</code> has been read,
   258      * until the requested amount of input data {@code len} has been read,
   259      * end of file is detected, or an exception is thrown. Subclasses are
   259      * end of file is detected, or an exception is thrown. Subclasses are
   260      * encouraged to provide a more efficient implementation of this method.
   260      * encouraged to provide a more efficient implementation of this method.
   261      *
   261      *
   262      * @param      b     the buffer into which the data is read.
   262      * @param      b     the buffer into which the data is read.
   263      * @param      off   the start offset in array <code>b</code>
   263      * @param      off   the start offset in array {@code b}
   264      *                   at which the data is written.
   264      *                   at which the data is written.
   265      * @param      len   the maximum number of bytes to read.
   265      * @param      len   the maximum number of bytes to read.
   266      * @return     the total number of bytes read into the buffer, or
   266      * @return     the total number of bytes read into the buffer, or
   267      *             <code>-1</code> if there is no more data because the end of
   267      *             {@code -1} if there is no more data because the end of
   268      *             the stream has been reached.
   268      *             the stream has been reached.
   269      * @throws     IOException If the first byte cannot be read for any reason
   269      * @throws     IOException If the first byte cannot be read for any reason
   270      *             other than end of file, or if the input stream has been closed,
   270      *             other than end of file, or if the input stream has been closed,
   271      *             or if some other I/O error occurs.
   271      *             or if some other I/O error occurs.
   272      * @throws     NullPointerException If <code>b</code> is <code>null</code>.
   272      * @throws     NullPointerException If {@code b} is {@code null}.
   273      * @throws     IndexOutOfBoundsException If <code>off</code> is negative,
   273      * @throws     IndexOutOfBoundsException If {@code off} is negative,
   274      *             <code>len</code> is negative, or <code>len</code> is greater than
   274      *             {@code len} is negative, or {@code len} is greater than
   275      *             <code>b.length - off</code>
   275      *             {@code b.length - off}
   276      * @see        java.io.InputStream#read()
   276      * @see        java.io.InputStream#read()
   277      */
   277      */
   278     public int read(byte b[], int off, int len) throws IOException {
   278     public int read(byte b[], int off, int len) throws IOException {
   279         Objects.checkFromIndexSize(off, len, b.length);
   279         Objects.checkFromIndexSize(off, len, b.length);
   280         if (len == 0) {
   280         if (len == 0) {
   507         }
   507         }
   508         return n;
   508         return n;
   509     }
   509     }
   510 
   510 
   511     /**
   511     /**
   512      * Skips over and discards <code>n</code> bytes of data from this input
   512      * Skips over and discards {@code n} bytes of data from this input
   513      * stream. The <code>skip</code> method may, for a variety of reasons, end
   513      * stream. The {@code skip} method may, for a variety of reasons, end
   514      * up skipping over some smaller number of bytes, possibly <code>0</code>.
   514      * up skipping over some smaller number of bytes, possibly {@code 0}.
   515      * This may result from any of a number of conditions; reaching end of file
   515      * This may result from any of a number of conditions; reaching end of file
   516      * before <code>n</code> bytes have been skipped is only one possibility.
   516      * before {@code n} bytes have been skipped is only one possibility.
   517      * The actual number of bytes skipped is returned. If {@code n} is
   517      * The actual number of bytes skipped is returned. If {@code n} is
   518      * negative, the {@code skip} method for class {@code InputStream} always
   518      * negative, the {@code skip} method for class {@code InputStream} always
   519      * returns 0, and no bytes are skipped. Subclasses may handle the negative
   519      * returns 0, and no bytes are skipped. Subclasses may handle the negative
   520      * value differently.
   520      * value differently.
   521      *
   521      *
   522      * <p> The <code>skip</code> method implementation of this class creates a
   522      * <p> The {@code skip} method implementation of this class creates a
   523      * byte array and then repeatedly reads into it until <code>n</code> bytes
   523      * byte array and then repeatedly reads into it until {@code n} bytes
   524      * have been read or the end of the stream has been reached. Subclasses are
   524      * have been read or the end of the stream has been reached. Subclasses are
   525      * encouraged to provide a more efficient implementation of this method.
   525      * encouraged to provide a more efficient implementation of this method.
   526      * For instance, the implementation may depend on the ability to seek.
   526      * For instance, the implementation may depend on the ability to seek.
   527      *
   527      *
   528      * @param      n   the number of bytes to be skipped.
   528      * @param      n   the number of bytes to be skipped.
   642 
   642 
   643     /**
   643     /**
   644      * Closes this input stream and releases any system resources associated
   644      * Closes this input stream and releases any system resources associated
   645      * with the stream.
   645      * with the stream.
   646      *
   646      *
   647      * <p> The <code>close</code> method of <code>InputStream</code> does
   647      * <p> The {@code close} method of {@code InputStream} does
   648      * nothing.
   648      * nothing.
   649      *
   649      *
   650      * @throws     IOException  if an I/O error occurs.
   650      * @throws     IOException  if an I/O error occurs.
   651      */
   651      */
   652     public void close() throws IOException {}
   652     public void close() throws IOException {}
   653 
   653 
   654     /**
   654     /**
   655      * Marks the current position in this input stream. A subsequent call to
   655      * Marks the current position in this input stream. A subsequent call to
   656      * the <code>reset</code> method repositions this stream at the last marked
   656      * the {@code reset} method repositions this stream at the last marked
   657      * position so that subsequent reads re-read the same bytes.
   657      * position so that subsequent reads re-read the same bytes.
   658      *
   658      *
   659      * <p> The <code>readlimit</code> arguments tells this input stream to
   659      * <p> The {@code readlimit} arguments tells this input stream to
   660      * allow that many bytes to be read before the mark position gets
   660      * allow that many bytes to be read before the mark position gets
   661      * invalidated.
   661      * invalidated.
   662      *
   662      *
   663      * <p> The general contract of <code>mark</code> is that, if the method
   663      * <p> The general contract of {@code mark} is that, if the method
   664      * <code>markSupported</code> returns <code>true</code>, the stream somehow
   664      * {@code markSupported} returns {@code true}, the stream somehow
   665      * remembers all the bytes read after the call to <code>mark</code> and
   665      * remembers all the bytes read after the call to {@code mark} and
   666      * stands ready to supply those same bytes again if and whenever the method
   666      * stands ready to supply those same bytes again if and whenever the method
   667      * <code>reset</code> is called.  However, the stream is not required to
   667      * {@code reset} is called.  However, the stream is not required to
   668      * remember any data at all if more than <code>readlimit</code> bytes are
   668      * remember any data at all if more than {@code readlimit} bytes are
   669      * read from the stream before <code>reset</code> is called.
   669      * read from the stream before {@code reset} is called.
   670      *
   670      *
   671      * <p> Marking a closed stream should not have any effect on the stream.
   671      * <p> Marking a closed stream should not have any effect on the stream.
   672      *
   672      *
   673      * <p> The <code>mark</code> method of <code>InputStream</code> does
   673      * <p> The {@code mark} method of {@code InputStream} does
   674      * nothing.
   674      * nothing.
   675      *
   675      *
   676      * @param   readlimit   the maximum limit of bytes that can be read before
   676      * @param   readlimit   the maximum limit of bytes that can be read before
   677      *                      the mark position becomes invalid.
   677      *                      the mark position becomes invalid.
   678      * @see     java.io.InputStream#reset()
   678      * @see     java.io.InputStream#reset()
   679      */
   679      */
   680     public synchronized void mark(int readlimit) {}
   680     public synchronized void mark(int readlimit) {}
   681 
   681 
   682     /**
   682     /**
   683      * Repositions this stream to the position at the time the
   683      * Repositions this stream to the position at the time the
   684      * <code>mark</code> method was last called on this input stream.
   684      * {@code mark} method was last called on this input stream.
   685      *
   685      *
   686      * <p> The general contract of <code>reset</code> is:
   686      * <p> The general contract of {@code reset} is:
   687      *
   687      *
   688      * <ul>
   688      * <ul>
   689      * <li> If the method <code>markSupported</code> returns
   689      * <li> If the method {@code markSupported} returns
   690      * <code>true</code>, then:
   690      * {@code true}, then:
   691      *
   691      *
   692      *     <ul><li> If the method <code>mark</code> has not been called since
   692      *     <ul><li> If the method {@code mark} has not been called since
   693      *     the stream was created, or the number of bytes read from the stream
   693      *     the stream was created, or the number of bytes read from the stream
   694      *     since <code>mark</code> was last called is larger than the argument
   694      *     since {@code mark} was last called is larger than the argument
   695      *     to <code>mark</code> at that last call, then an
   695      *     to {@code mark} at that last call, then an
   696      *     <code>IOException</code> might be thrown.
   696      *     {@code IOException} might be thrown.
   697      *
   697      *
   698      *     <li> If such an <code>IOException</code> is not thrown, then the
   698      *     <li> If such an {@code IOException} is not thrown, then the
   699      *     stream is reset to a state such that all the bytes read since the
   699      *     stream is reset to a state such that all the bytes read since the
   700      *     most recent call to <code>mark</code> (or since the start of the
   700      *     most recent call to {@code mark} (or since the start of the
   701      *     file, if <code>mark</code> has not been called) will be resupplied
   701      *     file, if {@code mark} has not been called) will be resupplied
   702      *     to subsequent callers of the <code>read</code> method, followed by
   702      *     to subsequent callers of the {@code read} method, followed by
   703      *     any bytes that otherwise would have been the next input data as of
   703      *     any bytes that otherwise would have been the next input data as of
   704      *     the time of the call to <code>reset</code>. </ul>
   704      *     the time of the call to {@code reset}. </ul>
   705      *
   705      *
   706      * <li> If the method <code>markSupported</code> returns
   706      * <li> If the method {@code markSupported} returns
   707      * <code>false</code>, then:
   707      * {@code false}, then:
   708      *
   708      *
   709      *     <ul><li> The call to <code>reset</code> may throw an
   709      *     <ul><li> The call to {@code reset} may throw an
   710      *     <code>IOException</code>.
   710      *     {@code IOException}.
   711      *
   711      *
   712      *     <li> If an <code>IOException</code> is not thrown, then the stream
   712      *     <li> If an {@code IOException} is not thrown, then the stream
   713      *     is reset to a fixed state that depends on the particular type of the
   713      *     is reset to a fixed state that depends on the particular type of the
   714      *     input stream and how it was created. The bytes that will be supplied
   714      *     input stream and how it was created. The bytes that will be supplied
   715      *     to subsequent callers of the <code>read</code> method depend on the
   715      *     to subsequent callers of the {@code read} method depend on the
   716      *     particular type of the input stream. </ul></ul>
   716      *     particular type of the input stream. </ul></ul>
   717      *
   717      *
   718      * <p>The method <code>reset</code> for class <code>InputStream</code>
   718      * <p>The method {@code reset} for class {@code InputStream}
   719      * does nothing except throw an <code>IOException</code>.
   719      * does nothing except throw an {@code IOException}.
   720      *
   720      *
   721      * @throws  IOException  if this stream has not been marked or if the
   721      * @throws  IOException  if this stream has not been marked or if the
   722      *          mark has been invalidated.
   722      *          mark has been invalidated.
   723      * @see     java.io.InputStream#mark(int)
   723      * @see     java.io.InputStream#mark(int)
   724      * @see     java.io.IOException
   724      * @see     java.io.IOException
   726     public synchronized void reset() throws IOException {
   726     public synchronized void reset() throws IOException {
   727         throw new IOException("mark/reset not supported");
   727         throw new IOException("mark/reset not supported");
   728     }
   728     }
   729 
   729 
   730     /**
   730     /**
   731      * Tests if this input stream supports the <code>mark</code> and
   731      * Tests if this input stream supports the {@code mark} and
   732      * <code>reset</code> methods. Whether or not <code>mark</code> and
   732      * {@code reset} methods. Whether or not {@code mark} and
   733      * <code>reset</code> are supported is an invariant property of a
   733      * {@code reset} are supported is an invariant property of a
   734      * particular input stream instance. The <code>markSupported</code> method
   734      * particular input stream instance. The {@code markSupported} method
   735      * of <code>InputStream</code> returns <code>false</code>.
   735      * of {@code InputStream} returns {@code false}.
   736      *
   736      *
   737      * @return  <code>true</code> if this stream instance supports the mark
   737      * @return  {@code true} if this stream instance supports the mark
   738      *          and reset methods; <code>false</code> otherwise.
   738      *          and reset methods; {@code false} otherwise.
   739      * @see     java.io.InputStream#mark(int)
   739      * @see     java.io.InputStream#mark(int)
   740      * @see     java.io.InputStream#reset()
   740      * @see     java.io.InputStream#reset()
   741      */
   741      */
   742     public boolean markSupported() {
   742     public boolean markSupported() {
   743         return false;
   743         return false;