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