源码剖析之java.io.ByteArrayOutputStream

对应于 java.io.ByteArrayInputStream 的byte内存输入流,jdk同样提供了java.io. ByteArrayOutputStream byte[]输出流的概念。
功能很简单,就是通过write方法 把字节写入到 byte[]中,取的时候直接toByteArray或者toString() 即可。

注意:此类是线程安全的。
实现方式:[b]通过对方法 添加synchronized 关键字实现,另外Reader 顶层类则是通过Lock 锁,锁定当前流对象实现的。[/b]

功能分析如下:


package java.io;

import java.util.Arrays;

/**
* 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.
翻译:ByteArrayOutputStream 实现了OutputStream 接口,数据是写入到一个 byte[]里,并且buf 是自动增长的随着流的写入。
* The data can be retrieved using <code>toByteArray()</code> and
* <code>toString()</code>.
翻译:ByteArrayOutputStream 缓存的数据,可以通过 toByteArray 和 toString() 方法获取
* <p>
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.

翻译:关闭ByteArrayOutputStream ,调用close方法是没有意义的。close是空实现,即便是关闭了也可以调用其他的方法。
*
* @author Arthur van Hoff
* @version %I%, %G%
* @since JDK1.0
*/

public class ByteArrayOutputStream extends OutputStream {

/**
* 承载ByteArrayOutputStream 写入的数据
*/
protected byte buf[];

/**
* buf 中当前有效的数据量
*/
protected int count;

/**
* 构造函数,默认创建的是一个长度为32的byte数组
*/
public ByteArrayOutputStream() {
this(32);
}

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

/**
* 将b的低八位写入 buf数组里
*/
public synchronized void write(int b) {
int newcount = count + 1;
if (newcount > buf.length) { //如果长度不够,那么将buf的长度扩展2倍。 另外:Arrays.copyOf 这个函数的含义请各位好好体会下,这个功能是数组操作最常用的功能!!
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
buf[count] = (byte)b;
count = newcount; //每写入一个字节,count+1
}

/**
* 将b 数组中的字节,从off开始 读取 len长度的byte数据。
注意:父类可是循环写入的,这里进行覆盖一次性搞定。
* @param b the data. 待写入的byte数组
* @param off data 代写的开始位置
* @param len 写入的长度
*/
public synchronized void write(byte b[], int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
int newcount = count + len;
if (newcount > buf.length) { //如果需要扩容
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
//否则直接copy即可
System.arraycopy(b, off, buf, count, len); //System.arraycopy 也是操作数组最常用的功能。
count = newcount;
}

/**
* Writes the complete contents of this byte array output stream to
* the specified output stream argument, as if by calling the output
* stream's write method using <code>out.write(buf, 0, count)</code>.
*
以上方法都是写入byte 或者byte[] ,但是writeTo 责任直接把out流中的数据全部写入
buf数组里。
*/
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}

/**
* 从新设置,count值改0
*/
public synchronized void reset() {
count = 0;
}

/**
返回流中的byte数组数据
*/
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}

/**
* 返回缓存中的数据长度
*
*/
public synchronized int size() {
return count;
}

/**
* 根据默认平台返回特定格式的string字符串
*/
public synchronized String toString() {
return new String(buf, 0, count);
}

/**
* 同上,只是用了特定编码格式
*/
public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}


/**
* 无用的关闭
*
*/
public void close() throws IOException {
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值