CharSequenceReader 类是以 CharSequence 的形式读取字符 CharSequenceReader 类继承自 Reader 类, 除了 remaining() hasremaining() 以及 checkopen() 函数之后, 其他的函数都是重写 Reader 类中的函数 CharSequenceReader 类声明没有用 public 关键字, 所以我们暂时还不能调用这个类 CharSequenceReader 类有下面三个成员变量 private CharSequence seq; // 存放字符序列 private int pos; // 存放上述字符序列的下一次读 private int mark; // 可以记录 pos 的位置, 用于下一次重置到 pos 位置 CharSequenceReader 类只有一个构造函数如下 : public CharSequenceReader(CharSequence seq) { this.seq = checknotnull(seq); 这个函数非常的简单, 就是将 seq 赋值给 CharSequenceReader 类的 seq, 使得其他方法可以操作 s eq private void checkopen() throws IOException { if (seq == null) { throw new IOException("reader closed"); private boolean hasremaining() { return remaining() > 0; private int remaining() { return seq.length() - pos; 1 / 5
上面三个函数是 CharSequenceReader 类专有的 checkopen() 函数主要是判断 seq 是否没有被清空, 如果被清空了, 抛出空指针异常 ; 否则什么事都不做 其他的函数实现几乎都用到了 c heckopen hasremaining 函数主要是判断当前是否还有数据可以读 在 CharSequenceReader 类中提供了三个用于读 seq 中数据的函数, 原型如下 : public synchronized int read(charbuffer target) throws IOException public synchronized int read() throws IOException public synchronized int read(char[] cbuf, int off, int len) throws IOException 上述上个函数都是重写自 Reader 类相关的函数,read(CharBuffer target) 函数实现如下 : public synchronized int read(charbuffer target) throws IOException { checknotnull(target); if (!hasremaining()) { return -1; int charstoread = Math.min(target.remaining(), remaining()); for (int i = 0; i < charstoread; i++) { target.put(seq.charat(pos++)); return charstoread; 先判断 seq 中是否没被清空 ; 接着判断 seq 中还有数据可读与否, 如果没有数据可读, 将返回 -1,; 否则得到 target 和 seq 的最小可读空间大小, 并从 seq 读取相应的数据于 target 中 其实 target.put 函数是将一个字符存放在内部 char 数组的相应位置上面去 read 函数最后返回本次读取字符的个数 read() 函数实现如下 : public synchronized int read() throws IOException { return hasremaining()? seq.charat(pos++) : -1; 2 / 5
可以看出, 这个函数实现相当的简单, 先判断 seq 中是否没被清空 ; 接着判断 seq 中还有数据可读与否, 如果有数据可读, 则返回 seq 中下标为 pos 的数据, 且 pos 向后移动一个位置 ; 如果没有数据可读, 则之间返回 -1 read() 函数最多只返回一个字符 read(char[] cbuf, int off, int len) 函数实现如下 : public synchronized int read(char[] cbuf, int off, int len) throws IOException { checkpositionindexes(off, off + len, cbuf.length); if (!hasremaining()) { return -1; int charstoread = Math.min(len, remaining()); for (int i = 0; i < charstoread; i++) { cbuf[off + i] = seq.charat(pos++); return charstoread; 大题和 read(charbuffer target) 函数实现类似 它主要是将读取的到字符存放在 cbuf 下标从 off 开始的位置, 并且依次读取 charstoread 个, 最后返回本次读取到的字符个数 接下来说说 skip(long n) 函数, 它的实现如下 : public synchronized long skip(long n) throws IOException { checkargument(n >= 0, "n (%s) may not be negative", n); // safe because remaining is an int int charstoskip = (int) Math.min(remaining(), n); pos += charstoskip; return charstoskip; 主要是通过移动 pos 指标, 从而达到忽略 seq 中 charstoskip 个字符 上面我们就说了 CharSequen 3 / 5
cereader 类中大多数的函数都是重写 Reader 类的, 阅读 Reader 类中的 skip(long n) 函数我们可以看到, 它忽略了 charstoskip 个字符的同时还保存了本次被忽略的 charstoskip 个字符于 char skipbuffer[] 数组中 剩下的几个函数为 public synchronized boolean ready() throws IOException { return true; public boolean marksupported() { return true; public synchronized void mark(int readaheadlimit) throws IOException { checkargument(readaheadlimit >= 0, "readaheadlimit (%s) may not be negative", readaheadlimit); mark = pos; public synchronized void reset() throws IOException { pos = mark; public synchronized void close() throws IOException { seq = null; 都是比较简单的,ready() 函数简单的判断 seq 是否没被清空, 从而返回 true 或者抛出异常 ( 见 checkopen() 函数 ) marksupported() 函数只是简单的标识 CharSequenceReader 类支持标记这个方法 mark 函数主要是记录下 seq 当前的下标 reset() 函数是将当前的下标重置为 mark clo se() 函数主要是清空 seq 中的数据 我们可以从源码中发现,CharSequenceReader 类中绝大部分的函数都是用了 synchronized 修饰的, 这使得每一次只有一个线程执行相关的函数 ( 完 ) 本博客文章除特别声明, 全部都是原创! 4 / 5
Powered by TCPDF (www.tcpdf.org) 转载本文请加上 : 转载自过往记忆 (https://www.iteblog.com/) 本文链接 : () 5 / 5