Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/InputStream.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* This abstract class is the superclass of all classes representing29* an input stream of bytes.30*31* <p> Applications that need to define a subclass of <code>InputStream</code>32* must always provide a method that returns the next byte of input.33*34* @author Arthur van Hoff35* @see java.io.BufferedInputStream36* @see java.io.ByteArrayInputStream37* @see java.io.DataInputStream38* @see java.io.FilterInputStream39* @see java.io.InputStream#read()40* @see java.io.OutputStream41* @see java.io.PushbackInputStream42* @since JDK1.043*/44public abstract class InputStream implements Closeable {4546// MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to47// use when skipping.48private static final int MAX_SKIP_BUFFER_SIZE = 2048;4950/**51* Reads the next byte of data from the input stream. The value byte is52* returned as an <code>int</code> in the range <code>0</code> to53* <code>255</code>. If no byte is available because the end of the stream54* has been reached, the value <code>-1</code> is returned. This method55* blocks until input data is available, the end of the stream is detected,56* or an exception is thrown.57*58* <p> A subclass must provide an implementation of this method.59*60* @return the next byte of data, or <code>-1</code> if the end of the61* stream is reached.62* @exception IOException if an I/O error occurs.63*/64public abstract int read() throws IOException;6566/**67* Reads some number of bytes from the input stream and stores them into68* the buffer array <code>b</code>. The number of bytes actually read is69* returned as an integer. This method blocks until input data is70* available, end of file is detected, or an exception is thrown.71*72* <p> If the length of <code>b</code> is zero, then no bytes are read and73* <code>0</code> is returned; otherwise, there is an attempt to read at74* least one byte. If no byte is available because the stream is at the75* end of the file, the value <code>-1</code> is returned; otherwise, at76* least one byte is read and stored into <code>b</code>.77*78* <p> The first byte read is stored into element <code>b[0]</code>, the79* next one into <code>b[1]</code>, and so on. The number of bytes read is,80* at most, equal to the length of <code>b</code>. Let <i>k</i> be the81* number of bytes actually read; these bytes will be stored in elements82* <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,83* leaving elements <code>b[</code><i>k</i><code>]</code> through84* <code>b[b.length-1]</code> unaffected.85*86* <p> The <code>read(b)</code> method for class <code>InputStream</code>87* has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>88*89* @param b the buffer into which the data is read.90* @return the total number of bytes read into the buffer, or91* <code>-1</code> if there is no more data because the end of92* the stream has been reached.93* @exception IOException If the first byte cannot be read for any reason94* other than the end of the file, if the input stream has been closed, or95* if some other I/O error occurs.96* @exception NullPointerException if <code>b</code> is <code>null</code>.97* @see java.io.InputStream#read(byte[], int, int)98*/99public int read(byte b[]) throws IOException {100return read(b, 0, b.length);101}102103/**104* Reads up to <code>len</code> bytes of data from the input stream into105* an array of bytes. An attempt is made to read as many as106* <code>len</code> bytes, but a smaller number may be read.107* The number of bytes actually read is returned as an integer.108*109* <p> This method blocks until input data is available, end of file is110* detected, or an exception is thrown.111*112* <p> If <code>len</code> is zero, then no bytes are read and113* <code>0</code> is returned; otherwise, there is an attempt to read at114* least one byte. If no byte is available because the stream is at end of115* file, the value <code>-1</code> is returned; otherwise, at least one116* byte is read and stored into <code>b</code>.117*118* <p> The first byte read is stored into element <code>b[off]</code>, the119* next one into <code>b[off+1]</code>, and so on. The number of bytes read120* is, at most, equal to <code>len</code>. Let <i>k</i> be the number of121* bytes actually read; these bytes will be stored in elements122* <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,123* leaving elements <code>b[off+</code><i>k</i><code>]</code> through124* <code>b[off+len-1]</code> unaffected.125*126* <p> In every case, elements <code>b[0]</code> through127* <code>b[off]</code> and elements <code>b[off+len]</code> through128* <code>b[b.length-1]</code> are unaffected.129*130* <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method131* for class <code>InputStream</code> simply calls the method132* <code>read()</code> repeatedly. If the first such call results in an133* <code>IOException</code>, that exception is returned from the call to134* the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If135* any subsequent call to <code>read()</code> results in a136* <code>IOException</code>, the exception is caught and treated as if it137* were end of file; the bytes read up to that point are stored into138* <code>b</code> and the number of bytes read before the exception139* occurred is returned. The default implementation of this method blocks140* until the requested amount of input data <code>len</code> has been read,141* end of file is detected, or an exception is thrown. Subclasses are encouraged142* to provide a more efficient implementation of this method.143*144* @param b the buffer into which the data is read.145* @param off the start offset in array <code>b</code>146* at which the data is written.147* @param len the maximum number of bytes to read.148* @return the total number of bytes read into the buffer, or149* <code>-1</code> if there is no more data because the end of150* the stream has been reached.151* @exception IOException If the first byte cannot be read for any reason152* other than end of file, or if the input stream has been closed, or if153* some other I/O error occurs.154* @exception NullPointerException If <code>b</code> is <code>null</code>.155* @exception IndexOutOfBoundsException If <code>off</code> is negative,156* <code>len</code> is negative, or <code>len</code> is greater than157* <code>b.length - off</code>158* @see java.io.InputStream#read()159*/160public int read(byte b[], int off, int len) throws IOException {161if (b == null) {162throw new NullPointerException();163} else if (off < 0 || len < 0 || len > b.length - off) {164throw new IndexOutOfBoundsException();165} else if (len == 0) {166return 0;167}168169int c = read();170if (c == -1) {171return -1;172}173b[off] = (byte)c;174175int i = 1;176try {177for (; i < len ; i++) {178c = read();179if (c == -1) {180break;181}182b[off + i] = (byte)c;183}184} catch (IOException ee) {185}186return i;187}188189/**190* Skips over and discards <code>n</code> bytes of data from this input191* stream. The <code>skip</code> method may, for a variety of reasons, end192* up skipping over some smaller number of bytes, possibly <code>0</code>.193* This may result from any of a number of conditions; reaching end of file194* before <code>n</code> bytes have been skipped is only one possibility.195* The actual number of bytes skipped is returned. If {@code n} is196* negative, the {@code skip} method for class {@code InputStream} always197* returns 0, and no bytes are skipped. Subclasses may handle the negative198* value differently.199*200* <p> The <code>skip</code> method of this class creates a201* byte array and then repeatedly reads into it until <code>n</code> bytes202* have been read or the end of the stream has been reached. Subclasses are203* encouraged to provide a more efficient implementation of this method.204* For instance, the implementation may depend on the ability to seek.205*206* @param n the number of bytes to be skipped.207* @return the actual number of bytes skipped.208* @exception IOException if the stream does not support seek,209* or if some other I/O error occurs.210*/211public long skip(long n) throws IOException {212213long remaining = n;214int nr;215216if (n <= 0) {217return 0;218}219220int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);221byte[] skipBuffer = new byte[size];222while (remaining > 0) {223nr = read(skipBuffer, 0, (int)Math.min(size, remaining));224if (nr < 0) {225break;226}227remaining -= nr;228}229230return n - remaining;231}232233/**234* Returns an estimate of the number of bytes that can be read (or235* skipped over) from this input stream without blocking by the next236* invocation of a method for this input stream. The next invocation237* might be the same thread or another thread. A single read or skip of this238* many bytes will not block, but may read or skip fewer bytes.239*240* <p> Note that while some implementations of {@code InputStream} will return241* the total number of bytes in the stream, many will not. It is242* never correct to use the return value of this method to allocate243* a buffer intended to hold all data in this stream.244*245* <p> A subclass' implementation of this method may choose to throw an246* {@link IOException} if this input stream has been closed by247* invoking the {@link #close()} method.248*249* <p> The {@code available} method for class {@code InputStream} always250* returns {@code 0}.251*252* <p> This method should be overridden by subclasses.253*254* @return an estimate of the number of bytes that can be read (or skipped255* over) from this input stream without blocking or {@code 0} when256* it reaches the end of the input stream.257* @exception IOException if an I/O error occurs.258*/259public int available() throws IOException {260return 0;261}262263/**264* Closes this input stream and releases any system resources associated265* with the stream.266*267* <p> The <code>close</code> method of <code>InputStream</code> does268* nothing.269*270* @exception IOException if an I/O error occurs.271*/272public void close() throws IOException {}273274/**275* Marks the current position in this input stream. A subsequent call to276* the <code>reset</code> method repositions this stream at the last marked277* position so that subsequent reads re-read the same bytes.278*279* <p> The <code>readlimit</code> arguments tells this input stream to280* allow that many bytes to be read before the mark position gets281* invalidated.282*283* <p> The general contract of <code>mark</code> is that, if the method284* <code>markSupported</code> returns <code>true</code>, the stream somehow285* remembers all the bytes read after the call to <code>mark</code> and286* stands ready to supply those same bytes again if and whenever the method287* <code>reset</code> is called. However, the stream is not required to288* remember any data at all if more than <code>readlimit</code> bytes are289* read from the stream before <code>reset</code> is called.290*291* <p> Marking a closed stream should not have any effect on the stream.292*293* <p> The <code>mark</code> method of <code>InputStream</code> does294* nothing.295*296* @param readlimit the maximum limit of bytes that can be read before297* the mark position becomes invalid.298* @see java.io.InputStream#reset()299*/300public synchronized void mark(int readlimit) {}301302/**303* Repositions this stream to the position at the time the304* <code>mark</code> method was last called on this input stream.305*306* <p> The general contract of <code>reset</code> is:307*308* <ul>309* <li> If the method <code>markSupported</code> returns310* <code>true</code>, then:311*312* <ul><li> If the method <code>mark</code> has not been called since313* the stream was created, or the number of bytes read from the stream314* since <code>mark</code> was last called is larger than the argument315* to <code>mark</code> at that last call, then an316* <code>IOException</code> might be thrown.317*318* <li> If such an <code>IOException</code> is not thrown, then the319* stream is reset to a state such that all the bytes read since the320* most recent call to <code>mark</code> (or since the start of the321* file, if <code>mark</code> has not been called) will be resupplied322* to subsequent callers of the <code>read</code> method, followed by323* any bytes that otherwise would have been the next input data as of324* the time of the call to <code>reset</code>. </ul>325*326* <li> If the method <code>markSupported</code> returns327* <code>false</code>, then:328*329* <ul><li> The call to <code>reset</code> may throw an330* <code>IOException</code>.331*332* <li> If an <code>IOException</code> is not thrown, then the stream333* is reset to a fixed state that depends on the particular type of the334* input stream and how it was created. The bytes that will be supplied335* to subsequent callers of the <code>read</code> method depend on the336* particular type of the input stream. </ul></ul>337*338* <p>The method <code>reset</code> for class <code>InputStream</code>339* does nothing except throw an <code>IOException</code>.340*341* @exception IOException if this stream has not been marked or if the342* mark has been invalidated.343* @see java.io.InputStream#mark(int)344* @see java.io.IOException345*/346public synchronized void reset() throws IOException {347throw new IOException("mark/reset not supported");348}349350/**351* Tests if this input stream supports the <code>mark</code> and352* <code>reset</code> methods. Whether or not <code>mark</code> and353* <code>reset</code> are supported is an invariant property of a354* particular input stream instance. The <code>markSupported</code> method355* of <code>InputStream</code> returns <code>false</code>.356*357* @return <code>true</code> if this stream instance supports the mark358* and reset methods; <code>false</code> otherwise.359* @see java.io.InputStream#mark(int)360* @see java.io.InputStream#reset()361*/362public boolean markSupported() {363return false;364}365366}367368369