|
ByteArrayOutputStream属于字节型输出流,该输出流中的数据被写入到一个byte数组里。byte数组会随着被写入其中的数据的增长而增长。定义如下: public class ByteArrayOutputStream extends OutputStream outline字段字段 说明
protected byte buf[]; 存储数据的缓冲区。
protected int count; 缓冲区中的有效字节数 构造方法字段 说明
public ByteArrayOutputStream() {…} 创建新的ByteArrayOutputStream,缓冲区容量初始化为32。
public ByteArrayOutputStream(int size) {…} 创建新的ByteArrayOutputStream,缓冲区容量初始化为size。 方法字段 说明
private void ensureCapacity(int minCapacity) {…} 确认缓冲区可以存放minCapacity个元素,如有必要,则进行扩容。
private void grow(int minCapacity) {…} 增加缓冲区容量,使其至少可以存放minCapacity个元素。
private static int hugeCapacity(int minCapacity) {…} 计算允许分配给byte数组的最大容量
public synchronized void write(int b) {…} 将指定的字节写入输出流。
public synchronized void write(byte b[], int off, int len) {…} 将指定byte数组中从偏移量off开始的len个字节写入输出流。
public synchronized void writeTo(OutputStream out) throws IOException {…} 将此byte数组输出流的全部内容写入到指定的输出流参数out中
public synchronized void reset() {…} 将输出流的count字段重置为零,从而丢弃输出流中目前已累积的所有输出。
public synchronized byte toByteArray()[] {…} 以byte数组的形式返回此输出流的当前内容。
public synchronized int size() {…} 返回count字段的值,这即输出流中有效字节的数目。
public synchronized String toString() {…} 使用平台默认的字符集,通过解码字节将缓冲区内容转换为字符串并返回。
public synchronized String toString(String charsetName) throws UnsupportedEncodingException {…} 使用指定的charsetName,通过解码字节将缓冲区内容转换为字符串并返回。
public void close() throws IOException {…} 关闭输出流,此方法无效,无法关闭输出流。 构造方法ByteArrayOutputStream()/** * 创建新的ByteArrayOutputStream,缓冲区容量初始化为32,如有必要可扩容。 */public ByteArrayOutputStream() { this(32);}ByteArrayOutputStream( int size)/** * 创建新的ByteArrayOutputStream,缓冲区容量初始化为参数size的大小,以字节为单位。 * * @param size 初始化容量. * @exception IllegalArgumentException 如果size为负数 */public ByteArrayOutputStream(int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } buf = new byte[size];}方法ensureCapacity( int minCapacity)/** * 确认缓冲区可以存放minCapacity个元素,如有必要,则进行扩容。 * * @param minCapacity 渴望的最小容量 * @throws OutOfMemoryError 如果minCapacity<0。 */private void ensureCapacity(int minCapacity) { // overflow-conscious code if (minCapacity - buf.length > 0) grow(minCapacity);}grow( int minCapacity)/** * 分配给数组的最大值。 * 一些虚拟机在数组中放了一些头字。如果将最大值分配给数组,会导致OutOfMemoryError。 */private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;/** * 增加缓冲区容量,使其至少可以存放minCapacity个元素。 * * @param minCapacity 渴望的最小容量 */private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = buf.length; // 新容量为旧容量*2(左移一位相当于乘2) int newCapacity = oldCapacity << 1; //如果新容量依然小于渴望的最小容量,将新容量设为渴望的最小容量 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; //如果新容量大于允许的最大容量 if (newCapacity - MAX_ARRAY_SIZE > 0) //将新容量设为允许的最大容量 newCapacity = hugeCapacity(minCapacity); buf = Arrays.copyOf(buf, newCapacity);}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
hugeCapacity( int minCapacity)/** * 计算允许分配给byte数组的最大容量 * /private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;}write( int b)/** * 将指定的字节写入此byte数组输出流。 * * @param b 被写入的字节 */public synchronized void write(int b) { //确认容量,如有需要则扩容 ensureCapacity(count + 1); //将指定的字节写入输出流 buf[count] = (byte) b; //缓冲区大小+1 count += 1;}write( byte b[], int off, int len)/** * 将指定byte数组中从偏移量off开始的len个字节写入此byte数组输出流。 * * @param b 数据. * @param off 偏移量. * @param len 写入的字节数 */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); //将指定byte数组中从偏移量off开始的len个字节写入此byte数组输出流。 System.arraycopy(b, off, buf, count, len); //缓冲区大小+len count += len;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
writeTo( OutputStream out)/** * 将此byte数组输出流的全部内容写入到指定的输出流参数中。 * 这与使用 out.write(buf, 0, count) 调用该输出流的 write 方法效果一样。 * * @param out 要写入数据的输出流。 * @exception IOException 如果发生I/O错误。 */public synchronized void writeTo(OutputStream out) throws IOException { out.write(buf, 0, count);}reset()/** * 将此byte数组输出流的count字段重置为零,从而丢弃输出流中目前已累积的所有输出。 * 通过重新使用已分配的缓冲区空间,可以再次使用该输出流。 * * @see java.io.ByteArrayInputStream#count */public synchronized void reset() { count = 0;}toByteArray()[]/** * 以byte数组的形式返回此输出流的当前内容。 * * @return 以byte数组的形式返回此输出流的当前内容。 * @see java.io.ByteArrayOutputStream#size() */public synchronized byte toByteArray()[] { return Arrays.copyOf(buf, count);}size()/** * 返回缓冲区的当前大小。 * * @return 返回缓冲区的当前大小。 * @see java.io.ByteArrayOutputStream#count */public synchronized int size() { return count;}toString()/** * 使用平台默认的字符集,通过解码字节将缓冲区内容转换为字符串并返回。 * * @return 从缓冲区内容解码的字符串。 * @since JDK1.1 */public synchronized String toString() { return new String(buf, 0, count);}toString( String charsetName)/** * 使用指定的charsetName,通过解码字节将缓冲区内容转换为字符串。 * * @param charsetName 指定的charsetName * @return 从缓冲区内容解码的字符串。 * @exception UnsupportedEncodingException * 如果charsetName是不被支持的字符集。 * @since JDK1.1 */public synchronized String toString(String charsetName) throws UnsupportedEncodingException{ return new String(buf, 0, count, charsetName);}close()/** * 关闭ByteArrayOutputStream,此方法无效。 */public void close() throws IOException {}demoimport java.io.ByteArrayOutputStream;import java.io.IOException;import org.junit.Test;public class ByteArrayOutputStreamTest { // write(int b)&&toByteArray()[] // 0,1,2, @Test public void test1() { int a = 0; int b = 1; int c = 2; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(a); baos.write(b); baos.write(c); byte[] buf = baos.toByteArray(); for (int i = 0; i < buf.length; i++) System.out.print(buf + ","); } // write(byte b[], int off, int len) // 15,67,-1,-9, @Test public void test2() { byte[] buf = new byte[] { 2, 15, 67, -1, -9, 9 }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(buf, 1, 4); buf = baos.toByteArray(); for (int i = 0; i < buf.length; i++) System.out.print(buf + ","); } // writeTo(OutputStream out) // 2,15,67,-1,-9,9, // *************** // 2,15,67,-1,-9,9, @Test public void test3() throws IOException { byte[] buf = new byte[] { 2, 15, 67, -1, -9, 9 }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(buf); buf = baos.toByteArray(); for (int i = 0; i < buf.length; i++) System.out.print(buf + ","); System.out.println("\n***************"); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); baos.writeTo(baos2); buf = baos2.toByteArray(); for (int i = 0; i < buf.length; i++) System.out.print(buf + ","); } // reset() // 2,15,67,-1,-9,9, // *************** // 0 @Test public void test4() throws IOException { byte[] buf = new byte[] { 2, 15, 67, -1, -9, 9 }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(buf); buf = baos.toByteArray(); for (int i = 0; i < buf.length; i++) System.out.print(buf + ","); System.out.println("\n*************"); baos.reset(); buf = baos.toByteArray(); System.out.println(buf.length); }}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
总结- ByteArrayOutputStream中的数据被写入到一个byte数组里。byte数组会随着被写入其中的数据的增长而增长。
- 表示字节输出流的类必须提供至少一种可写入一个输出字节的方法。ByteArrayOutputStream提供了两种。
- ByteArrayOutputStream可以将缓冲区中的数据转化为byte数组或字符串并返回。
- ByteArrayOutputStream可以通过writeTo( OutputStream out)实现输出流之间数据的复制
- ByteArrayOutputStream 的close方法无效,无法关闭此输出流。
对ByteArrayOutputStream的介绍就到这里。如何使用,后面会讲到。 想了解更多内容请参考 版权声明
|