InputStream是一个抽象类,实现了Closeable接口。InputStream是代表字节输入流的所有类的父类。程序想要定义一个InputStream抽象类的子类,则必须提供(实现)一个返回输入的下一个字节的方法。
来看看InputStream内部结构:

下面一个一个地来看这些方法。
close方法其实是实现Closeable接口的方法。并且InputStream的close方法是一个空实现。这意味着继承InputStream的子类虽然不是强制性要重写close方法,但还是建议根据业务进行重写。
- /**
- * Closes this input stream and releases any system resources associated
- * with the stream.
- *
- *
The close method of InputStream does
- * nothing.
- *
- * @exception IOException if an I/O error occurs.
- */
- public void close() throws IOException {}
read()方法从输入流中读取下一个字节的数据。返回的字节值是一个1~255区间的int类型值(字节值的十进制整形表示)。如果达到了输入流的末尾没有剩余的字节了。将返回-1。
该方法将阻塞线程,直到出现以下情况
1、输入数据可用;
2、检测到流的结尾;
3、抛出一个异常
注意:read()方法是一个抽象方法,即子类必须实现这个方法。
- /**
- * Reads the next byte of data from the input stream. The value byte is
- * returned as an
int in the range 0 to - *
255. If no byte is available because the end of the stream - * has been reached, the value
-1 is returned. This method - * blocks until input data is available, the end of the stream is detected,
- * or an exception is thrown.
- *
- *
A subclass must provide an implementation of this method.
- *
- * @return the next byte of data, or
-1 if the end of the - * stream is reached.
- * @exception IOException if an I/O error occurs.
- */
- public abstract int read() throws IOException;
从输入流中读取一些字节,并存储这些字节到缓冲数组b中。实际读取的字节数量以整数的形式返回(即read(byte b[])返回值是实际读取到的字节数量)。这也是一个阻塞方法,同read()。
如果字节数组b的长度为空,则读取不到字节并返回0。否则,将尝试读取至少一个字节。如果由于输入流到达了文件末尾导致没有剩余的字节,将返回-1;否则,至少读取一个字节并存储进数组数组b。
读取到的第一个字节被存到b[0]位置,下一个字节存入到b[1],以此类推。读取到的这些字节的数量最多等于数组b的长度。设k为实际读取的字节数量,这些字节将被存储到b[0]到b[k-1],而b[k]到b[b.length-1]之间的元素不会被影响。
read(b)方法和read(b, 0, b.length)作用是一样的。
可以看到,read(byte b[]) 实际上是调用read(byte b[], int off, int len)。
- /**
- * Reads some number of bytes from the input stream and stores them into
- * the buffer array
b. The number of bytes actually read is - * returned as an integer. This method blocks until input data is
- * available, end of file is detected, or an exception is thrown.
- *
- *
If the length of b is zero, then no bytes are read and
- *
0 is returned; otherwise, there is an attempt to read at - * least one byte. If no byte is available because the stream is at the
- * end of the file, the value
-1 is returned; otherwise, at - * least one byte is read and stored into
b. - *
- *
The first byte read is stored into element b[0], the
- * next one into
b[1], and so on. The number of bytes read is, - * at most, equal to the length of
b. Let k be the - * number of bytes actually read; these bytes will be stored in elements
- *
b[0] through b[k-1], - * leaving elements
b[k] through - *
b[b.length-1] unaffected. - *
- *
The read(b) method for class InputStream
- * has the same effect as:
read(b, 0, b.length)
- *
- * @param b the buffer into which the data is read.
- * @return the total number of bytes read into the buffer, or
- *
-1 if there is no more data because the end of - * the stream has been reached.
- * @exception IOException If the first byte cannot be read for any reason
- * other than the end of the file, if the input stream has been closed, or
- * if some other I/O error occurs.
- * @exception NullPointerException if
b is null. - * @see java.io.InputStream#read(byte[], int, int)
- */
- public int read(byte b[]) throws IOException {
- return read(b, 0, b.length);
- }
从输入流中读取最多len个字节数据进字节数组中。该方法尝试读取尽可能和len一样多的字节,但是可能读取到更小数量的字节。该方法返回一个整形数,代表实际读取到的字节数量。
其实可以直接看代码就能明白其是如何实现的。这个方法时InputStream抽象类的一个核心方法,读取的大部分逻辑都在这里,方法实现比较简单,但是其思想值得借鉴。
该方法体内部实际还是调用read()抽象方法来进行读取字节(在for循环中一个一个地读取字节)。这种思想很好地降低了代码耦合,它不需要关心是如何实现read()方法的。 在read()方法的基础上增加一系列逻辑就能读取字节数组了。抽象read()方法的具体实现交给子类去完成,抽象类把处理逻辑搭建好,这种思想在很多地方都用得到:比如android中的baseActivity就是在base基类中把逻辑流程都实现好,具体用到的某个方法在子类中实现。这样可以降低代码耦合,并大大地减少了重复代码。
- /**
- * Reads up to
len bytes of data from the input stream into - * an array of bytes. An attempt is made to read as many as
- *
len bytes, but a smaller number may be read. - * The number of bytes actually read is returned as an integer.
- *
- *
This method blocks until input data is available, end of file is
- * detected, or an exception is thrown.
- *
- *
If len is zero, then no bytes are read and
- *
0 is returned; otherwise, there is an attempt to read at - * least one byte. If no byte is available because the stream is at end of
- * file, the value
-1 is returned; otherwise, at least one - * byte is read and stored into
b. - *
- *
The first byte read is stored into element b[off], the
- * next one into
b[off+1], and so on. The number of bytes read - * is, at most, equal to
len. Let k be the number of - * bytes actually read; these bytes will be stored in elements
- *
b[off] through b[off+k-1], - * leaving elements
b[off+k] through - *
b[off+len-1] unaffected. - *
- *
In every case, elements b[0] through
- *
b[off] and elements b[off+len] through - *
b[b.length-1] are unaffected. - *
- *
The read(b, off, len) method
- * for class
InputStream simply calls the method - *
read() repeatedly. If the first such call results in an - *
IOException, that exception is returned from the call to - * the
read(b, off, len) method. If - * any subsequent call to
read() results in a - *
IOException, the exception is caught and treated as if it - * were end of file; the bytes read up to that point are stored into
- *
b and the number of bytes read before the exception - * occurred is returned. The default implementation of this method blocks
- * until the requested amount of input data
len has been read, - * end of file is detected, or an exception is thrown. Subclasses are encouraged
- * to provide a more efficient implementation of this method.
- *
- * @param b the buffer into which the data is read.
- * @param off the start offset in array
b - * at which the data is written.
- * @param len the maximum number of bytes to read.
- * @return the total number of bytes read into the buffer, or
- *
-1 if there is no more data because the end of - * the stream has been reached.
- * @exception IOException If the first byte cannot be read for any reason
- * other than end of file, or if the input stream has been closed, or if
- * some other I/O error occurs.
- * @exception NullPointerException If
b is null. - * @exception IndexOutOfBoundsException If
off is negative, - *
len is negative, or len is greater than - *
b.length - off - * @see java.io.InputStream#read()
- */
- public int read(byte b[], int off, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if (off < 0 || len < 0 || len > b.length - off) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- }
-
- int c = read();
- if (c == -1) {
- return -1;
- }
- b[off] = (byte)c;
-
- int i = 1;
- try {
- for (; i < len ; i++) {
- c = read(); //返回的是字节值的十进制表示方式,即ascii码值
- if (c == -1) {
- break;
- }
- b[off + i] = (byte)c; //c是字节值的十进制表示方法,这里需要转换成字节类型
- }
- } catch (IOException ee) {
- }
- return i;
- }
该方法跳过并丢弃输入流中的n个字节数据。由于一些原因,skip方法可能会跳过一些更小数量的字节,可能是0。这可能由多种情况中的任何一种造成;在跳过n个字节之前到达文件末尾是唯一的可能性。
返回的是实际跳过的字节数量值。如果n是负数,则该方法返回0,并且没有字节被跳过。子类可能以不同方式处理负值。
类中的skip方法创建一个字节数组,并在之后重复地读取字节进入数组中,直到读取了n个字节,或到达了输入流的末尾。鼓励子类提供一个更高效的实现。比如,该实现可能依赖于seek的能力。
- /**
- * Skips over and discards
n bytes of data from this input - * stream. The
skip method may, for a variety of reasons, end - * up skipping over some smaller number of bytes, possibly
0. - * This may result from any of a number of conditions; reaching end of file
- * before
n bytes have been skipped is only one possibility. - * The actual number of bytes skipped is returned. If {@code n} is
- * negative, the {@code skip} method for class {@code InputStream} always
- * returns 0, and no bytes are skipped. Subclasses may handle the negative
- * value differently.
- *
- *
The skip method of this class creates a
- * byte array and then repeatedly reads into it until
n bytes - * have been read or the end of the stream has been reached. Subclasses are
- * encouraged to provide a more efficient implementation of this method.
- * For instance, the implementation may depend on the ability to seek.
- *
- * @param n the number of bytes to be skipped.
- * @return the actual number of bytes skipped.
- * @exception IOException if the stream does not support seek,
- * or if some other I/O error occurs.
- */
- public long skip(long n) throws IOException {
-
- long remaining = n;
- int nr;
-
- if (n <= 0) {
- return 0;
- }
-
- int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
- byte[] skipBuffer = new byte[size];
- while (remaining > 0) {
- nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
- if (nr < 0) {
- break;
- }
- remaining -= nr;
- }
-
- return n - remaining;
- }
skip内部具体实现是值得研究一下的。假如输入流中的字节数量大于skip方法的参数n,则可以成功跳过n个字节;相反情况则在循环中nr = read(skipBuffer, 0, (int)Math.min(size, remaining));读取到输入流末尾时将返回-1,此时if判断内部会跳出循环。remaining代表还剩多少个字节没有被跳过,所以返回值为n - remaining。这种情况下,实际返回skip数量值就小于参数n。
在不会被输入流的下一次方法调用阻塞情况下,返回可从此输入流读取(或跳过)的字节数的估计值。下次调用可能是在相同线程或其他线程。一次简单的读取或跳过许多字节将不会被阻塞,但可能读取或跳过更少的字节。
注意,尽管一些InputStream的实现类将返回输入流中字节数量的总值,但大部分InputStream的实现类却不会这么做。使用此方法的返回值来分配用于保存此流中所有数据的缓冲区永远都是错误的。
如果已经调用过close方法关闭了输入流,则该方法的子类实现可能选择抛出一个IOException(即假如一个输入流已经被close了,再调用该输入流的available()方法将抛出异常)。
最后,这个方法应该被子类重写。
- /**
- * Returns an estimate of the number of bytes that can be read (or
- * skipped over) from this input stream without blocking by the next
- * invocation of a method for this input stream. The next invocation
- * might be the same thread or another thread. A single read or skip of this
- * many bytes will not block, but may read or skip fewer bytes.
- *
- *
Note that while some implementations of {@code InputStream} will return
- * the total number of bytes in the stream, many will not. It is
- * never correct to use the return value of this method to allocate
- * a buffer intended to hold all data in this stream.
- *
- *
A subclass' implementation of this method may choose to throw an
- * {@link IOException} if this input stream has been closed by
- * invoking the {@link #close()} method.
- *
- *
The {@code available} method for class {@code InputStream} always
- * returns {@code 0}.
- *
- *
This method should be overridden by subclasses.
- *
- * @return an estimate of the number of bytes that can be read (or skipped
- * over) from this input stream without blocking or {@code 0} when
- * it reaches the end of the input stream.
- * @exception IOException if an I/O error occurs.
- */
- public int available() throws IOException {
- return 0;
- }
mark(int readlimit)、reset()和markSupported()。