Java 日看一类(51)之IO包中的Reader类

该类完成了Readable、Closeable接口

无引入其他包




该类的类头注释如下:

/**
 * Abstract class for reading character streams.  The only methods that a
 * subclass must implement are read(char[], int, int) and close().  Most
 * subclasses, however, will override some of the methods defined here in order
 * to provide higher efficiency, additional functionality, or both.
 *
 *
 * @see BufferedReader
 * @see   LineNumberReader
 * @see CharArrayReader
 * @see InputStreamReader
 * @see   FileReader
 * @see FilterReader
 * @see   PushbackReader
 * @see PipedReader
 * @see StringReader
 * @see Writer
 *
 * @author      Mark Reinhold
 * @since       JDK1.1
 */

大意如下:

字符读入流的抽象类

该类子类必须完成read()(数组读取方法),close()方法

然而,该类的大部分子类都会覆写该类的声明的方法来提供更高的便利性

或者增加一些独有的方法



该类含有如下的成员变量:

同步线程锁(在一段时间内 仅有一个线程能触发该Reader

protected Object lock;

最大缓冲限制

private static final int maxSkipBufferSize = 8192;

跳过的字符的缓冲区

private char skipBuffer[] = null;




该类含有如下成员方法:

默认构造方法(初始化线程锁

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

构造方法(提供其他流,对那个流加锁

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

读取数据到字符缓冲区

public int read(java.nio.CharBuffer target) throws IOException {
    int len = target.remaining();//获得当前CharBuffer的有效剩余可读取容量
    char[] cbuf = new char[len];
    int n = read(cbuf, 0, len);//把传入数据写入缓存
    if (n > 0)
        target.put(cbuf, 0, n);//需要完成put(char)方法,应该传入的是charbuffer的子类(string一类)
    return n;
}

返回读取字节的UTF-8码

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;

跳过后续的n个字符

public long skip(long n) throws IOException {
    if (n < 0L)
        throw new IllegalArgumentException("skip value is negative");
    int nn = (int) Math.min(n, maxSkipBufferSize);//nn为传入址和限制值的较小量
    synchronized (lock) {
        if ((skipBuffer == null) || (skipBuffer.length < nn))//缓冲区不存在或者缓冲区小于当前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;
}

是否支持标记操作(后续会加,基类一般只是给个接口

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;



该类是字符输入流的基类,本身功能比较简单,仅给出了大概的框架,具体实现可以去看看BufferReaderFilterReader

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值