Java 日看一类(28)之IO包中的Flushable接口和InputStream类

Flushable接口无继承类

引入了

import java.io.IOException;



该接口的类头注释如下:

/**
 * A <tt>Flushable</tt> is a destination of data that can be flushed.  The
 * flush method is invoked to write any buffered output to the underlying
 * stream.
 *
 * @since 1.5
 */

大意如下:

该类是可被输出数据输出的目标地

调用该方法可讲已缓存的输出写入底层输出流



该类含有一个方法

将缓冲推入底层流中

void flush() throws IOException;




该类的使用见前面的输出流,基本都有用到。该方法十分重要,与缓冲搭配提高IO速率;在流关闭时调用防止数据缺失等。




InputStream类无引入包

实现了Closeable接口


类头注释如下:

/**
 * This abstract class is the superclass of all classes representing
 * an input stream of bytes.
 *
 * <p> Applications that need to define a subclass of <code>InputStream</code>
 * must always provide a method that returns the next byte of input.
 *
 * @author  Arthur van Hoff
 * @see     java.io.BufferedInputStream
 * @see     java.io.ByteArrayInputStream
 * @see     java.io.DataInputStream
 * @see     java.io.FilterInputStream
 * @see     java.io.InputStream#read()
 * @see     java.io.OutputStream
 * @see     java.io.PushbackInputStream
 * @since   JDK1.0
 */

大意如下:

该抽象类是所有byte输入流的父类

应用程序需要声命该类的子类必须要提供返回输入流中下一个字节的方法



该类含有如下成员变量:

实施跳过操作时的最大缓冲区大小

private static final int MAX_SKIP_BUFFER_SIZE = 2048;




该类含有如下的成员方法:

从输入流中读取下一个字节

public abstract int read() throws IOException;

从输入流中读取该数组大小的字节

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

从输入流中读取进数组特定位置,特定长度的字节

public int read(byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();//空数组指针
    } else if (off < 0 || len < 0 || len > b.length - off) {//标识符越界
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }

    int c = read();
    if (c == -1) {//读取失败
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len ; i++) {//重复调用读取单个字节的方法并转格式储存
            c = read();
            if (c == -1) {
                break;
            }
            b[off + i] = (byte)c;
        }
    } catch (IOException ee) {
    }
    return i;//返回实际读取长度
}

跳过后续的n个字节

public long skip(long n) throws IOException {

    long remaining = n;
    int nr;

    if (n <= 0) {//需跳过长度小于0
        return 0;
    }

    int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);//创建一个不大于最大缓冲区大小的缓冲区
    byte[] skipBuffer = new byte[size];
    while (remaining > 0) {//重复装填
        nr = read(skipBuffer, 0, (int)Math.min(size, remaining));//将跳过的字节储存进缓冲区
        if (nr < 0) {
            break;
        }
        remaining -= nr;//减去已跳过的字节
    }

    return n - remaining;//返回实际跳过大小
}

返回缓冲区中可被直接读取或跳过的估计字节数

public int available() throws IOException {
    return 0;
}

关闭流

public void close() throws IOException {}

在当前位置设置标记点(传入值为回滚上限

public synchronized void mark(int readlimit) {}

回滚

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

判定当前类是否支持标记回滚操作

public boolean markSupported() {
    return false;
}


该类是所有字节输入类的父类,代码内容虽然没有什么,但是体系架构方式值得我们学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值