Package java.nio.channels.spi
Class AbstractSelectableChannel
- java.lang.Object
-
- java.nio.channels.spi.AbstractInterruptibleChannel
-
- java.nio.channels.SelectableChannel
-
- java.nio.channels.spi.AbstractSelectableChannel
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,InterruptibleChannel
- Direct Known Subclasses:
DatagramChannel,Pipe.SinkChannel,Pipe.SourceChannel,ServerSocketChannel,SocketChannel
public abstract class AbstractSelectableChannel extends SelectableChannel
AbstractSelectableChannelis the base implementation class for selectable channels. It declares methods for registering, unregistering and closing selectable channels. It is thread-safe.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractSelectableChannel(SelectorProvider selectorProvider)Constructs a newAbstractSelectableChannel.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ObjectblockingLock()Gets the object used for the synchronization ofregisterandconfigureBlocking.SelectableChannelconfigureBlocking(boolean blockingMode)Sets the blocking mode of this channel.protected voidimplCloseChannel()Implements the channel closing behavior.protected abstract voidimplCloseSelectableChannel()Implements the closing function of the SelectableChannel.protected abstract voidimplConfigureBlocking(boolean blocking)Implements the configuration of blocking/non-blocking mode.booleanisBlocking()Indicates whether this channel is in blocking mode.booleanisRegistered()Indicates whether this channel is registered with one or more selectors.SelectionKeykeyFor(Selector selector)Gets this channel's selection key for the specified selector.SelectorProviderprovider()Returns the selector provider that has created this channel.SelectionKeyregister(Selector selector, int interestSet, Object attachment)Registers this channel with the specified selector for the specified interest set.-
Methods inherited from class java.nio.channels.SelectableChannel
register, validOps
-
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
-
-
-
-
Constructor Detail
-
AbstractSelectableChannel
protected AbstractSelectableChannel(SelectorProvider selectorProvider)
Constructs a newAbstractSelectableChannel.- Parameters:
selectorProvider- the selector provider that creates this channel.
-
-
Method Detail
-
provider
public final SelectorProvider provider()
Returns the selector provider that has created this channel.- Specified by:
providerin classSelectableChannel- Returns:
- this channel's selector provider.
- See Also:
SelectableChannel.provider()
-
isRegistered
public final boolean isRegistered()
Indicates whether this channel is registered with one or more selectors.- Specified by:
isRegisteredin classSelectableChannel- Returns:
trueif this channel is registered with a selector,falseotherwise.
-
keyFor
public final SelectionKey keyFor(Selector selector)
Gets this channel's selection key for the specified selector.- Specified by:
keyForin classSelectableChannel- Parameters:
selector- the selector with which this channel has been registered.- Returns:
- the selection key for the channel or
nullif this channel has not been registered withselector.
-
register
public final SelectionKey register(Selector selector, int interestSet, Object attachment) throws ClosedChannelException
Registers this channel with the specified selector for the specified interest set. If the channel is already registered with the selector, theinterest setis updated tointerestSetand the corresponding selection key is returned. If the channel is not yet registered, this method calls theregistermethod ofselectorand adds the selection key to this channel's key set.- Specified by:
registerin classSelectableChannel- Parameters:
selector- the selector with which to register this channel.interestSet- this channel'sinterest set.attachment- the object to attach, can benull.- Returns:
- the selection key for this registration.
- Throws:
CancelledKeyException- if this channel is registered but its key has been canceled.ClosedChannelException- if this channel is closed.IllegalArgumentException- ifinterestSetis not supported by this channel.IllegalBlockingModeException- if this channel is in blocking mode.IllegalSelectorException- if this channel does not have the same provider as the given selector.
-
implCloseChannel
protected final void implCloseChannel() throws IOExceptionImplements the channel closing behavior. CallsimplCloseSelectableChannel()first, then loops through the list of selection keys and cancels them, which unregisters this channel from all selectors it is registered with.- Specified by:
implCloseChannelin classAbstractInterruptibleChannel- Throws:
IOException- if a problem occurs while closing the channel.
-
implCloseSelectableChannel
protected abstract void implCloseSelectableChannel() throws IOExceptionImplements the closing function of the SelectableChannel. This method is called fromimplCloseChannel().- Throws:
IOException- if an I/O exception occurs.
-
isBlocking
public final boolean isBlocking()
Indicates whether this channel is in blocking mode.- Specified by:
isBlockingin classSelectableChannel- Returns:
trueif this channel is blocking,falseotherwise.
-
blockingLock
public final Object blockingLock()
Gets the object used for the synchronization ofregisterandconfigureBlocking.- Specified by:
blockingLockin classSelectableChannel- Returns:
- the synchronization object.
-
configureBlocking
public final SelectableChannel configureBlocking(boolean blockingMode) throws IOException
Sets the blocking mode of this channel. A call to this method blocks if other calls to this method or toregisterare executing. The actual setting of the mode is done by callingimplConfigureBlocking(boolean).- Specified by:
configureBlockingin classSelectableChannel- Parameters:
blockingMode-truefor setting this channel's mode to blocking,falseto set it to non-blocking.- Returns:
- this channel.
- Throws:
ClosedChannelException- if this channel is closed.IllegalBlockingModeException- ifblockistrueand this channel has been registered with at least one selector.IOException- if an I/O error occurs.- See Also:
SelectableChannel.configureBlocking(boolean)
-
implConfigureBlocking
protected abstract void implConfigureBlocking(boolean blocking) throws IOExceptionImplements the configuration of blocking/non-blocking mode.- Parameters:
blocking- true for blocking, false for non-blocking.- Throws:
IOException- if an I/O error occurs.
-
-