Class AbstractSelectableChannel

All Implemented Interfaces:
Closeable, AutoCloseable, Channel, InterruptibleChannel
Direct Known Subclasses:
DatagramChannel, Pipe.SinkChannel, Pipe.SourceChannel, ServerSocketChannel, SocketChannel

public abstract class AbstractSelectableChannel
extends SelectableChannel
AbstractSelectableChannel is the base implementation class for selectable channels. It declares methods for registering, unregistering and closing selectable channels. It is thread-safe.
  • Constructor Details

    • AbstractSelectableChannel

      protected AbstractSelectableChannel​(SelectorProvider selectorProvider)
      Constructs a new AbstractSelectableChannel.
      Parameters:
      selectorProvider - the selector provider that creates this channel.
  • Method Details

    • provider

      public final SelectorProvider provider()
      Returns the selector provider that has created this channel.
      Specified by:
      provider in class SelectableChannel
      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:
      isRegistered in class SelectableChannel
      Returns:
      true if this channel is registered with a selector, false otherwise.
    • keyFor

      public final SelectionKey keyFor​(Selector selector)
      Gets this channel's selection key for the specified selector.
      Specified by:
      keyFor in class SelectableChannel
      Parameters:
      selector - the selector with which this channel has been registered.
      Returns:
      the selection key for the channel or null if this channel has not been registered with selector.
    • 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, the interest set is updated to interestSet and the corresponding selection key is returned. If the channel is not yet registered, this method calls the register method of selector and adds the selection key to this channel's key set.
      Specified by:
      register in class SelectableChannel
      Parameters:
      selector - the selector with which to register this channel.
      interestSet - this channel's interest set.
      attachment - the object to attach, can be null.
      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 - if interestSet is 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 IOException
      Implements the channel closing behavior. Calls implCloseSelectableChannel() 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:
      implCloseChannel in class AbstractInterruptibleChannel
      Throws:
      IOException - if a problem occurs while closing the channel.
    • implCloseSelectableChannel

      protected abstract void implCloseSelectableChannel() throws IOException
      Implements the closing function of the SelectableChannel. This method is called from implCloseChannel().
      Throws:
      IOException - if an I/O exception occurs.
    • isBlocking

      public final boolean isBlocking()
      Indicates whether this channel is in blocking mode.
      Specified by:
      isBlocking in class SelectableChannel
      Returns:
      true if this channel is blocking, false otherwise.
    • blockingLock

      public final Object blockingLock()
      Gets the object used for the synchronization of register and configureBlocking.
      Specified by:
      blockingLock in class SelectableChannel
      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 to register are executing. The actual setting of the mode is done by calling implConfigureBlocking(boolean).
      Specified by:
      configureBlocking in class SelectableChannel
      Parameters:
      blockingMode - true for setting this channel's mode to blocking, false to set it to non-blocking.
      Returns:
      this channel.
      Throws:
      ClosedChannelException - if this channel is closed.
      IllegalBlockingModeException - if block is true and 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 IOException
      Implements the configuration of blocking/non-blocking mode.
      Parameters:
      blocking - true for blocking, false for non-blocking.
      Throws:
      IOException - if an I/O error occurs.