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

David M. Lloyd david.lloyd at redhat.com
Fri Jul 19 17:47:54 UTC 2013

On 07/19/2013 12:43 PM, 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 ?

Even if you did not, relying on file size and then reading contents is 
inherently racy anyway.  Using size as a hint to preallocate, and then 
allowing reallocation as needed, seems like the smart policy.


More information about the core-libs-dev mailing list