ByteArrayOutputStream简介和使用_动力节点Java学院整理
ByteArrayOutputStream介绍
ByteArrayOutputStream是字节数组输出流。它继承于OutputStream。
ByteArrayOutputStream中的数据被写入一个byte数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()和toString()获取数据。
OutputStream函数列表
我们来看看ByteArrayOutputStream的父类OutputStream的函数接口。
//构造函数 OutputStream() voidclose() voidflush() voidwrite(byte[]buffer,intoffset,intcount) voidwrite(byte[]buffer) abstractvoidwrite(intoneByte) ByteArrayOutputStream函数列表 //构造函数 ByteArrayOutputStream() ByteArrayOutputStream(intsize) voidclose() synchronizedvoidreset() intsize() synchronizedbyte[]toByteArray() StringtoString(inthibyte) StringtoString(StringcharsetName) StringtoString() synchronizedvoidwrite(byte[]buffer,intoffset,intlen) synchronizedvoidwrite(intoneByte) synchronizedvoidwriteTo(OutputStreamout)
OutputStream和ByteArrayOutputStream源码分析
OutputStream是ByteArrayOutputStream的父类,我们先看看OutputStream的源码,然后再学ByteArrayOutputStream的源码。
1.OutputStream.java源码分析(基于jdk1.7.40)
packagejava.io; publicabstractclassOutputStreamimplementsCloseable,Flushable{ //将字节b写入到“输出流”中。 //它在子类中实现! publicabstractvoidwrite(intb)throwsIOException; //写入字节数组b到“字节数组输出流”中。 publicvoidwrite(byteb[])throwsIOException{ write(b,0,b.length); } //写入字节数组b到“字节数组输出流”中,并且off是“数组b的起始位置”,len是写入的长度 publicvoidwrite(byteb[],intoff,intlen)throwsIOException{ if(b==null){ thrownewNullPointerException(); }elseif((off<0)||(off>b.length)||(len<0)|| ((off+len)>b.length)||((off+len)<0)){ thrownewIndexOutOfBoundsException(); }elseif(len==0){ return; } for(inti=0;i2.ByteArrayOutputStream源码分析(基于jdk1.7.40)
packagejava.io; importjava.util.Arrays; publicclassByteArrayOutputStreamextendsOutputStream{ //保存“字节数组输出流”数据的数组 protectedbytebuf[]; //“字节数组输出流”的计数 protectedintcount; //构造函数:默认创建的字节数组大小是。 publicByteArrayOutputStream(){ this(32); } //构造函数:创建指定数组大小的“字节数组输出流” publicByteArrayOutputStream(intsize){ if(size<0){ thrownewIllegalArgumentException("Negativeinitialsize:" +size); } buf=newbyte[size]; } //确认“容量”。 //若“实际容量0) grow(minCapacity); } //增加“容量”。 privatevoidgrow(intminCapacity){ intoldCapacity=buf.length; //“新容量”的初始化=“旧容量”x2 intnewCapacity=oldCapacity<<1; //比较“新容量”和“minCapacity”的大小,并选取其中较大的数为“新的容量”。 if(newCapacity-minCapacity<0) newCapacity=minCapacity; if(newCapacity<0){ if(minCapacity<0)//overflow thrownewOutOfMemoryError(); newCapacity=Integer.MAX_VALUE; } buf=Arrays.copyOf(buf,newCapacity); } //写入一个字节b到“字节数组输出流”中,并将计数+1 publicsynchronizedvoidwrite(intb){ ensureCapacity(count+1); buf[count]=(byte)b; count+=1; } //写入字节数组b到“字节数组输出流”中。off是“写入字节数组b的起始位置”,len是写入的长度 publicsynchronizedvoidwrite(byteb[],intoff,intlen){ if((off<0)||(off>b.length)||(len<0)|| ((off+len)-b.length>0)){ thrownewIndexOutOfBoundsException(); } ensureCapacity(count+len); System.arraycopy(b,off,buf,count,len); count+=len; } //写入输出流outb到“字节数组输出流”中。 publicsynchronizedvoidwriteTo(OutputStreamout)throwsIOException{ out.write(buf,0,count); } //重置“字节数组输出流”的计数。 publicsynchronizedvoidreset(){ count=0; } //将“字节数组输出流”转换成字节数组。 publicsynchronizedbytetoByteArray()[]{ returnArrays.copyOf(buf,count); } //返回“字节数组输出流”当前计数值 publicsynchronizedintsize(){ returncount; } publicsynchronizedStringtoString(){ returnnewString(buf,0,count); } publicsynchronizedStringtoString(StringcharsetName) throwsUnsupportedEncodingException { returnnewString(buf,0,count,charsetName); } @Deprecated publicsynchronizedStringtoString(inthibyte){ returnnewString(buf,hibyte,0,count); } publicvoidclose()throwsIOException{ } } 说明:
ByteArrayOutputStream实际上是将字节数据写入到“字节数组”中去。
(01)通过ByteArrayOutputStream()创建的“字节数组输出流”对应的字节数组大小是32。
(02)通过ByteArrayOutputStream(intsize)创建“字节数组输出流”,它对应的字节数组大小是size。
(03)write(intoneByte)的作用将int类型的oneByte换成byte类型,然后写入到输出流中。
(04)write(byte[]buffer,intoffset,intlen)是将字节数组buffer写入到输出流中,offset是从buffer中读取数据的起始偏移位置,len是读取的长度。
(05)writeTo(OutputStreamout)将该“字节数组输出流”的数据全部写入到“输出流out”中。
示例代码
关于ByteArrayOutputStream中API的详细用法,参考示例代码(ByteArrayOutputStreamTest.java):
importjava.io.IOException; importjava.io.OutputStream; importjava.io.ByteArrayOutputStream; importjava.io.ByteArrayInputStream; /** *ByteArrayOutputStream测试程序 * * */ publicclassByteArrayOutputStreamTest{ privatestaticfinalintLEN=5; //对应英文字母“abcddefghijklmnopqrsttuvwxyz” privatestaticfinalbyte[]ArrayLetters={ 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A }; publicstaticvoidmain(String[]args){ //Stringtmp=newString(ArrayLetters); //System.out.println("ArrayLetters="+tmp); tesByteArrayOutputStream(); } /** *ByteArrayOutputStream的API测试函数 */ privatestaticvoidtesByteArrayOutputStream(){ //创建ByteArrayOutputStream字节流 ByteArrayOutputStreambaos=newByteArrayOutputStream(); //依次写入“A”、“B”、“C”三个字母。0x41对应A,0x42对应B,0x43对应C。 baos.write(0x41); baos.write(0x42); baos.write(0x43); System.out.printf("baos=%s\n",baos); //将ArrayLetters数组中从“3”开始的后5个字节写入到baos中。 //即对应写入“0x64,0x65,0x66,0x67,0x68”,即“defgh” baos.write(ArrayLetters,3,5); System.out.printf("baos=%s\n",baos); //计算长度 intsize=baos.size(); System.out.printf("size=%s\n",size); //转换成byte[]数组 byte[]buf=baos.toByteArray(); Stringstr=newString(buf); System.out.printf("str=%s\n",str); //将baos写入到另一个输出流中 try{ ByteArrayOutputStreambaos2=newByteArrayOutputStream(); baos.writeTo((OutputStream)baos2); System.out.printf("baos2=%s\n",baos2); }catch(IOExceptione){ e.printStackTrace(); } } }运行结果:
baos=ABC baos=ABCdefgh size=8 str=ABCdefgh baos2=ABCdefgh以上所述是小编给大家介绍的ByteArrayOutputStream简介和使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!