天天看点

JAVA IO源码学习系列之InputStream

JAVA IO源码学习系列

## 1.字节输入流源码InputStream介绍##

  1. InpuSteam是字节输入流操作的超类(抽象类),定义了基本的一些操作方法,流的操作大概四部分:(1)构造数据流(来源);(2)读取流;(3)读取流则需要判断是否可读;(4):操作结束要记得关闭;所以主要关注的方法:构造函数(构造数据),available()判断当前数据是否还能继续读取,read()读取数据,close()结束之后一定关闭相关资源;下面是具体的源码:
public abstract class InputStream implements Closeable {

    //默认可以跳过的最大范围
    private static final int MAX_SKIP_BUFFER_SIZE = ;

    //读取字节流(子类具体实现)
    public abstract int read() throws IOException;

    //将字节流写入具体的字节数组b[]
    public int read(byte b[]) throws IOException {
        return read(b, , b.length);
    }

    //将字节流写入具体的字节数组b[],从指定的位置读off,和读具体的大小len
    public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
            //如果读取的大小超过了具体字节数组的容量大小,则抛出数组越界
        } else if (off <  || len <  || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == ) {
            return ;
        }

        int c = read();
        if (c == -) {
            return -;
        }
        b[off] = (byte)c;

        int i = ;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -) {
                    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 <= ) {
            return ;
        }

        int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
        byte[] skipBuffer = new byte[size];
        while (remaining > ) {
            nr = read(skipBuffer, , (int)Math.min(size, remaining));
            if (nr < ) {
                break;
            }
            remaining -= nr;
        }

        return n - remaining;
    }

    // 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
    public int available() throws IOException {
        return ;
    }
    //关闭此输入流并释放与该流关联的所有系统资源。
    public void close() throws IOException {}

    //在此输入流中标记当前的位置。
    public synchronized void mark(int readlimit) {}

    将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
    public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }
    测试此输入流是否支持 mark 和 reset 方法。
    public boolean markSupported() {
        return false;
    }
           

InputSteam主要抽象了一般的方法,具体实现通过不同的子类,在此就不多做介绍;

神兽出发:

/**
 *        ┌─┐           ┌─┐    
 *   ┌──┘ ┴───────┘ ┴──┐
 *   │                           │
 *   │          ───            │
 *   ███████───███████│+
 *   │                           │+
 *   │         ─┴─             │
 *   │                           │
 *   └───┐         ┌───----┘
 *         │         │
 *         │         │   + +
 *         │         │
 *         │         └──────────────┐
 *         │                        │
 *         │                        ├─┐
 *         │                        ┌─┘
 *         │                        │
 *         └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘  + + + +
 *                神兽保佑
 *               代码无BUG!
 */