美文网首页java语言基础
【Java IO】 四、Writer和Reader源码阅读

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

作者: 6d898101c4c9 | 来源:发表于2018-11-19 23:56 被阅读6次
    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;
    
    }
    
    
    

    相关文章

      网友评论

        本文标题:【Java IO】 四、Writer和Reader源码阅读

        本文链接:https://www.haomeiwen.com/subject/hwinfqtx.html