java - inputstream转为byte数组 数组越界
问题描述
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;
相关文章:
1. docker start -a dockername 老是卡住,什么情况?2. 在windows下安装docker Toolbox 启动Docker Quickstart Terminal 失败!3. docker-compose中volumes的问题4. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?5. angular.js - angularjs的自定义过滤器如何给文字加颜色?6. angular.js使用$resource服务把数据存入mongodb的问题。7. dockerfile - 为什么docker容器启动不了?8. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?9. javascript - 正则匹配字符串特定语句后的数字10. docker-machine添加一个已有的docker主机问题

网公网安备