(fc) FileChannel.transferFrom/transferFromArbitraryChannel to return -1 at some error condition

Alan Bateman Alan.Bateman at oracle.com
Thu Jan 28 08:19:27 UTC 2021


On 28/01/2021 00:49, lihuaming (A) wrote:
>
> Hi Everyone,
>
> in FileChannel.transferFromArbitraryChannel, when src.read returns -1, 
> it means "the channel has reached end-of-stream", it could be a socket 
> underlying is reset by peer host, or some other conditions. But this 
> "-1" is completely hidden when transferFromArbitraryChannel returns 
> back to File.Channel.transferFrom.
>
> Would it better to return -1 in 
> FileChannel.transferFrom/transferFromArbitraryChannel when src.read 
> returns -1 and tw is 0?
>
>
Hi Hamlin,

The transferFrom method returns the number of bytes written to the file. 
Changing the spec to allow it return -1 would be an incompatible change 
and could potentially break usages that keep a running total of bytes 
transferred.

Can you expand a bit on the scenario? If the underlying connection has 
been reset or there is another error then I would expect the read from 
the source channel to fail (SocketException "connection reset" for 
example). This is different to the peer closing the connection 
gracefully where reading from the source channel returns -1/EOF and 
there are no bytes written to the file.

-Alan.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20210128/779b080b/attachment.htm>


More information about the nio-dev mailing list