zoukankan      html  css  js  c++  java
  • byte[]和InputStream的相互转换

    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为转换之后的结果 
    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;  
        }  
      
    }
  • 相关阅读:
    MFC常用控件使用方法
    用CImage类来显示PNG、JPG等图片
    javascript
    gSoap学习笔记
    Linux增加Swap分区
    nagios 监控shell脚本
    新机器连接老机器遇到的ERROR
    linux下PS1命令提示符设置
    python基础篇之进阶
    mysql不能使用localhost登录
  • 原文地址:https://www.cnblogs.com/zh-1721342390/p/10717399.html
Copyright © 2011-2022 走看看