InputStream与String,Byte之间互转

来源:互联网 发布:小黑屋写作mac 编辑:程序博客网 时间:2024/05/22 13:33

 本文将介绍InputStream与String,Byte之间的相互转换。以代码来说明:

[html] view plaincopy
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. /**  
  7.  *   
  8.  * @author Andy.Chen  
  9.  * @mail Chenjunjun.ZJ@gmail.com  
  10.  *  
  11.  */  
  12. public class InputStreamUtils {  
  13.       
  14.     final static int BUFFER_SIZE = 4096;  
  15.       
  16.     /**  
  17.      * 将InputStream转换成String  
  18.      * @param in InputStream  
  19.      * @return String  
  20.      * @throws Exception  
  21.      *   
  22.      */  
  23.     public static String InputStreamTOString(InputStream in) throws Exception{  
  24.           
  25.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  26.         byte[] data = new byte[BUFFER_SIZE];  
  27.         int count = -1;  
  28.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  29.             outStream.write(data, 0, count);  
  30.           
  31.         data = null;  
  32.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  33.     }  
  34.       
  35.     /**  
  36.      * 将InputStream转换成某种字符编码的String  
  37.      * @param in  
  38.      * @param encoding  
  39.      * @return  
  40.      * @throws Exception  
  41.      */  
  42.          public static String InputStreamTOString(InputStream in,String encoding) throws Exception{  
  43.           
  44.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  45.         byte[] data = new byte[BUFFER_SIZE];  
  46.         int count = -1;  
  47.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  48.             outStream.write(data, 0, count);  
  49.           
  50.         data = null;  
  51.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  52.     }  
  53.       
  54.     /**  
  55.      * 将String转换成InputStream  
  56.      * @param in  
  57.      * @return  
  58.      * @throws Exception  
  59.      */  
  60.     public static InputStream StringTOInputStream(String in) throws Exception{  
  61.           
  62.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
  63.         return is;  
  64.     }  
  65.       
  66.     /**  
  67.      * 将InputStream转换成byte数组  
  68.      * @param in InputStream  
  69.      * @return byte[]  
  70.      * @throws IOException  
  71.      */  
  72.     public static byte[] InputStreamTOByte(InputStream in) throws IOException{  
  73.           
  74.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  75.         byte[] data = new byte[BUFFER_SIZE];  
  76.         int count = -1;  
  77.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  78.             outStream.write(data, 0, count);  
  79.           
  80.         data = null;  
  81.         return outStream.toByteArray();  
  82.     }  
  83.       
  84.     /**  
  85.      * 将byte数组转换成InputStream  
  86.      * @param in  
  87.      * @return  
  88.      * @throws Exception  
  89.      */  
  90.     public static InputStream byteTOInputStream(byte[] in) throws Exception{  
  91.           
  92.         ByteArrayInputStream is = new ByteArrayInputStream(in);  
  93.         return is;  
  94.     }  
  95.       
  96.     /**  
  97.      * 将byte数组转换成String  
  98.      * @param in  
  99.      * @return  
  100.      * @throws Exception  
  101.      */  
  102.     public static String byteTOString(byte[] in) throws Exception{  
  103.           
  104.         InputStream is = byteTOInputStream(in);  
  105.         return InputStreamTOString(is);  
  106.     }  
  107.   
  108. }  
0 0
原创粉丝点击