InputStream and string,byte between each other

Source: Internet
Author: User
Tags array to string

ImportJava.io.ByteArrayInputStream; ImportJava.io.ByteArrayOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; /**   *    * @authorandy.chen * @mail [email protected] **/   Public classInputstreamutils {Final Static intBuffer_size = 4096; /*** Convert InputStream to String *@paramIn InputStream *@returnString *@throwsException **/       Public StaticString inputstreamtostring (InputStream in)throwsexception{Bytearrayoutputstream OutStream=NewBytearrayoutputstream (); byte[] data =New byte[Buffer_size]; intCount =-1;  while((count = In.read (data,0,buffer_size))! =-1) outstream.write (data,0, Count); Data=NULL; return NewString (Outstream.tobytearray (), "Iso-8859-1"); }            /*** Convert InputStream to some character encoded string *@paramIn *@paramencoding *@return       * @throwsException*/            Public StaticString inputstreamtostring (inputstream in,string encoding)throwsexception{Bytearrayoutputstream OutStream=NewBytearrayoutputstream (); byte[] data =New byte[Buffer_size]; intCount =-1;  while((count = In.read (data,0,buffer_size))! =-1) outstream.write (data,0, Count); Data=NULL; return NewString (Outstream.tobytearray (), "Iso-8859-1"); }            /*** Convert String to InputStream *@paramIn *@return       * @throwsException*/       Public StaticInputStream Stringtoinputstream (String in)throwsexception{Bytearrayinputstream is=NewBytearrayinputstream (In.getbytes ("Iso-8859-1")); returnis ; }            /*** Convert InputStream to byte array *@paramIn InputStream *@returnbyte[] *@throwsIOException*/       Public Static byte[] Inputstreamtobyte (InputStream in)throwsioexception{Bytearrayoutputstream OutStream=NewBytearrayoutputstream (); byte[] data =New byte[Buffer_size]; intCount =-1;  while((count = In.read (data,0,buffer_size))! =-1) outstream.write (data,0, Count); Data=NULL; returnOutstream.tobytearray (); }            /*** Convert byte array to InputStream *@paramIn *@return       * @throwsException*/       Public StaticInputStream Bytetoinputstream (byte[] in)throwsexception{Bytearrayinputstream is=NewBytearrayinputstream (in); returnis ; }            /*** Convert byte array to String *@paramIn *@return       * @throwsException*/       Public StaticString bytetostring (byte[] in)throwsexception{InputStream is=Bytetoinputstream (in); returninputstreamtostring (IS); }    }  

InputStream and string,byte between each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.