FileDocCategorySizeDatePackage
AbstractSelectableChannel.javaAPI DocAndroid 1.5 API10133Wed May 06 22:41:04 BST 2009java.nio.channels.spi

AbstractSelectableChannel

public abstract class AbstractSelectableChannel extends SelectableChannel
{@code AbstractSelectableChannel} is the base implementation class for selectable channels. It declares methods for registering, unregistering and closing selectable channels. It is thread-safe.
since
Android 1.0

Fields Summary
private final SelectorProvider
provider
private List
keyList
private final Object
blockingLock
boolean
isBlocking
Constructors Summary
protected AbstractSelectableChannel(SelectorProvider selectorProvider)
Constructs a new {@code AbstractSelectableChannel}.

param
selectorProvider the selector provider that creates this channel.
since
Android 1.0


                                      
       
        super();
        provider = selectorProvider;
    
Methods Summary
public final java.lang.ObjectblockingLock()
Gets the object used for the synchronization of {@code register} and {@code configureBlocking}.

return
the synchronization object.
since
Android 1.0

        return blockingLock;
    
public final java.nio.channels.SelectableChannelconfigureBlocking(boolean blockingMode)
Sets the blocking mode of this channel. A call to this method blocks if other calls to this method or to {@code register} are executing. The actual setting of the mode is done by calling {@code implConfigureBlocking(boolean)}.

param
blockingMode {@code true} for setting this channel's mode to blocking, {@code false} to set it to non-blocking.
return
this channel.
throws
ClosedChannelException if this channel is closed.
throws
IllegalBlockingModeException if {@code block} is {@code true} and this channel has been registered with at least one selector.
throws
IOException if an I/O error occurs.
since
Android 1.0

        if (isOpen()) {
            synchronized (blockingLock) {
                if (isBlocking == blockingMode) {
                    return this;
                }
                if (blockingMode && isRegistered()) {
                    throw new IllegalBlockingModeException();
                }
                implConfigureBlocking(blockingMode);
                isBlocking = blockingMode;
            }
            return this;
        }
        throw new ClosedChannelException();

    
synchronized voidderegister(java.nio.channels.SelectionKey k)

        if (null != keyList) {
            keyList.remove(k);
        }
    
protected final synchronized voidimplCloseChannel()
Implements the channel closing behavior. Calls {@code implCloseSelectableChannel()} first, then loops through the list of selection keys and cancels them, which unregisters this channel from all selectors it is registered with.

throws
IOException if a problem occurs while closing the channel.
since
Android 1.0

        implCloseSelectableChannel();
        for (int i = 0; i < keyList.size(); i++) {
            SelectionKey key = keyList.get(i);
            if (null != key) {
                key.cancel();
            }
        }
    
protected abstract voidimplCloseSelectableChannel()
Implements the closing function of the SelectableChannel. This method is called from {@code implCloseChannel()}.

throws
IOException if an I/O exception occurs.
since
Android 1.0

protected abstract voidimplConfigureBlocking(boolean blockingMode)
Implements the setting of the blocking mode.

param
blockingMode {@code true} for setting this channel's mode to blocking, {@code false} to set it to non-blocking.
throws
IOException if an I/O error occurs.
since
Android 1.0

public final booleanisBlocking()
Indicates whether this channel is in blocking mode.

return
{@code true} if this channel is blocking, {@code false} otherwise.
since
Android 1.0

        synchronized (blockingLock) {
            return isBlocking;
        }
    
public final synchronized booleanisRegistered()
Indicates whether this channel is registered with one or more selectors.

return
{@code true} if this channel is registered with a selector, {@code false} otherwise.
since
Android 1.0

        return !keyList.isEmpty();
    
public final synchronized java.nio.channels.SelectionKeykeyFor(java.nio.channels.Selector selector)
Gets this channel's selection key for the specified selector.

param
selector the selector with which this channel has been registered.
return
the selection key for the channel or {@code null} if this channel has not been registered with {@code selector}.
since
Android 1.0

        for (int i = 0; i < keyList.size(); i++) {
            SelectionKey key = keyList.get(i);
            if (null != key && key.selector() == selector) {
                return key;
            }
        }
        return null;
    
public final java.nio.channels.spi.SelectorProviderprovider()
Returns the selector provider that has created this channel.

see
java.nio.channels.SelectableChannel#provider()
return
this channel's selector provider.
since
Android 1.0

        return provider;
    
public final java.nio.channels.SelectionKeyregister(java.nio.channels.Selector selector, int interestSet, java.lang.Object attachment)
Registers this channel with the specified selector for the specified interest set. If the channel is already registered with the selector, the {@link SelectionKey interest set} is updated to {@code interestSet} and the corresponding selection key is returned. If the channel is not yet registered, this method calls the {@code register} method of {@code selector} and adds the selection key to this channel's key set.

param
selector the selector with which to register this channel.
param
interestSet this channel's {@link SelectionKey interest set}.
param
attachment the object to attach, can be {@code null}.
return
the selection key for this registration.
throws
CancelledKeyException if this channel is registered but its key has been canceled.
throws
ClosedChannelException if this channel is closed.
throws
IllegalArgumentException if {@code interestSet} is not supported by this channel.
throws
IllegalBlockingModeException if this channel is in blocking mode.
throws
IllegalSelectorException if this channel does not have the same provider as the given selector.
since
Android 1.0

        if (!isOpen()) {
            throw new ClosedChannelException();
        }
        if (!((interestSet & ~validOps()) == 0)) {
            throw new IllegalArgumentException();
        }

        synchronized (blockingLock) {
            if (isBlocking) {
                throw new IllegalBlockingModeException();
            }
            if (!selector.isOpen()) {
                if (0 == interestSet) {
                    // throw ISE exactly to keep consistency
                    throw new IllegalSelectorException();
                }
                // throw NPE exactly to keep consistency
                throw new NullPointerException();
            }
            if (0 == interestSet) {
                // throw ISE exactly to keep consistency
                throw new IllegalSelectorException();
            }
            SelectionKey key = keyFor(selector);
            if (null == key) {
                key = ((AbstractSelector) selector).register(this, interestSet,
                        attachment);
                keyList.add(key);
            } else {
                if (!key.isValid()) {
                    throw new CancelledKeyException();
                }
                key.interestOps(interestSet);
                key.attach(attachment);
            }
            return key;
        }