Inputstreamutils. java001import Java. io. bytearrayinputstream; 002 import Java. io. bytearrayoutputstream; 003 import Java. io. ioexception; 004 import Java. io. inputstream; 005 006/** 007*008 * @ author Andy. chen009 * @ mail Chenjunjun.ZJ@gmail.com010 * 011 */012 public class inputstreamutils {013 014 final static int buffer_size = 4096; 015 016/** 017 * convert inputstream to string018 * @ Param in inputstream019 * @ return String020 * @ throws exception022 */023 public static string inputstreamtostring (inputstream in) throws exception {024 025 bytearrayoutputstream outstream = new bytearrayoutputstream (); 026 byte [] DATA = new byte [buffer_size]; 027 int COUNT =-1; 028 while (COUNT = in. read (data, 0, buffer_size ))! =-1) 029 outstream. write (data, 0, count); 030 031 DATA = NULL; 032 return new string (outstream. tobytearray (), "ISO-8859-1 "); 033} 034 035/** 036 * convert inputstream to a character-encoded string037 * @ Param in038 * @ Param encoding039 * @ return040 * @ throws exception041 */042 public static string inputstreamtostring (inputstream in, string encoding) throwsexception {043 044 bytearrayoutputstream outstream = new bytearrayoutp Utstream (); 045 byte [] DATA = new byte [buffer_size]; 046 int COUNT =-1; 047 while (COUNT = in. read (data, 0, buffer_size ))! =-1) 048 outstream. write (data, 0, count); 049 050 DATA = NULL; 051 return new string (outstream. tobytearray (), "ISO-8859-1 "); 052} 053 054/** 055 * convert string to inputstream056 * @ Param in057 * @ return058 * @ throws exception059 */060 public static inputstream stringtoinputstream (string in) throws exception {061 062 bytearrayinputstream is = new bytearrayinputstream (in. getbytes ("ISO-8859-1"); 063 return is; 06 4} 065 066/** 067 * convert inputstream to byte array 068 * @ Param in inputstream069 * @ return byte [] 070 * @ throws ioexception071 */072 public static byte [] inputstreamtobyte (inputstream in) throws ioexception {073 074 bytearrayoutputstream outstream = new bytearrayoutputstream (); 075 byte [] DATA = new byte [buffer_size]; 076 int COUNT =-1; 077 while (COUNT = in. read (data, 0, buffer_size ))! =-1) 078 outstream. write (data, 0, count); 079 080 DATA = NULL; 081 return outstream. tobytearray (); 082} 083 084/** 085 * convert byte array to inputstream086 * @ Param in087 * @ return088 * @ throws exception089 */090 public static inputstream bytetoinputstream (byte [] in) throws exception {091 092 bytearrayinputstream is = new bytearrayinputstream (in); 093 return is; 094} 095 096/** 097 * convert byte array to string098 * @ Param in099 * @ return100 * @ throws exception101 */102 public static string bytetostring (byte [] in) throws exception {103 104 inputstream is = bytetoinputstream (in); 105 return inputstreamtostring (is); 106} 107 108}