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