ByteArrayOutputStream继承自OutputStream,它的变量及方法包括:
ByteArrayOutputStream() | 长度为32个字节 |
public ByteArrayOutputStream(int size) | 可自定义长度size,用于字节超过32个字节时。 |
public void close() throws IOException | 关闭输出流,可释放系统资源 |
public synchronized void reset() | 重置输出流,使内部字节为0 |
public int size() | 该输出流中的字节数 |
public synchronized byte[] toByteArray() | 将该输出流中内容转换为字节数组 |
public String toString() | 转换为字符串 |
public String toString(String charsetName) throws UnsupportedEncodingException | 转换为编码格式为charsetName的字符串 |
public synchronized void write(byte[] buffer, int offset, int len) | 将buffer写入该输出流 |
public synchronized void write(int oneByte) | 写入1个字节的数据 |
public synchronized void writeTo(OutputStream out) throws IOException | 将该输出流写入out中 |
@Override
public synchronized void write(byte[] buffer, int offset, int len) {
Arrays.checkOffsetAndCount(buffer.length, offset, len);
if (len == 0) {
return;
}
expand(len);
System.arraycopy(buffer, offset, buf, this.count, len);
this.count += len;
}
其中buffer为写入该OutputStream流的数据源;offset为buffer中数据拷贝的索引值,即从第几个数据开始复制;this.count为该OutputStream的长度,即数据添加的起始编号,len为写入数据的长度。