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

lihuaming (A) lihuaming3 at huawei.com
Thu Jan 28 00:49:29 UTC 2021

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?

The reason for such a request is: We met some condition where user is relying on Channel.isOpen to tell if the underlying socket is usable (I know this is not the expected behavior) and then call FileChannel.transferFrom. it looks like following code snippet:
while (srcSocketChannel.isOpen()) {
  long l = fileChannel.transferFrom(srcSocketChannel,...);
  if (l == -1) { // break out the while loop }
There is one condition, where peer side has closed the underlying socket, at this situation, the above loop will be a infinite loop.

I think it's more friendly for end user if we could change the behavior of FileChannel.transferFrom/transferFromArbitraryChannel to return -1 when src.read returns -1 and tw is 0.

How do you think about it?
It's tracked by https://bugs.openjdk.java.net/browse/JDK-8260486


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

More information about the nio-dev mailing list