所引入的包和继承关系:
引入java.util.Arrays文件
继承自OutputStream类
类的作用注释:
/** * 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 <code>toByteArray()</code> and * <code>toString()</code>. * <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>. * * @author Arthur van Hoff * @since JDK1.0 */大意如下:
这个类是一个输出流接口,会把数据写入到byte数组中。
缓冲区会自动增长,添加数据。
数据可以通过“toByteArray”和“toString”方法进行格式转换
关闭ByteArrayOutputStream是无效的。
close方法在这个类中只能在流已经被关闭后调用(不抛出IO异常)
这个类含有三个成员变量:
缓冲区:
protected byte buf[];
缓冲区有效长度:
protected int count;
最大数组长度:
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
含有十五个方法:
构造函数(默认大小缓冲区):
public ByteArrayOutputStream() { this(32); }
构造函数(构造特定大小缓冲区):
public ByteArrayOutputStream(int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } buf = new byte[size]; }
确认缓冲区容量是否够用:
private void ensureCapacity(int minCapacity) { // overflow-conscious code if (minCapacity - buf.length > 0)//不够就增加缓冲区大小 grow(minCapacity); }
增大缓冲区容量:
private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = buf.length; int newCapacity = oldCapacity << 1;//新容量为旧容量2倍 if (newCapacity - minCapacity < 0)//新容量小于最小需要容量 newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0)//新容量超出系统数组所能容纳最大值(Int型) newCapacity = hugeCapacity(minCapacity); buf = Arrays.copyOf(buf, newCapacity);//引入包内容 }
大容量判定(返回一个合适的容量值):
private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
写入缓冲区(单个值):
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); }
重置缓冲区:
public synchronized void reset() { count = 0; }
检查缓冲区内容(以Byte数组形式):
public synchronized byte toByteArray()[] { return Arrays.copyOf(buf, count); }
获得当前缓冲区有效大小:
public synchronized int size() { return count; }
检查缓冲区内容(以字符串形式):
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); }
检查缓冲区内容(字符串形式,该String构造函数早已过时,我也不知道这个方法现在有啥用):
public synchronized String toString(int hibyte) { return new String(buf, hibyte, 0, count); }
(作者目前使用的JDK1.8)
关闭流(没啥用,和ByteArrayInputStream类相同):
public void close() throws IOException { }
ByteArrayOutputStream仍是比ByteArrayInputStream简单,但相对于一般的OutputStream,由于其加入了缓冲区检测机制,所以功能会更为强大,准确率也更高。