【Java IO】 四、Writer和Reader源码阅读

20160421004327228.png

1.Writer

官方描述: 写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

继承了三个接口
public abstract class Writer implements Appendable, Closeable, Flushable {


//缓存区 ,如欲写入String/CharSequence将使用该buffer
    private char[] writeBuffer;
//默认的大小,如写入的String大于该缓冲区,则会新建相应大小的buffer
    private static final int WRITE_BUFFER_SIZE = 1024;

  
    protected Object lock;

    protected Writer() {
        this.lock = this;
    }

    protected Writer(Object lock) {
        if (lock == null) {
            throw new NullPointerException();
        }
        this.lock = lock;
    }

    public void write(int c) throws IOException {
//加锁
        synchronized (lock) {
//如果buffer未初始化,则初始化
            if (writeBuffer == null){
                writeBuffer = new char[WRITE_BUFFER_SIZE];
            }
//将 写入的int c 的char值写入到 char数组中
            writeBuffer[0] = (char) c;
            write(writeBuffer, 0, 1);
        }
    }
//调用抽象的方法
    public void write(char cbuf[]) throws IOException {
        write(cbuf, 0, cbuf.length);
    }

//**核心方法**,将char数组中的内容写入流中,需子类实现
    abstract public void write(char cbuf[], int off, int len) throws IOException;


    public void write(String str) throws IOException {
        write(str, 0, str.length());
    }

    public void write(String str, int off, int len) throws IOException {
//加锁
        synchronized (lock) {
//一个chars数组,用来将String放入,再调用write(char cbuf[])
            char cbuf[];
//欲写入的字符串长度如果小于buffer,就使用默认的buffer,如果大于,就创
//建len长度的char[]
            if (len <= WRITE_BUFFER_SIZE) {
                if (writeBuffer == null) {
                    writeBuffer = new char[WRITE_BUFFER_SIZE];
                }
                cbuf = writeBuffer;
            } else {    // Don't permanently allocate very large buffers.
                cbuf = new char[len];
            }
            str.getChars(off, (off + len), cbuf, 0);
//调用抽象的方法,需子类进行实现
            write(cbuf, 0, len);
        }
    }


    public Writer append(CharSequence csq) throws IOException {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
        return this;
    }

    public Writer append(CharSequence csq, int start, int end) throws IOException {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(start, end).toString());
        return this;
    }

    public Writer append(char c) throws IOException {
        write(c);
        return this;
    }
//子类需实现
    abstract public void flush() throws IOException;

    abstract public void close() throws IOException;

1.Reader


public abstract class Reader implements Readable, Closeable {


    protected Object lock;

//将new出的对象的引用赋值给lock
    protected Reader() {
        this.lock = this;
    }

 //用一个给定的对象来实现同步
    protected Reader(Object lock) {
        if (lock == null) {
            throw new NullPointerException();
        }
        this.lock = lock;
    }

 //将流读入到buffer中
    public int read(java.nio.CharBuffer target) throws IOException {
        int len = target.remaining();
        char[] cbuf = new char[len];
        int n = read(cbuf, 0, len);
        if (n > 0)
            target.put(cbuf, 0, n);
        return n;
    }

 //读一个单独的字符
    public int read() throws IOException {
        char cb[] = new char[1];
        if (read(cb, 0, 1) == -1)
            return -1;
        else
            return cb[0];
    }


    public int read(char cbuf[]) throws IOException {
        return read(cbuf, 0, cbuf.length);
    }

//**核心方法**,将流读入字符数组,返回读入的字符个数, 需子类实现
    abstract public int read(char cbuf[], int off, int len) throws IOException;


    private static final int maxSkipBufferSize = 8192;

    private char skipBuffer[] = null;


    public long skip(long n) throws IOException {
        if (n < 0L)
            throw new IllegalArgumentException("skip value is negative");
        int nn = (int) Math.min(n, maxSkipBufferSize);
        synchronized (lock) {
            if ((skipBuffer == null) || (skipBuffer.length < nn))
                skipBuffer = new char[nn];
            long r = n;
            while (r > 0) {
                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
                if (nc == -1)
                    break;
                r -= nc;
            }
            return n - r;
        }
    }

//这个流是否准备好被读
    public boolean ready() throws IOException {
        return false;
    }

 //是否支持mark操作
    public boolean markSupported() {
        return false;
    }


    public void mark(int readAheadLimit) throws IOException {
        throw new IOException("mark() not supported");
    }

    public void reset() throws IOException {
        throw new IOException("reset() not supported");
    }

//关闭该流,释放占用的资源
     abstract public void close() throws IOException;

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 说明 FileFile类函数列表即释义File类API典型应用当前目录的子目录下,再新建一个目录新建文件的几种常用...
    HikariCP阅读 7,202评论 0 3
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 7,073评论 0 4
  • 概述 java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。java.io ...
    Steven1997阅读 13,037评论 1 25
  • 1、我与老俞只差两本书的距离 俞敏洪在在《告别2017,我的年度清单》一文中谈到,2017年他读了95本书,而我在...
    我是太阳会发光xl阅读 4,328评论 0 1
  • 蓝天,稀疏几絮白云。 飞机, 一架,又一架,划过。 瓦舍,缺着小口, 补上了光芒。 耳边风吹过, 抬起头,眼睛扑闪...
    叶千祈阅读 1,328评论 0 0

友情链接更多精彩内容