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 = 2048;

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

    //将字节流写入具体的字节数组b[]
    public int read(byte b[]) throws IOException {
        return read(b, 0, 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 < 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;
    }

    // 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
    public int available() throws IOException {
        return 0;
    }
    //关闭此输入流并释放与该流关联的所有系统资源。
    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!
 */
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值