athrunwang

          紀元
          數據加載中……
          InputStream與String,Byte之間互轉
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.InputStream;

          /**
           *
           * @author Andy.Chen
           * @mail Chenjunjun.ZJ@gmail.com
           *
           */
          public class InputStreamUtils {
              
              final static int BUFFER_SIZE = 4096;
              
              /**
               * 將InputStream轉換成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");
              }
              
              /**
               * 將InputStream轉換成某種字符編碼的String
               * @param in
               * @param encoding
               * @return
               * @throws Exception
               */
                   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");
              }
              
              /**
               * 將String轉換成InputStream
               * @param in
               * @return
               * @throws Exception
               */
              public static InputStream StringTOInputStream(String in) throws Exception{
                  
                  ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
                  return is;
              }
              
              /**
               * 將InputStream轉換成byte數組
               * @param in InputStream
               * @return byte[]
               * @throws IOException
               */
              public static byte[] InputStreamTOByte(InputStream in) throws IOException{
                  
                  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 outStream.toByteArray();
              }
              
              /**
               * 將byte數組轉換成InputStream
               * @param in
               * @return
               * @throws Exception
               */
              public static InputStream byteTOInputStream(byte[] in) throws Exception{
                  
                  ByteArrayInputStream is = new ByteArrayInputStream(in);
                  return is;
              }
              
              /**
               * 將byte數組轉換成String
               * @param in
               * @return
               * @throws Exception
               */
              public static String byteTOString(byte[] in) throws Exception{
                  
                  InputStream is = byteTOInputStream(in);
                  return InputStreamTOString(is);
              }

          }

          posted on 2012-01-09 20:02 AthrunWang 閱讀(1455) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 陕西省| 荆门市| 德惠市| 沁源县| 大丰市| 宝应县| 东丽区| 岗巴县| 平和县| 普洱| 揭阳市| 龙江县| 旬邑县| 宁德市| 金川县| 漠河县| 襄城县| 德令哈市| 福鼎市| 开阳县| 廊坊市| 尤溪县| 东平县| 华宁县| 萨嘎县| 贵南县| 奉贤区| 万荣县| 辽阳县| 唐山市| 元朗区| 雅安市| 灵山县| 仙桃市| 安化县| 大宁县| 通道| 顺昌县| 岢岚县| 梅州市| 长春市|