博客园 首页 私信博主 显示目录 隐藏目录 管理

Reader 的API简单使用

1、Reader API简介:
A:关键字
protected Object lock; 用于同步针对此流的操作的对象。

B:构造方法
例子
protected Reader(); 创建一个新的字符流 reader,其重要部分将同步其自身的 reader。

protected Reader(Object obj); 创建一个新的字符流 reader,其重要部分将同步给定的对象

C:一般方法

abstract void close(); 关闭此流、释放与此流有关的所有资源

boolean markSupported(); 检测此流是否支持标记
void mark(int readAheadLimit); 标记此流、与reset()结合使用

void reset(); 将流的读取位置重置到最后一次调用mark方法时的位置

boolean ready(); 检测此流是否可读

int read(java.nio.CharBuffer target); 将输入流管道中的target.length个字节读取到指定的缓存字符数组target中、返回实际存放到target中的字符数。

int read(); 读取一个字符并返回

int read(char[] cbuf); 将字符读取到cbuf中、返回实际读取到的字符数。

abstract int read(char[] cbuf, int off, int len); 抽象类:将字符读取到下标冲off开始、len个后续位置的cbuf中。

long skip(long n); 从剩余有效字符中跳过n个

package com.chy.io.core;

import java.io.Closeable;
import java.io.IOException;
/**
 * Reader:字符输入流、本身是抽象类、为所有字符输入流类提供一个标准、和基本方法及简单实现、与InputStream相比、多实现一个Readble接口、
 * 支持将字符读取到指定的缓存字符数组中。要求子类重写read(char[] cbuf, int off, int len)、close()、这两个方法、一般子类都会重写Reader提供的方法
 * 或者部分方法、也可能添加新的方法来体现自己的特色。与InputStream的本质区别就是操作单位不同、由字节变为字符。
 */

public abstract class Reader implements Readable, Closeable {
    /**
     * 用于同步针对此流的操作的对象。
     */
    protected Object lock;

    /**
     * 创建一个新的字符流 reader,其重要部分将同步其自身的 reader。
     */
    protected Reader() {
        this.lock = this;
    }
    /**
     * 创建一个新的字符流 reader,其重要部分将同步给定的对象。
     */
    protected Reader(Object lock) {
        if (lock == null) {
            throw new NullPointerException();
        }
        this.lock = lock;
    }

    /**
     * 将输入流管道中的target.length个字节读取到指定的缓存字符数组target中、返回实际存放到target中的字符数。
     */
    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];
    }
    /**
     * 将字符读取到cbuf中、返回实际读取到的字符数。
     */
    public int read(char cbuf[]) throws IOException {
        return read(cbuf, 0, cbuf.length);
    }

    /**
     * 抽象类:将字符读取到下标冲off开始、len个后续位置的cbuf中。
     */
    abstract public int read(char cbuf[], int off, int len) throws IOException;

    /** 最大允许跳过字符数 */
    private static final int maxSkipBufferSize = 8192;

    /** 用于skip方法临时存放字符 */
    private char skipBuffer[] = null;
    /**
     * 从剩余有效字符中跳过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);
        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;
    }
    /** 检测此流是否支持标记*/
    public boolean markSupported() {
        return false;
    }

    /** 标记此流、与reset()结合使用*/
    public void mark(int readAheadLimit) throws IOException {
        throw new IOException("mark() not supported");
    }

    /** 将流的读取位置重置到最后一次调用mark方法时的位置*/
    public void reset() throws IOException {
        throw new IOException("reset() not supported");
    }

    /** 关闭此流、释放与此流有关的所有资源*/
     abstract public void close() throws IOException;
}
View Code
posted @ 2018-04-15 10:35  mingjone_sherman  阅读(636)  评论(0编辑  收藏  举报