国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

java - inputstream轉為byte數組 數組越界

瀏覽:102日期:2023-12-09 10:37:15

問題描述

public static byte[] readInputStream(InputStream inStream) throws Exception {

try {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray(); }catch (Exception e){e.printStackTrace();throw new Exception(e); }

}

網上都是這種處理方式 寫死有越界的可能性

不知道有沒有其他的處理方式

問題解答

回答1:

最好的方法是用Apache commons IO的IOUtils.toByteArray(inputStream),一行代碼解決。

回答2:

int count = 0;while (count == 0) { count = inStream.available();}byte[] b = new byte[count];inStream.read(b);return b;

標簽: java
相關文章:
主站蜘蛛池模板: 龙岩市| 五峰| 罗城| 鄂尔多斯市| 西藏| 砀山县| 财经| 南和县| 大渡口区| 会宁县| 微山县| 太谷县| 商都县| 宝坻区| 沿河| 错那县| 大荔县| 罗江县| 龙岩市| 织金县| 阜康市| 东方市| 武强县| 巫山县| 丹棱县| 台南县| 团风县| 通海县| 宾阳县| 读书| 凤山市| 晋城| 勐海县| 平南县| 无为县| 江北区| 崇明县| 洛浦县| 仁怀市| 马鞍山市| 金平|