jnr / jnr-unixsocket

UNIX domain sockets (AF_UNIX) for Java
Apache License 2.0
280 stars 75 forks source link

Why do native reads that return 0 bytes read, return -1? #57

Closed huntc closed 6 years ago

huntc commented 6 years ago

I noticed that when reading from a Unix socket, if the return value of a native read call is 0 then it is returned to the caller as -1. Relevant code:

https://github.com/jnr/jnr-unixsocket/blob/master/src/main/java/jnr/enxio/channels/Common.java#L59

I'm left curious as to why particularly given that I'm having a problem with half-closing Unix sockets. My understanding is that a native read could return zero on receipt of a FIN. I'm not sure if my problem is related, but I'd still like to understand why the translation is performed.

Thanks.

huntc commented 6 years ago

Not to worry. I’d forgotten that the Unix read returns 0 on EOF where NIO’s read returns -1. Hence the translation.