`
bluerose
  • 浏览: 145788 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

byte[]和InputStream的相互转换

    博客分类:
  • Java
阅读更多

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

2:InputStream转换为byte[]数组
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;
}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics