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;  
        }  
      
    }
    /*InputStream in = this.getClass().getResourceAsStream(path);
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            in.close();*/
  • 相关阅读:
    Oracle数据库五种约束
    Oracle中sql的基本使用
    暮光之城》剧本
    英语电影剧本大全(中英对照)
    加勒比海盗1英文剧本
    公主日记英文剧本
    教父2-英文版
    <老友记>学习笔记
    霍夫变换Hough
    景深(Depth of Field)
  • 原文地址:https://www.cnblogs.com/lukelook/p/11067725.html
Copyright © 2011-2022 走看看