Java8 I/O源码-ByteArrayInputStream

OutputStream这个抽象类是所有表示字节输出流的类的父类。输出流接受输出字节并将这些字节发送到某个“池”。继承这个抽象类的类必须提供至少一种可写入一个输出字节的方法。
摘要由CSDN通过智能技术生成

前两篇文章Java8 I/O源码-InputStreamJava8 I/O源码-OutputStream简单介绍了IntputStream和OutputStream。本文将详细介绍ByteArrayInputStream的实现,拉开学习字节型输入流的序幕。

ByteArrayInputStream属于字节型输入流,包含一个内部缓冲区,该缓冲区包含从流中读取的字节。定义如下:

public class ByteArrayInputStream extends InputStream

outline
字段
字段 说明
protected byte buf[] 用来创建流的字节数组。
protected int pos 从输入流缓冲器中读取的下个字节的索引。
protected int mark = 0 流中的当前标记位置。
protected int count 字节流的长度
构造方法
方法 说明
public ByteArrayInputStream(byte buf[]) {…} 创建一个ByteArrayInputStream,使用buf作为其缓冲区数组。
public ByteArrayInputStream(byte buf[], int offset, int length) {…} 创建一个ByteArrayInputStream,使用buf作为其缓冲区数组,并指定标记位置和可以从缓冲区中可以读取字节的最大长度。
方法
方法 说明
public synchronized int read() {…} 从此输入流中读取下一个数据字节。
public synchronized int read(byte b[], int off, int len) {…} 将最多len个数据字节从此输入流读入byte数组。
public synchronized long skip(long n) {…} 从此输入流中跳过n个输入字节。
public synchronized int available() {…} 返回可从此输入流读取(或跳过)的剩余字节数。
public boolean markSupported() {…} 测试是否支持 mark/reset。
public void mark(int readAheadLimit) {…} 设置流中的当前标记位置。
public synchronized void reset() {…} 将缓冲区的位置重置为标记位置。
public void close() throws IOException {…} 关闭ByteArrayInputStream,此方法无效。
构造方法
ByteArrayInputStream( byte buf[])
/**
 * 创建一个ByteArrayInputStream,使用buf作为它的缓冲区数组。
 * 
 * pos的初始值为0。
 * count的初始值为buf的长度。
 *
 * @param   buf   作为输入缓存区的字节数组。
 */
public ByteArrayInputStream(byte buf[]) {
    // 使用buf作为缓冲区数组。
    this.buf = buf;
    // pos的初始值为0。
    this.pos = 0;
    // count的初始值为buf的长度。
    this.count = buf.length;
}
ByteArrayInputStream( byte buf[], int offset, int length)
/**
 * 创建一个ByteArrayInputStream,使用buf作为它的缓冲区数组。
 * pos的初始值为offset。
 * count的初始值为offset+length和buf.length的最小值。
 * 将mark的值设为offset。
 *
 * @param   buf      作为输入缓存区的
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值