public static byte[] getBytes2(InputStream inputStream) { try { List<byte[]> buffers = new ArrayList<byte[]>(); int size = 0; int read; do { byte[] buffer = new byte[bufferSize]; read = inputStream.read(buffer); if (read > 0) { size += read; buffers.add(Arrays.copyOf(buffer, read)); } } while (read == bufferSize); byte[] bytes = new byte[size]; int position = 0; for (byte[] buffer : buffers) { System.arraycopy(buffer, 0, bytes, position, buffer.length); position += buffer.length; } return bytes; } catch (Exception e) { throw ExceptionHandler.throwException(e); } finally { Closer.close(inputStream); } }
/** * 获取InputStream对象指定长度的字节内容 * * @param inputStream * @return 文件字节内容 */ public static byte[] getBytes(InputStream inputStream) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int read; byte[] buffer = new byte[bufferSize]; while ((read = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, read); } return byteArrayOutputStream.toByteArray(); } catch (Exception e) { throw ExceptionHandler.throwException(e); } finally { Closer.close(inputStream); } }