增加 Java FileInputStream 使用的内部缓冲区大小
在 FileInputStream
上调用 read(byte[])
时,读取大小始终为 8k,即使 byte[]
呈指数级大。
如何增加每次调用返回的最大读取量?
请不要建议仅仅掩盖 FileInputStream
限制的方法。
更新:似乎没有真正的解决方案。然而,我计算出在我的系统上,对于 1G 文件,方法调用开销约为 226uS。可以肯定地说,这不会以任何实际方式影响性能。
When calling read(byte[])
on a FileInputStream
, the read size is always 8k, even if byte[]
is exponentially large.
How do you increase the max read amount returned per call?
Please do not suggest a method that merely masks the limitation of FileInputStream
.
Update: There doesn't seem to be a real solution to this. However, I calculated the method call overhead to about 226uS on my system, for 1G file. It's probably safe to say this is not going to impact the performance in any real way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其包装在 BufferedInputStream 中,它允许您指定缓冲区大小。
Wrap it in a BufferedInputStream which allows you to specify the buffer size.
您可以尝试使用 NIO 来内存映射文件,但我不确定 8K 的问题是什么。
您可以将 8K 复制到更大的数组,也可以使用返回的长度来调用,
其中 off 是上次读取的返回值。
You could try to memory map the file by using NIO, but I'm not sure what the problem with 8K is.
You can either copy the 8K to your bigger array or use the returned length to call
With off being the return value from the last read.
您看到的每次读取的大小可能是操作系统本身使用的缓冲区大小。因此,您可能必须在操作系统级别进行更改。你如何做到这一点将取决于系统。您可以在创建文件系统时指定块大小。传统上,这对于 Unix 文件系统来说是可能的。尽管具有讽刺意味的是,我认为该功能用于为预计有许多小文件的文件系统提供更小的块。
The size of each read you see might be the buffer size used by the operating system itself. So you might have to make a change at the OS level. How you do that would be system dependent. You might be able to specify the block size when creating the file system. This has traditionally been possible for Unix filesystems. Although ironically I believe the feature was used to have smaller blocks for filesystems expected to have many small files.