Android · 2016年7月18日 0

InputStream与String,Byte之间互转

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

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. /**
  6.  *
  7.  * @author Andy.Chen
  8.  * @mail Chenjunjun.ZJ@gmail.com
  9.  *
  10.  */
  11. public class InputStreamUtils {
  12.     final static int BUFFER_SIZE = 4096;
  13.     /**
  14.      * 将InputStream转换成String
  15.      * @param in InputStream
  16.      * @return String
  17.      * @throws Exception
  18.      *
  19.      */
  20.     public static String InputStreamTOString(InputStream in) throws Exception{
  21.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  22.         byte[] data = new byte[BUFFER_SIZE];
  23.         int count = -1;
  24.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)
  25.             outStream.write(data, 0, count);
  26.         data = null;
  27.         return new String(outStream.toByteArray(),”ISO-8859-1″);
  28.     }
  29.     /**
  30.      * 将InputStream转换成某种字符编码的String
  31.      * @param in
  32.      * @param encoding
  33.      * @return
  34.      * @throws Exception
  35.      */
  36.          public static String InputStreamTOString(InputStream in,String encoding) throws Exception{
  37.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  38.         byte[] data = new byte[BUFFER_SIZE];
  39.         int count = -1;
  40.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)
  41.             outStream.write(data, 0, count);
  42.         data = null;
  43.         return new String(outStream.toByteArray(),”ISO-8859-1″);
  44.     }
  45.     /**
  46.      * 将String转换成InputStream
  47.      * @param in
  48.      * @return
  49.      * @throws Exception
  50.      */
  51.     public static InputStream StringTOInputStream(String in) throws Exception{
  52.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes(“ISO-8859-1”));
  53.         return is;
  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.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  63.         byte[] data = new byte[BUFFER_SIZE];
  64.         int count = -1;
  65.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)
  66.             outStream.write(data, 0, count);
  67.         data = null;
  68.         return outStream.toByteArray();
  69.     }
  70.     /**
  71.      * 将byte数组转换成InputStream
  72.      * @param in
  73.      * @return
  74.      * @throws Exception
  75.      */
  76.     public static InputStream byteTOInputStream(byte[] in) throws Exception{
  77.         ByteArrayInputStream is = new ByteArrayInputStream(in);
  78.         return is;
  79.     }
  80.     /**
  81.      * 将byte数组转换成String
  82.      * @param in
  83.      * @return
  84.      * @throws Exception
  85.      */
  86.     public static String byteTOString(byte[] in) throws Exception{
  87.         InputStream is = byteTOInputStream(in);
  88.         return InputStreamTOString(is);
  89.     }
  90. }

Share this: