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 閱讀(1458) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 涟源市| 大丰市| 晋宁县| 漳浦县| 西林县| 梅州市| 亳州市| 天全县| 海阳市| 尚义县| 佳木斯市| 贵州省| 阳原县| 东明县| 大足县| 嘉善县| 日照市| 裕民县| 雅江县| 大宁县| 游戏| 辽宁省| 龙胜| 丰都县| 博爱县| 高州市| 商河县| 广宗县| 德格县| 怀宁县| 万山特区| 浦县| 二手房| 武冈市| 东丰县| 南乐县| 西昌市| 库车县| 嵊泗县| 峨眉山市| 五莲县|