ByteArrayOutputStream 與InputStream 互相轉換


InputStream 轉為 ByteArrayOutputStream

public Reader(InputStream input) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
} catch (IOException e) {
throw new Exception("Illegal flow.");
} finally {
try {
input.close();
} catch (IOException e) {
logger.error("file stream shutdown failed.");
}
}
this.baos = baos;
}
 
ByteArrayOutputStream 轉為 InputStream

private InputStream streamTran(ByteArrayOutputStream in) {
return new ByteArrayInputStream(in.toByteArray());
}

————————————————
版權聲明:本文為CSDN博主「Mr-先森你好」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u011067966/article/details/97757260


免責聲明!

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



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