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

Martin Buchholz martinrb at google.com
Mon Jul 22 18:58:05 UTC 2013


On Mon, Jul 22, 2013 at 10:16 AM, roger riggs <roger.riggs at oracle.com>wrote:

> Hi,
>
> All I was thinking was:
>
>     if (size == 0) {
>        use BAOS... code from 1st webrev.
>

More efficient would be to dip your toe in the water with
int c = read()
and recover if you don't get back -1.



>     } else {
>         current code
>     }
>
> But I am curious why fixing the size() method is not a better fix; if
> FileChannel
> can return incorrect values that seems like a more serious flaw in the
> implementation.
> And it will contaminate code that uses it with the uncertainty about the
> true size.


It's the OS/filesystem implementer deciding to return a bogus number.

More accurate would be to count by reading all the bytes, but that is
impractical and/or impossible (e.g. don't try reading "all the bytes" from
/dev/null)
A middle ground would be to mistrust specifically file sizes of zero,
testing via
int c = read()
but then what?  Check for /proc bug on Linux?



More information about the core-libs-dev mailing list