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);
} }

InputStream与String,Byte之间互转的更多相关文章

  1. [技巧篇]19.InputStream与String,Byte之间互转[转载]

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  2. 转:Image与byte之间互转

    #region<Image 与 byte之间互转> /// <summary> /// 将一个byte转换成一个Bitmap对象 /// </summary> // ...

  3. 关于InputStream 和String对象之间的相互转换

    代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  4. InputStream转换为String, byte[] data = new byte[1024]详解

    /** * This file created at 2018年2月28日. * * Copyright (c) 2002-2018 Bingosoft, Inc. All rights reserv ...

  5. java中String\十六进制String\byte[]之间相互转换函数

    java二进制,字节数组,字符,十六进制,BCD编码转换2007-06-07 00:17/** *//** * 把16进制字符串转换成字节数组 * @param hex * @return */ pu ...

  6. inputStream、File、Byte、String等等之间的相互转换

    一:inputStream转换 1.inputStream转为byte //方法一 org.apache.commons.io.IOUtils包下的实现(建议) IOUtils.toByteArray ...

  7. 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

  8. Java中InputStream和String之间的转化

    https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转 ...

  9. Go语言网络通信---string与int互转,int64与[]byte互转,int直接互转,string与[]byte互转

    string与int互转 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt( ...

随机推荐

  1. win7与win server 2008防火墙设置

    转自:http://blog.51cto.com/jimshu/590411 Windows 防火墙通过阻止未授权用户通过 Internet 或网络访问您的计算机来帮助保护计算机. Windows 2 ...

  2. Redis分布式锁的python实现

    案例1: #!/usr/bin/env python # coding=utf-8 import time import redis class RedisLock(object): def __in ...

  3. Python爬虫:爬取自己博客的主页的标题,链接,和发布时间

    代码 # -*- coding: utf-8 -*- """ ------------------------------------------------- File ...

  4. Eclipse 输出的文本乱码

    Version: Mars.2 Release (4.5.2)Build id: 20160218-0600 在使用时,发现文本编辑器中的文字出现了乱码.然后在项目的Properties->&g ...

  5. [转]springmvc中的常用的返回

    package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...

  6. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  7. mysql多表查询原理

    转:https://www.cnblogs.com/Toolo/p/3634563.html MySQL的多表查询(笛卡尔积原理)   先确定数据要用到哪些表. 将多个表先通过笛卡尔积变成一个表. 然 ...

  8. C语言:内存字节对齐详解

    转:http://blog.csdn.net/arethe/article/details/2548867 一.什么是对齐,以及为什么要对齐: 1. 现代计算机中内存空间都是按照byte划分的,从理论 ...

  9. Go goroutine (协程)

    在Go语言中goroutine是一个协程,但是跟Python里面的协程有很大的不同: 在任何函数前只需要加上go关键字就可以定义为协程; 不需要在定义时区分是否是异步函数  VS  async def ...

  10. 如何判断Linux服务器是否被入侵?

    被入侵服务器的症状 当服务器被没有经验攻击者或者自动攻击程序入侵了的话,他们往往会消耗 100% 的资源.他们可能消耗 CPU 资源来进行数字货币的采矿或者发送垃圾邮件,也可能消耗带宽来发动 DoS ...