AsynchronousFileChannel end of file

Alan Bateman Alan.Bateman at oracle.com
Thu Aug 4 06:57:51 PDT 2011


Tim Fox wrote:
> Hello,
>
> It's not clear to me how to tell if I have read to end of file using 
> AsynchronousFileChannel.read().
>
> The javadoc mentions that if -1 is returned as number of bytes read 
> than that means the position was >= file size at time of read.
>
> However, if the read returns less bytes read than the limit of the 
> ByteBuffer passed in, does that also signify end of file was reached, 
> or is it sometimes possible to get partial reads that don't fill the 
> buffer even though there is more data in the file to read?
>
> In other words, do I always have to issue reads until I received bytes 
> read == -1?
>
> Thanks again.
Yes, you have to read -1 to know that you've hit EOF. That is, a short 
read isn't guaranteeing that you've read to the end.

-Alan.


More information about the nio-discuss mailing list