JAVA基础 | IO之ByteArrayOutputStream与ByteArrayInputStream

本文是关于JAVA IO系列的文章,详细介绍了ByteArrayOutputStream和ByteArrayInputStream的用途、构造方法、涉及的变量和方法。ByteArrayOutputStream用于将数据写入字节数组,其缓冲区会自动增长;ByteArrayInputStream则包含一个内部缓冲区,可以从流中读取字节。两者都是Java IO操作的重要组成部分。

IO系列文章目录

添加链接描述



前言

ByteArrayOutputStream与ByteArrayInputStream的区别


提示:以下是本篇文章正文内容

一、ByteArrayOutputStream

1. 简介

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.
The data can be retrieved using toByteArray() and toString().

这个类继承输出流,其中数据被写入一个字节数组。
缓冲区会随着数据的写入而自动增长。
可以使用toByteArray()和toString()来检索数据代码。

2. 构造方法

//
public ByteArrayOutputStream() {
        this(32);
    }
    
public ByteArrayOutputStream(int size) {
        if (size < 0) {
            throw new IllegalArgumentException("Negative initial size: "
                                               + size);
        }
        buf = new byte[size];
    }

3. 涉及变量

//数据缓冲区
protected byte buf[];
//缓冲区有效字节数
protected int count;

4. 涉及方法

 / * *
    *如有需要,增加容量,以确保它可以容纳最少指定元素的数目最小能力参数。
   * */
private void ensureCapacity(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - buf.length > 0)
            grow(minCapacity);
    }
    
    / * *
    *将指定的字符写入缓冲区
   * */
public synchronized void write(int b) {
        ensureCapacity(count + 1);
        buf[count] = (byte) b;
        count += 1;
    }
    
    /**
     * 将一定范围的字节,写入缓冲区
    **/
public synchronized void write(byte b[], int off, int len) {
        if ((off < 0) || (off > b.length) || (len < 0) ||
            ((off + len) - b.length > 0)) {
            throw new IndexOutOfBoundsException();
        }
        ensureCapacity(count + len);
        System.arraycopy(b, off, buf, count, len);
        count += len;
    }
    
    /**
     * 写入另一个流
    **/
public synchronized void writeTo(OutputStream out) throws IOException {
        out.write(buf, 0, count);
    }
    
    /**
     * 重置到mark位置
     **/
public synchronized void reset() {
        count = 0;
    }

二、ByteArrayOutputStream

1. 简介

A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.
An internal counter keeps track of the next byte to be supplied by the read method.
toByteArray() and toString().

一个包含字节的内部缓冲区可以从流中读取。
一个内部计数器跟踪下一个字节到read。

2. 构造方法

//接收数组buf全部字节
public ByteArrayInputStream(byte[] buf) {
    this.buf = buf;
    this.pos = 0;
    this.count = buf.length;
}
//接受数组buf,选择读取范围,pos开始读取的位置,mark标记位与pos相同
public ByteArrayInputStream(byte[] buf, int offset, int length) {
    this.buf = buf;
    this.pos = offset;
    this.count = Math.min(offset + length, buf.length);
    this.mark = offset;
}

3. 涉及变量

//数据缓冲区
protected byte[] buf;
//偏移位
protected int pos;
protected int mark = 0;
//字节总长度
protected int count;

4. 涉及方法

 / * *
    *读取缓冲区
   * */
public synchronized int read() {
        return (pos < count) ? (buf[pos++] & 0xff) : -1;
    }
    
    / * *
    *读取缓冲区
   * */
public synchronized int read(byte b[], int off, int len) {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        }

        if (pos >= count) {
            return -1;
        }

        int avail = count - pos;
        if (len > avail) {
            len = avail;
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(buf, pos, b, off, len);
        pos += len;
        return len;
    }
    
    /**
     * 剩余量
    **/
public synchronized int available() {
        return count - pos;
    }
    
    /**
     * 重置到mark位置
    **/
public synchronized void reset() {
        pos = mark;
    }
    
    /**
     * 记录当前位置,readAheadLimit在这里没有意义
     **/
public void mark(int readAheadLimit) {
        mark = pos;
    }

举例

private static void test3() throws IOException {

        byte[] bytes = "hello boy".getBytes();
        //将字节写入文件,构造器默认大小为32位
        ByteArrayOutputStream write = new ByteArrayOutputStream(64);
        ByteArrayOutputStream write1 = new ByteArrayOutputStream(64);
        //写入字节
        write.write(bytes);
        //写入字节,自定义范围
        write.write(bytes,0,5);
        //写入规定的字节
        write.write(97);
        //输入长度
        System.out.println("长度大小:"+write.size());
        //System.out.println(Arrays.toString(write.toByteArray()));
        write.writeTo(write1);
        write.close();

        //读取文件
        ByteArrayInputStream read = new ByteArrayInputStream(write1.toByteArray());
        int c;
        while ((c = read.read()) != -1){
            //read.skip(5);
            read.mark(1);
            read.reset();
            System.out.println((char)c);
        }
        System.out.println("余量:"+read.available());



    }

总结

主要对ByteArrayOutputStream和ByteArrayInputStream进行简单的介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值