1:byte[]转换为InputStream 
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为转换之后的结果

    1. import java.io.ByteArrayInputStream;
    2. import java.io.ByteArrayOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. public class ByteToInputStream {
    6. public static final InputStream byte2Input(byte[] buf) {
    7. return new ByteArrayInputStream(buf);
    8. }
    9. public static final byte[] input2byte(InputStream inStream)
    10. throws IOException {
    11. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    12. byte[] buff = new byte[100];
    13. int rc = 0;
    14. while ((rc = inStream.read(buff, 0, 100)) > 0) {
    15. swapStream.write(buff, 0, rc);
    16. }
    17. byte[] in2b = swapStream.toByteArray();
    18. return in2b;
    19. }
    20. }

byte[]和InputStream的相互转换的更多相关文章

  1. byte[]和InputStream的相互转换[转载]

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...

  2. byte[],File和InputStream的相互转换

    File.FileInputStream 转换为byte[] File file = new File("test.txt"); InputStream input = new F ...

  3. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  4. java中outputStream与inputStream的相互转换

    package com.boco.test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; im ...

  5. Java 中byte 与 char 的相互转换 Java基础 但是很重要

    char转化为byte: public static byte[] charToByte(char c) {        byte[] b = new byte[2];        b[0] = ...

  6. Java中字符串和byte数组之间的相互转换

    1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...

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

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

  8. byte数组和File,InputStream互转

    1.将File.FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = n ...

  9. java 中 byte[]、File、InputStream 互相转换

    1.将File.FileInputStream 转换为byte数组: File file = new File("test.txt"); InputStream input = n ...

随机推荐

  1. CSS3--阴影,渐变,背景图片

    文字阴影.element{ text-shadow:1px 1px 1px #cccccc;}先右再下第一个值:右侧阴影的大小第二个值:下方阴影的大小第三个值:模糊距离(阴影从开始变淡到完全消失的距离 ...

  2. django搭建Bootstrap常用问题解决方法

    1.进入页面,提示Creating a ModelForm without either the 'fields' attribute or the 'exclude'时 解决方法:打开forms.p ...

  3. 导入Excel后绑定GridView实例

    http://blog.csdn.net/loveheronly/article/details/6715552 项目中经常用到导入导出的例子,前面做了导出的例子,现在把导入Excel的数据的例子也把 ...

  4. 深入理解HTTP协议(转) 浏览器和服务器如何通信(HTTP协议)

    http协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  5. LeetCode OJ 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  6. LeetCode OJ 220.Contains Duplicate 3

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  7. MySQL外键的作用和创建

    MySQL外键的作用: 保持数据一致性,完整性,主要目的是控制存储在外键表中的数据.使两张表形成关联,外键只能引用外表中列的值! 我们来建两个表 CREATE TABLE `example1` ( ` ...

  8. 用GDB调试程序的设置 Segmentation fault(Core Dump)调试

    在写wifi库的时候碰见一个 Segmentation fault(Core Dump) 所以需要用GDB调试下. 在cmake的时候,修改CMakeLists.txt set(CMAKE_C_FLA ...

  9. maven source

    accepted To download sources for your dependencies: mvn eclipse:eclipse -DdownloadSources=true To at ...

  10. J2EE判断重复的数据

    import java.util.ArrayList; import java.util.List; import org.junit.Test; /** * 判断重复的数据 * @author Ya ...