Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.InputStream; /** * @see
InputStream Conversion to String
Convert String to InputStream
InputStream conversion to byte array
Convert byte to InputStream
Convert byte to String * @author Mr. Fat * @mail [email protected] * */public class Inputstreamutils {final static int buffer_size = 4096; /** * Convert InputStream to String * @param in InputStream * @return String * @throws Exception * */ public static String inputstreamtostring (InputStream in) throws exception{Bytearrayoutputstream OutStream = new Bytearrayoutputstream (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write (data, 0, count); data = null; return new String (Outstream.tobytearray (), "iso-8859-1"); }/** * Convert InputStream to a character encoded string * @param in * @param encoding * @return * @throws Excepti On */public static String inputstreamtostring (InputStream in,string encoding) throws exception{ Bytearrayoutputstream OutStream = new BytearrayoutputstreAM (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write (data, 0, count); data = null; return new String (Outstream.tobytearray (), "iso-8859-1"); /** * Convert String to InputStream * @param in * @return * @throws Exception */public static in Putstream Stringtoinputstream (String in) throws exception{Bytearrayinputstream is = new Bytearrayinputstr EAM (In.getbytes ("iso-8859-1")); return is; }/** * Convert InputStream to byte array * @param in InputStream * @return byte[] * @throws IOException * /public static byte[] Inputstreamtobyte (InputStream in) throws ioexception{Bytearrayoutputstream outs Tream = new Bytearrayoutputstream (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write(data, 0, count); data = null; return Outstream.tobytearray (); /** * Converts a byte array to InputStream * @param in * @return * @throws Exception */public static in Putstream Bytetoinputstream (byte[] in) throws exception{Bytearrayinputstream is = new Bytearrayinputstrea M (in); return is; /** * Convert byte array to String * @param in * @return * @throws Exception */public static String Bytetostring (byte[] in) throws exception{InputStream are = Bytetoinputstream (in); Return inputstreamtostring (IS); } }
[]19.inputstream and String,byte transfer [reprint]