java.nio.channels.spi
Class AbstractInterruptibleChannel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel
Direct Known Subclasses:
FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel , SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel

public abstract class AbstractInterruptibleChannel
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel

Base implementation class for interruptible channels.

This class encapsulates the low-level machinery required to implement the asynchronous closing and interruption of channels. A concrete channel class must invoke the begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() and end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) methods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that the end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) method is always invoked, these methods should be used within a try ... finally block:

 boolean completed = false;
 try {
     begin();
     completed = ...;    // Perform blocking I/O operation
     return ...;         // Return result
 } finally {
     end(completed);
 }

The completed argument to the end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) method tells whether or not the I/O operation actually completed, that is, whether it had any effect that would be visible to the invoker. In the case of an operation that reads bytes, for example, this argument should be true if, and only if, some bytes were actually transferred into the invoker's target buffer.

A concrete channel class must also implement the implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() method in such a way that if it is invoked while another thread is blocked in a native I/O operation upon the channel then that operation will immediately return, either by throwing an exception or by returning normally. If a thread is interrupted or the channel upon which it is blocked is asynchronously closed then the channel's end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) method will throw the appropriate exception.

This class performs the synchronization required to implement the Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel specification. Implementations of the implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() method need not synchronize against other threads that might be attempting to close the channel.

Since:
1.4

Constructor Summary
protected AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.AbstractInterruptibleChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.AbstractInterruptibleChannel() ()
          Initializes a new instance of this class.
 
Method Summary
protected  void begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() ()
          Marks the beginning of an I/O operation that might block indefinitely.
 void close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() ()
          Closes this channel.
protected  void end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) (boolean completed)
          Marks the end of an I/O operation that might block indefinitely.
protected abstract  void implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() ()
          Closes this channel.
 boolean isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() ()
          Tells whether or not this channel is open.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel()

protected AbstractInterruptibleChannel()
Initializes a new instance of this class.

Method Detail

close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close()

public final void close()
                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Closes this channel.

If the channel has already been closed then this method returns immediately. Otherwise it marks the channel as closed and then invokes the implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() method in order to complete the close operation.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Specified by:
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() in interface Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
Specified by:
close sample code for java.nio.channels.InterruptibleChannel.close() definition code for java.nio.channels.InterruptibleChannel.close() in interface InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel()

protected abstract void implCloseChannel()
                                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Closes this channel.

This method is invoked by the close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.

An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.

Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs while closing the channel

isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen()

public final boolean isOpen()
Description copied from interface: Channel sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
Tells whether or not this channel is open.

Specified by:
isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen() in interface Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
Returns:
true if, and only if, this channel is open

begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin()

protected final void begin()
Marks the beginning of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) method, using a try ... finally block as shown above, in order to implement asynchronous closing and interruption for this channel.


end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean)

protected final void end(boolean completed)
                  throws AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException 
Marks the end of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() method, using a try ... finally block as shown above, in order to implement asynchronous closing and interruption for this channel.

Parameters:
completed - true if, and only if, the I/O operation completed successfully, that is, had some effect that would be visible to the operation's invoker
Throws:
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If the channel was asynchronously closed
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If the thread blocked in the I/O operation was interrupted