byte[]和InputStream的相互转换[转载]
2016-03-04 10:07
309 查看
1:byte[]转换为InputStream
2:InputStream转换为InputStreambyte[]
java代码:
转载自:http://javapub.iteye.com/blog/665696
InputStream sbs = new ByteArrayInputStream(byte[] buf);
2:InputStream转换为InputStreambyte[]
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据 int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果
java代码:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class ByteToInputStream { public static final InputStream byte2Input(byte[] buf) { return new ByteArrayInputStream(buf); } public static final byte[] input2byte(InputStream inStream) throws IOException { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in2b = swapStream.toByteArray(); return in2b; } }
转载自:http://javapub.iteye.com/blog/665696
相关文章推荐
- awk使用shell变量,shell获取awk中…
- 《将博客搬至CSDN》
- [置顶] 基于nutcracker …
- nginx 获取源IP
- Session存储到Redis
- linux shell脚本用while&nbsp…
- 如何解决“504 Gateway T…
- 使用supervisor监控进程
- MYSQL order by排序与索引关系总结
- 为什么V8引擎这么快?
- Tou 4000 chSlop,Velocity,GestureDector
- 如何安装nginx第三方模块
- nginx禁止IP直接访问可能禁止非本…
- Nginx中的rewrite指令
- 轻量级HTTP服务器Nginx(常用配置…
- 服务器数据库系列 - Ngi…
- Nginx配置性能优化
- linux中的 tar命令的 -C…
- PreLoadHack
- PathEffectTextView Update