ByteArrayOutputStream (Java SE 10 & JDK 10 )

文章推薦指數: 80 %
投票人數:10人

This method is equivalent to #toString(charset) that takes a charset . An invocation of this method of the form ByteArrayOutputStream b = ... b.toString("UTF-8") java.lang.Object java.io.OutputStream java.io.ByteArrayOutputStream AllImplementedInterfaces: Closeable,Flushable,AutoCloseable publicclassByteArrayOutputStream extendsOutputStream Thisclassimplementsanoutputstreaminwhichthedatais writtenintoabytearray.Thebufferautomaticallygrowsasdata iswrittentoit. ThedatacanberetrievedusingtoByteArray()and toString(). ClosingaByteArrayOutputStreamhasnoeffect.Themethodsin thisclasscanbecalledafterthestreamhasbeenclosedwithout generatinganIOException. Since: 1.0 FieldSummary Fields  ModifierandType Field Description protectedbyte[] buf Thebufferwheredataisstored. protectedint count Thenumberofvalidbytesinthebuffer. ConstructorSummary Constructors  Constructor Description ByteArrayOutputStream() Createsanewbytearrayoutputstream. ByteArrayOutputStream​(int size) Createsanewbytearrayoutputstream,withabuffercapacityof thespecifiedsize,inbytes. MethodSummary AllMethods InstanceMethods ConcreteMethods DeprecatedMethods  ModifierandType Method Description void close() ClosingaByteArrayOutputStreamhasnoeffect. void reset() Resetsthecountfieldofthisbytearrayoutput streamtozero,sothatallcurrentlyaccumulatedoutputinthe outputstreamisdiscarded. int size() Returnsthecurrentsizeofthebuffer. byte[] toByteArray() Createsanewlyallocatedbytearray. String toString() Convertsthebuffer'scontentsintoastringdecodingbytesusingthe platform'sdefaultcharacterset. String toString​(int hibyte) Deprecated. Thismethoddoesnotproperlyconvertbytesintocharacters. String toString​(String charsetName) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thenamedcharset. String toString​(Charset charset) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thespecifiedcharset. void write​(byte[] b, int off, int len) Writeslenbytesfromthespecifiedbytearray startingatoffsetofftothisbytearrayoutputstream. void write​(int b) Writesthespecifiedbytetothisbytearrayoutputstream. void writeTo​(OutputStream out) Writesthecompletecontentsofthisbytearrayoutputstreamto thespecifiedoutputstreamargument,asifbycallingtheoutput stream'swritemethodusingout.write(buf,0,count). Methodsdeclaredinclass java.lang.Object clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,wait Methodsdeclaredinclass java.io.OutputStream flush,write FieldDetail buf protected byte[]buf Thebufferwheredataisstored. count protected intcount Thenumberofvalidbytesinthebuffer. ConstructorDetail ByteArrayOutputStream public ByteArrayOutputStream() Createsanewbytearrayoutputstream.Thebuffercapacityis initially32bytes,thoughitssizeincreasesifnecessary. ByteArrayOutputStream public ByteArrayOutputStream​(int size) Createsanewbytearrayoutputstream,withabuffercapacityof thespecifiedsize,inbytes. Parameters: size-theinitialsize. Throws: IllegalArgumentException-ifsizeisnegative. MethodDetail write public void write​(int b) Writesthespecifiedbytetothisbytearrayoutputstream. Specifiedby: write inclass OutputStream Parameters: b-thebytetobewritten. write public void write​(byte[] b, int off, int len) Writeslenbytesfromthespecifiedbytearray startingatoffsetofftothisbytearrayoutputstream. Overrides: write inclass OutputStream Parameters: b-thedata. off-thestartoffsetinthedata. len-thenumberofbytestowrite. writeTo public void writeTo​(OutputStream out) throwsIOException Writesthecompletecontentsofthisbytearrayoutputstreamto thespecifiedoutputstreamargument,asifbycallingtheoutput stream'swritemethodusingout.write(buf,0,count). Parameters: out-theoutputstreamtowhichtowritethedata. Throws: IOException-ifanI/Oerroroccurs. reset public void reset() Resetsthecountfieldofthisbytearrayoutput streamtozero,sothatallcurrentlyaccumulatedoutputinthe outputstreamisdiscarded.Theoutputstreamcanbeusedagain, reusingthealreadyallocatedbufferspace. SeeAlso: ByteArrayInputStream.count toByteArray public byte[] toByteArray() Createsanewlyallocatedbytearray.Itssizeisthecurrent sizeofthisoutputstreamandthevalidcontentsofthebuffer havebeencopiedintoit. Returns: thecurrentcontentsofthisoutputstream,asabytearray. SeeAlso: size() size public int size() Returnsthecurrentsizeofthebuffer. Returns: thevalueofthecountfield,whichisthenumber ofvalidbytesinthisoutputstream. SeeAlso: count toString public String toString() Convertsthebuffer'scontentsintoastringdecodingbytesusingthe platform'sdefaultcharacterset.ThelengthofthenewString isafunctionofthecharacterset,andhencemaynotbeequaltothe sizeofthebuffer. Thismethodalwaysreplacesmalformed-inputandunmappable-character sequenceswiththedefaultreplacementstringfortheplatform's defaultcharacterset.TheCharsetDecoder classshouldbeusedwhenmorecontroloverthedecodingprocessis required. Overrides: toString inclass Object Returns: Stringdecodedfromthebuffer'scontents. Since: 1.1 toString public String toString​(String charsetName) throwsUnsupportedEncodingException Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thenamedcharset. Thismethodisequivalentto#toString(charset)thattakesa charset. Aninvocationofthismethodoftheform ByteArrayOutputStreamb=... b.toString("UTF-8") behavesinexactlythesamewayastheexpression ByteArrayOutputStreamb=... b.toString(StandardCharsets.UTF_8) Parameters: charsetName-thenameofasupported charset Returns: Stringdecodedfromthebuffer'scontents. Throws: UnsupportedEncodingException-Ifthenamedcharsetisnotsupported Since: 1.1 toString public String toString​(Charset charset) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thespecifiedcharset.Thelengthofthenew Stringisafunctionofthecharset,andhencemaynotbeequal tothelengthofthebytearray. Thismethodalwaysreplacesmalformed-inputandunmappable-character sequenceswiththecharset'sdefaultreplacementstring.TheCharsetDecoderclassshouldbeusedwhenmorecontrol overthedecodingprocessisrequired. Parameters: charset-thecharset tobeusedtodecodethebytes Returns: Stringdecodedfromthebuffer'scontents. Since: 10 toString @Deprecated public String toString​(int hibyte) Deprecated. Thismethoddoesnotproperlyconvertbytesintocharacters. AsofJDK 1.1,thepreferredwaytodothisisviathe toString(StringcharsetName)ortoString(Charsetcharset) method,whichtakesanencoding-nameorcharsetargument, orthetoString()method,whichusestheplatform'sdefault characterencoding. Createsanewlyallocatedstring.Itssizeisthecurrentsizeof theoutputstreamandthevalidcontentsofthebufferhavebeen copiedintoit.Eachcharactercintheresultingstringis constructedfromthecorrespondingelementbinthebyte arraysuchthat: c==(char)(((hibyte&0xff)<<8)|(b&0xff)) Parameters: hibyte-thehighbyteofeachresultingUnicodecharacter. Returns: thecurrentcontentsoftheoutputstream,asastring. SeeAlso: size(), toString(String), toString() close public void close() throwsIOException ClosingaByteArrayOutputStreamhasnoeffect.Themethodsin thisclasscanbecalledafterthestreamhasbeenclosedwithout generatinganIOException. Specifiedby: close ininterface AutoCloseable Specifiedby: close ininterface Closeable Overrides: close inclass OutputStream Throws: IOException-ifanI/Oerroroccurs.



請為這篇文章評分?