RFR [8020669] java.nio.file.Files.readAllBytes() does not read any data when Files.size() is 0

Ivan Gerasimov ivan.gerasimov at oracle.com
Fri Jul 19 18:11:24 UTC 2013

On 19.07.2013 21:43, Remi Forax wrote:
> On 07/19/2013 07:15 PM, Ivan Gerasimov wrote:
>> Hello everybody!
>> Would you please review a fix for the problem with 
>> j.n.f.Files.readAllBytes() function?
>> The current implementation relies on FileChannel.size() to 
>> preallocate a buffer for the whole file's content.
>> However, some special filesystems can report a wrong size.
>> An example is procfs under Linux, which reports many files under 
>> /proc to be zero sized.
>> Thus it is proposed not to rely on the size() and instead 
>> continuously read until EOF.
>> The downside is reallocation and copying file content between buffers.
>> But taking into account that the doc says: "It is not intended for 
>> reading in large files." it should not be a big problem.
>> http://bugs.sun.com/view_bug.do?bug_id=8020669
>> http://cr.openjdk.java.net/~igerasim/8020669/0/webrev/
>> The fix is for JDK8. If it is approved, it can be applied to JDK7 as 
>> well.
>> Sincerely yours,
>> Ivan Gerasimov
> Is not better to just not trust the filesystem when a call to size() 
> returns 0 ?
Then we couldn't use Files.readAllBytes() to read /proc/* files.

Here's what the documentation says about Files.size(): "The size may 
differ from the actual size on the file system due to compression, 
support for sparse files, or other reasons."

So it is already said, that the exact file size is not always known.
Then why should we rely on this information when trying to read all 
bytes of the file?

Sincerely yours,

> Rémi

More information about the core-libs-dev mailing list