JAVA基础 | IO之ByteArrayOutputStream与ByteArrayInputStream

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;
        c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值