Java IO 系列源码分析——InputStream和OutputStream

来源:互联网 发布:2017最新一手数据 编辑:程序博客网 时间:2024/05/18 00:02

InputStreamOutputStream是字节流的超类,它们分别提供了read()write()方法来读取和写入字节。下面就InputStreamOutputStream源码进行简单分析。
1. InputStream源码分析(基于jdk1.8.0_66)

package java.io;public abstract class InputStream implements Closeable {    //最大能skip的数目    private static final int MAX_SKIP_BUFFER_SIZE = 2048;    //抽象方法,需要子类去实现    //从输入流中读取数据的下一个字节    public abstract int read() throws IOException;    //将输入流中的数据读入byte数组    public int read(byte b[]) throws IOException {        return read(b, 0, b.length);    }    //将输入流中最多len个字节读取到byte数组的off起始位置及后续位置    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();                if (c == -1) {                    break;                }                b[off + i] = (byte)c;            }        } catch (IOException ee) {        }        return i;    }    //跳过输入流的n个字节    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;    }    //获取输入流中还可以读取或者跳过的字节数(大致数目)    //在超类InputStream中该方法永远返回0。    public int available() throws IOException {        return 0;    }    public void close() throws IOException {}    //给当前位置设置标志,当之后第一次使用reset方法时,将会从该标志位重新读取后续数据    //readlimit参数目前没有实际意义,可以设置为任意值    public synchronized void mark(int readlimit) {}    public synchronized void reset() throws IOException {        throw new IOException("mark/reset not supported");    }    //标注方法是否支持mark方法,超类InputStream不支持。    public boolean markSupported() {        return false;    }}
  1. OutputStream源码分析(基于jdk1.8.0_66)
package java.io;public abstract class OutputStream implements Closeable, Flushable {    //将字节b写入到输出流中,该方法需要由子类实现。    public abstract void write(int b) throws IOException;    //将byte数组中的字节数据写入到字节输出流中。    public void write(byte b[]) throws IOException {        write(b, 0, b.length);    }    //将字节数组b中off起始位置开始的len长度的数据写入到字节输出流中    public void write(byte b[], int off, int len) throws IOException {        if (b == null) {            throw new NullPointerException();        } else if ((off < 0) || (off > b.length) || (len < 0) ||                   ((off + len) > b.length) || ((off + len) < 0)) {            throw new IndexOutOfBoundsException();        } else if (len == 0) {            return;        }        for (int i = 0 ; i < len ; i++) {            write(b[off + i]);        }    }    //刷新字节输出流,将输出流中缓存的字节数据强制刷新输出    public void flush() throws IOException {    }    public void close() throws IOException {    }}

InputStreamOutputStream作为字节流的超类,定义了读取和写入字节的read()write()方法,从源代码中我们可以看到这两个方法需要子类根据自身需求进行实现(工厂方法)。在下面系列文章中我们将进一步分析具体子类的源码,观察各个子类是怎样实现和扩展read()wirte()方法的。

0 0
原创粉丝点击