0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.nio.channels;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.nio.channels.spi.AbstractInterruptibleChannel;
0N/Aimport java.nio.channels.spi.SelectorProvider;
0N/A
0N/A
0N/A/**
0N/A * A channel that can be multiplexed via a {@link Selector}.
0N/A *
0N/A * <p> In order to be used with a selector, an instance of this class must
0N/A * first be <i>registered</i> via the {@link #register(Selector,int,Object)
0N/A * register} method. This method returns a new {@link SelectionKey} object
0N/A * that represents the channel's registration with the selector.
0N/A *
0N/A * <p> Once registered with a selector, a channel remains registered until it
0N/A * is <i>deregistered</i>. This involves deallocating whatever resources were
0N/A * allocated to the channel by the selector.
0N/A *
0N/A * <p> A channel cannot be deregistered directly; instead, the key representing
0N/A * its registration must be <i>cancelled</i>. Cancelling a key requests that
0N/A * the channel be deregistered during the selector's next selection operation.
0N/A * A key may be cancelled explicitly by invoking its {@link
0N/A * SelectionKey#cancel() cancel} method. All of a channel's keys are cancelled
0N/A * implicitly when the channel is closed, whether by invoking its {@link
0N/A * Channel#close close} method or by interrupting a thread blocked in an I/O
0N/A * operation upon the channel.
0N/A *
0N/A * <p> If the selector itself is closed then the channel will be deregistered,
0N/A * and the key representing its registration will be invalidated, without
0N/A * further delay.
0N/A *
0N/A * <p> A channel may be registered at most once with any particular selector.
0N/A *
0N/A * <p> Whether or not a channel is registered with one or more selectors may be
0N/A * determined by invoking the {@link #isRegistered isRegistered} method.
0N/A *
0N/A * <p> Selectable channels are safe for use by multiple concurrent
0N/A * threads. </p>
0N/A *
0N/A *
0N/A * <a name="bm">
0N/A * <h4>Blocking mode</h4>
0N/A *
0N/A * A selectable channel is either in <i>blocking</i> mode or in
0N/A * <i>non-blocking</i> mode. In blocking mode, every I/O operation invoked
0N/A * upon the channel will block until it completes. In non-blocking mode an I/O
0N/A * operation will never block and may transfer fewer bytes than were requested
0N/A * or possibly no bytes at all. The blocking mode of a selectable channel may
0N/A * be determined by invoking its {@link #isBlocking isBlocking} method.
0N/A *
0N/A * <p> Newly-created selectable channels are always in blocking mode.
0N/A * Non-blocking mode is most useful in conjunction with selector-based
0N/A * multiplexing. A channel must be placed into non-blocking mode before being
0N/A * registered with a selector, and may not be returned to blocking mode until
0N/A * it has been deregistered.
0N/A *
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A *
0N/A * @see SelectionKey
0N/A * @see Selector
0N/A */
0N/A
0N/Apublic abstract class SelectableChannel
0N/A extends AbstractInterruptibleChannel
0N/A implements Channel
0N/A{
0N/A
0N/A /**
0N/A * Initializes a new instance of this class.
0N/A */
0N/A protected SelectableChannel() { }
0N/A
0N/A /**
0N/A * Returns the provider that created this channel.
0N/A *
0N/A * @return The provider that created this channel
0N/A */
0N/A public abstract SelectorProvider provider();
0N/A
0N/A /**
0N/A * Returns an <a href="SelectionKey.html#opsets">operation set</a>
0N/A * identifying this channel's supported operations. The bits that are set
0N/A * in this integer value denote exactly the operations that are valid for
0N/A * this channel. This method always returns the same value for a given
0N/A * concrete channel class. </p>
0N/A *
0N/A * @return The valid-operation set
0N/A */
0N/A public abstract int validOps();
0N/A
0N/A // Internal state:
0N/A // keySet, may be empty but is never null, typ. a tiny array
0N/A // boolean isRegistered, protected by key set
0N/A // regLock, lock object to prevent duplicate registrations
0N/A // boolean isBlocking, protected by regLock
0N/A
0N/A /**
0N/A * Tells whether or not this channel is currently registered with any
0N/A * selectors. A newly-created channel is not registered.
0N/A *
0N/A * <p> Due to the inherent delay between key cancellation and channel
0N/A * deregistration, a channel may remain registered for some time after all
0N/A * of its keys have been cancelled. A channel may also remain registered
0N/A * for some time after it is closed. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this channel is registered
0N/A */
0N/A public abstract boolean isRegistered();
0N/A //
0N/A // sync(keySet) { return isRegistered; }
0N/A
0N/A /**
0N/A * Retrieves the key representing the channel's registration with the given
0N/A * selector. </p>
0N/A *
0N/A * @return The key returned when this channel was last registered with the
0N/A * given selector, or <tt>null</tt> if this channel is not
0N/A * currently registered with that selector
0N/A */
0N/A public abstract SelectionKey keyFor(Selector sel);
0N/A //
0N/A // sync(keySet) { return findKey(sel); }
0N/A
0N/A /**
0N/A * Registers this channel with the given selector, returning a selection
0N/A * key.
0N/A *
0N/A * <p> If this channel is currently registered with the given selector then
0N/A * the selection key representing that registration is returned. The key's
0N/A * interest set will have been changed to <tt>ops</tt>, as if by invoking
0N/A * the {@link SelectionKey#interestOps(int) interestOps(int)} method. If
0N/A * the <tt>att</tt> argument is not <tt>null</tt> then the key's attachment
0N/A * will have been set to that value. A {@link CancelledKeyException} will
0N/A * be thrown if the key has already been cancelled.
0N/A *
0N/A * <p> Otherwise this channel has not yet been registered with the given
0N/A * selector, so it is registered and the resulting new key is returned.
0N/A * The key's initial interest set will be <tt>ops</tt> and its attachment
0N/A * will be <tt>att</tt>.
0N/A *
0N/A * <p> This method may be invoked at any time. If this method is invoked
0N/A * while another invocation of this method or of the {@link
0N/A * #configureBlocking(boolean) configureBlocking} method is in progress
0N/A * then it will first block until the other operation is complete. This
0N/A * method will then synchronize on the selector's key set and therefore may
0N/A * block if invoked concurrently with another registration or selection
0N/A * operation involving the same selector. </p>
0N/A *
0N/A * <p> If this channel is closed while this operation is in progress then
0N/A * the key returned by this method will have been cancelled and will
0N/A * therefore be invalid. </p>
0N/A *
0N/A * @param sel
0N/A * The selector with which this channel is to be registered
0N/A *
0N/A * @param ops
0N/A * The interest set for the resulting key
0N/A *
0N/A * @param att
0N/A * The attachment for the resulting key; may be <tt>null</tt>
0N/A *
0N/A * @throws ClosedChannelException
0N/A * If this channel is closed
0N/A *
673N/A * @throws ClosedSelectorException
673N/A * If the selector is closed
673N/A *
0N/A * @throws IllegalBlockingModeException
0N/A * If this channel is in blocking mode
0N/A *
0N/A * @throws IllegalSelectorException
0N/A * If this channel was not created by the same provider
0N/A * as the given selector
0N/A *
0N/A * @throws CancelledKeyException
0N/A * If this channel is currently registered with the given selector
0N/A * but the corresponding key has already been cancelled
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If a bit in the <tt>ops</tt> set does not correspond to an
0N/A * operation that is supported by this channel, that is, if
0N/A * <tt>set & ~validOps() != 0</tt>
0N/A *
0N/A * @return A key representing the registration of this channel with
0N/A * the given selector
0N/A */
0N/A public abstract SelectionKey register(Selector sel, int ops, Object att)
0N/A throws ClosedChannelException;
0N/A //
0N/A // sync(regLock) {
0N/A // sync(keySet) { look for selector }
0N/A // if (channel found) { set interest ops -- may block in selector;
0N/A // return key; }
0N/A // create new key -- may block somewhere in selector;
0N/A // sync(keySet) { add key; }
0N/A // attach(attachment);
0N/A // return key;
0N/A // }
0N/A
0N/A /**
0N/A * Registers this channel with the given selector, returning a selection
0N/A * key.
0N/A *
0N/A * <p> An invocation of this convenience method of the form
0N/A *
0N/A * <blockquote><tt>sc.register(sel, ops)</tt></blockquote>
0N/A *
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <blockquote><tt>sc.{@link
0N/A * #register(java.nio.channels.Selector,int,java.lang.Object)
0N/A * register}(sel, ops, null)</tt></blockquote>
0N/A *
0N/A * @param sel
0N/A * The selector with which this channel is to be registered
0N/A *
0N/A * @param ops
0N/A * The interest set for the resulting key
0N/A *
0N/A * @throws ClosedChannelException
0N/A * If this channel is closed
0N/A *
673N/A * @throws ClosedSelectorException
673N/A * If the selector is closed
673N/A *
0N/A * @throws IllegalBlockingModeException
0N/A * If this channel is in blocking mode
0N/A *
0N/A * @throws IllegalSelectorException
0N/A * If this channel was not created by the same provider
0N/A * as the given selector
0N/A *
0N/A * @throws CancelledKeyException
0N/A * If this channel is currently registered with the given selector
0N/A * but the corresponding key has already been cancelled
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If a bit in <tt>ops</tt> does not correspond to an operation
0N/A * that is supported by this channel, that is, if <tt>set &
0N/A * ~validOps() != 0</tt>
0N/A *
0N/A * @return A key representing the registration of this channel with
0N/A * the given selector
0N/A */
0N/A public final SelectionKey register(Selector sel, int ops)
0N/A throws ClosedChannelException
0N/A {
0N/A return register(sel, ops, null);
0N/A }
0N/A
0N/A /**
0N/A * Adjusts this channel's blocking mode.
0N/A *
0N/A * <p> If this channel is registered with one or more selectors then an
0N/A * attempt to place it into blocking mode will cause an {@link
0N/A * IllegalBlockingModeException} to be thrown.
0N/A *
0N/A * <p> This method may be invoked at any time. The new blocking mode will
0N/A * only affect I/O operations that are initiated after this method returns.
0N/A * For some implementations this may require blocking until all pending I/O
0N/A * operations are complete.
0N/A *
0N/A * <p> If this method is invoked while another invocation of this method or
0N/A * of the {@link #register(Selector, int) register} method is in progress
0N/A * then it will first block until the other operation is complete. </p>
0N/A *
0N/A * @param block If <tt>true</tt> then this channel will be placed in
0N/A * blocking mode; if <tt>false</tt> then it will be placed
0N/A * non-blocking mode
0N/A *
0N/A * @return This selectable channel
0N/A *
0N/A * @throws ClosedChannelException
0N/A * If this channel is closed
0N/A *
0N/A * @throws IllegalBlockingModeException
0N/A * If <tt>block</tt> is <tt>true</tt> and this channel is
0N/A * registered with one or more selectors
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A */
0N/A public abstract SelectableChannel configureBlocking(boolean block)
0N/A throws IOException;
0N/A //
0N/A // sync(regLock) {
0N/A // sync(keySet) { throw IBME if block && isRegistered; }
0N/A // change mode;
0N/A // }
0N/A
0N/A /**
0N/A * Tells whether or not every I/O operation on this channel will block
0N/A * until it completes. A newly-created channel is always in blocking mode.
0N/A *
0N/A * <p> If this channel is closed then the value returned by this method is
0N/A * not specified. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this channel is in blocking mode
0N/A */
0N/A public abstract boolean isBlocking();
0N/A
0N/A /**
0N/A * Retrieves the object upon which the {@link #configureBlocking
0N/A * configureBlocking} and {@link #register register} methods synchronize.
0N/A * This is often useful in the implementation of adaptors that require a
0N/A * specific blocking mode to be maintained for a short period of time.
0N/A * </p>
0N/A *
0N/A * @return The blocking-mode lock object
0N/A */
0N/A public abstract Object blockingLock();
0N/A
0N/A}