InputStream String与byte数组之间的互转

来源:互联网 发布:网络买大麻暗语 编辑:程序博客网 时间:2024/05/16 07:11
  1. public class InputStreamUtils {  
  2.       
  3.     final static int BUFFER_SIZE = 4096;  
  4.       
  5.     /**  
  6.      * 将InputStream转换成String  
  7.      * @param in InputStream  
  8.      * @return String  
  9.      * @throws Exception  
  10.      *   
  11.      */  
  12.     public static String InputStreamTOString(InputStream in) throws Exception{  
  13.           
  14.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  15.         byte[] data = new byte[BUFFER_SIZE];  
  16.         int count = -1;  
  17.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  18.             outStream.write(data, 0, count);  
  19.           
  20.         data = null;  
  21.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  22.     }  
  23.       
  24.     /**  
  25.      * 将InputStream转换成某种字符编码的String  
  26.      * @param in  
  27.      * @param encoding  
  28.      * @return  
  29.      * @throws Exception  
  30.      */  
  31.          public static String InputStreamTOString(InputStream in,String encoding) throws Exception{  
  32.           
  33.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  34.         byte[] data = new byte[BUFFER_SIZE];  
  35.         int count = -1;  
  36.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  37.             outStream.write(data, 0, count);  
  38.           
  39.         data = null;  
  40.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  41.     }  
  42.       
  43.     /**  
  44.      * 将String转换成InputStream  
  45.      * @param in  
  46.      * @return  
  47.      * @throws Exception  
  48.      */  
  49.     public static InputStream StringTOInputStream(String in) throws Exception{  
  50.           
  51.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
  52.         return is;  
  53.     }  
  54.       
  55.     /**  
  56.      * 将InputStream转换成byte数组  
  57.      * @param in InputStream  
  58.      * @return byte[]  
  59.      * @throws IOException  
  60.      */  
  61.     public static byte[] InputStreamTOByte(InputStream in) throws IOException{  
  62.           
  63.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  64.         byte[] data = new byte[BUFFER_SIZE];  
  65.         int count = -1;  
  66.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  67.             outStream.write(data, 0, count);  
  68.           
  69.         data = null;  
  70.         return outStream.toByteArray();  
  71.     }  
  72.       
  73.     /**  
  74.      * 将byte数组转换成InputStream  
  75.      * @param in  
  76.      * @return  
  77.      * @throws Exception  
  78.      */  
  79.     public static InputStream byteTOInputStream(byte[] in) throws Exception{  
  80.           
  81.         ByteArrayInputStream is = new ByteArrayInputStream(in);  
  82.         return is;  
  83.     }  
  84.       
  85.     /**  
  86.      * 将byte数组转换成String  
  87.      * @param in  
  88.      * @return  
  89.      * @throws Exception  
  90.      */  
  91.     public static String byteTOString(byte[] in) throws Exception{  
  92.           
  93.         InputStream is = byteTOInputStream(in);  
  94.         return InputStreamTOString(is);  
  95.     }  
  96.   
  97. }
0 0
原创粉丝点击