What happens if a Channel is read after being closed?

3 messages Options
Embed this post
Permalink
Gili

What happens if a Channel is read after being closed?

Reply Threaded More More options
Print post
Permalink
The Javadoc talks about what happens if a channel is closed while an operation is pending but it says nothing about what happens if someone tries reading after a close() operation completed. Shouldn't the methods throw an appropriate exception?

Gili
Gili

Re: What happens if a Channel is read after being closed?

Reply Threaded More More options
Print post
Permalink
Oops, AsynchronousChannel.close()'s Javadoc reads:

"further attempts to initiate asynchronous I/O operations complete immediately with cause ClosedChannelException."

That being said, shouldn't the read/write methods still document this as an exception that may be thrown?

Gili

Gili wrote:
The Javadoc talks about what happens if a channel is closed while an operation is pending but it says nothing about what happens if someone tries reading after a close() operation completed. Shouldn't the methods throw an appropriate exception?

Gili
Alan Bateman

Re: What happens if a Channel is read after being closed?

Reply Threaded More More options
Print post
Permalink
Gili wrote:
> Oops, AsynchronousChannel.close()'s Javadoc reads:
>
> "further attempts to initiate asynchronous I/O operations complete
> immediately with cause ClosedChannelException."
>
> That being said, shouldn't the read/write methods still document this as an
> exception that may be thrown?
>
>  
The read/write methods don't throw I/O exceptions or any checked
exception, at least not directly. Instead the I/O operation completes
immediately with ClosedChannelException. So where you use a
CompletionHandler, its failed method is invoked with the
ClosedChannelException. If using a Future then its get method throws
ExecutionException with ClosedChannelException as the cause. I agree
this isn't obvious from just looking at the specific I/O methods so I'll
look into how we might improve this.

-Alan