(一)InputStream与OutputStream

本文详细解析了Java中InputStream和OutputStream两个核心类的源码。介绍了InputStream作为字节输入流基类的角色及其主要方法read()的实现方式,并讨论了其性能影响因素。同时,也分析了OutputStream作为字节输出流基类的功能和write()方法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

InputStream源码

总结:

  1. InputStream是一个抽象类,所有字节输入流都要继承该类。
  2. InputStream定义了对字节输入操作的基本方法,其中read()是抽象方法,需要子类实现。
  3. read(byte b[], int off, int len)的底层实现是循环调用read()方法。循环调用read()导致I/O访问频繁,限制了性能,大部分的子类都会对其进行重写。
package java.io;
public abstract class InputStream implements Closeable {
    //可以跳过的最大的字节数
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;
    public abstract int read() throws IOException;
    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

    //可以发现这里一次性读取一个字节数组的底层实现实际是循环调用read();将多次调用I/O
    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;
        }
        //这里先读一个字节再循环,感觉不到什么作用?直接循环不可以吗?
        //for (int i=0; i < len ; i++) {
        //        c = read();
        //        if (c == -1) {
        //            break;
        //        }
        //        b[off + i] = (byte)c;
        //    }

        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) {}
    public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }

    public boolean markSupported() {
        return false;
    }

}

OutputStream源码:

总结:

  1. OutputStream是所有字节输出流的父类
  2. 该类定义了对字节输出操作的基本方法,只要write()需要底层实现。
package java.io;
public abstract class OutputStream implements Closeable, Flushable {

    public abstract void write(int b) throws IOException;


    public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }


    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 {
    }

}




参考文章

Java io系列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值