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();*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM