Why do IOExceptions occur in ReadableByteChannel.read()
Posted
by Steffen Heil
on Stack Overflow
See other posts from Stack Overflow
or by Steffen Heil
Published on 2010-03-15T20:01:59Z
Indexed on
2010/03/15
20:49 UTC
Read the original article
Hit count: 384
Hi
The specification of ReadableByteChannel.read()
shows -1
as result value for end-of-stream. Moreover it specifies ClosedByInterruptException
as possible result if the thread is interrupted.
Now I thought that would be all - and it is most of the time. However, now and then I get the following:
java.io.IOException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
at sun.nio.ch.IOUtil.read(IOUtil.java:206)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:236)
at ...
I do not unterstand why I don't get -1
in this case. Also this is not a clean exception, as I cannot catch it without catching any possible IOException
.
So here are my questions:
- Why is this exception thrown in the first place?
- Is it safe to assume that ANY exception thrown by read are about the socket being closed?
- Is all this the same for
write()
?
And by the way: If I call SocketChannel.close()
do I have to call SocketChannel.socket().close()
as well or is this implied by the earlier?
Thanks, Steffen
© Stack Overflow or respective owner