我想将输入流转换为 byte[],并且我正在使用 IOUtils.toByteArray(inputStream)
。使用像 BufferedInputStream 这样的包装器作为 inputStream 会提高效率吗?它节省内存吗?
最佳答案
Will it make more efficient by wrapper like BufferedInputStream for the inputStream ?
没有任何意义。 IOUtils.toByteArray
将数据读取到 4096 字节的缓冲区中。 BufferedInputStream
默认使用 8192 字节缓冲区。
使用 BufferedInputStream 的 IO 读取次数较少,但您需要非常快的数据源才能注意到任何差异。
如果您一次读取一个字节(或几个字节),那么使用 BufferedInputStream 确实可以提高性能,因为它将操作系统调用的数量减少了 8000 倍。而且操作系统调用需要花费大量时间,相对而言。
Does it save memory ?
没有。无论传入缓冲还是无缓冲的InputStream,IOUtils.toByteArray
都会创建一个新字节[4096]
。创建 BufferdedInputStream 会花费更多的内存。但没什么重要的。
关于java : Does using buffered input stream make the conversion of input streams to byte[] more efficient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50039154/