InputStreamUtils.java

InputStreamUtils.java
001	import java.io.ByteArrayInputStream;
002	import java.io.ByteArrayOutputStream;
003	import java.io.IOException;
004	import java.io.InputStream;
005	 
006	/**
007	 *
008	 * @author Andy.Chen
009	 * @mail Chenjunjun.ZJ@gmail.com
010	 *
011	 */
012	public class InputStreamUtils {
013	     
014	    final static int BUFFER_SIZE = 4096;
015	     
016	    /**
017	     * 将InputStream转换成String
018	     * @param in InputStream
019	     * @return String
020	     * @throws Exception
022	     */
023	    public static String InputStreamTOString(InputStream in) throws Exception{
024	         
025	        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
026	        byte[] data = new byte[BUFFER_SIZE];
027	        int count = -1;
028	        while((count = in.read(data,0,BUFFER_SIZE)) != -1)
029	            outStream.write(data, 0, count);
030	         
031	        data = null;
032	        return new String(outStream.toByteArray(),"ISO-8859-1");
033	    }
034	     
035	    /**
036	     * 将InputStream转换成某种字符编码的String
037	     * @param in
038	     * @param encoding
039	     * @return
040	     * @throws Exception
041	     */
042	         public static String InputStreamTOString(InputStream in,String encoding) throwsException{
043	         
044	        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
045	        byte[] data = new byte[BUFFER_SIZE];
046	        int count = -1;
047	        while((count = in.read(data,0,BUFFER_SIZE)) != -1)
048	            outStream.write(data, 0, count);
049	         
050	        data = null;
051	        return new String(outStream.toByteArray(),"ISO-8859-1");
052	    }
053	     
054	    /**
055	     * 将String转换成InputStream
056	     * @param in
057	     * @return
058	     * @throws Exception
059	     */
060	    public static InputStream StringTOInputStream(String in) throws Exception{
061	         
062	        ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
063	        return is;
064	    }
065	     
066	    /**
067	     * 将InputStream转换成byte数组
068	     * @param in InputStream
069	     * @return byte[]
070	     * @throws IOException
071	     */
072	    public static byte[] InputStreamTOByte(InputStream in) throws IOException{
073	         
074	        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
075	        byte[] data = new byte[BUFFER_SIZE];
076	        int count = -1;
077	        while((count = in.read(data,0,BUFFER_SIZE)) != -1)
078	            outStream.write(data, 0, count);
079	         
080	        data = null;
081	        return outStream.toByteArray();
082	    }
083	     
084	    /**
085	     * 将byte数组转换成InputStream
086	     * @param in
087	     * @return
088	     * @throws Exception
089	     */
090	    public static InputStream byteTOInputStream(byte[] in) throws Exception{
091	         
092	        ByteArrayInputStream is = new ByteArrayInputStream(in);
093	        return is;
094	    }
095	     
096	    /**
097	     * 将byte数组转换成String
098	     * @param in
099	     * @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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值