Source Code Cross Referenced for SelectableChannel.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2000-2001 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.nio.channels;
027
028        import java.io.IOException;
029        import java.nio.channels.spi.AbstractInterruptibleChannel;
030        import java.nio.channels.spi.SelectorProvider;
031
032        /**
033         * A channel that can be multiplexed via a {@link Selector}.
034         *
035         * <p> In order to be used with a selector, an instance of this class must
036         * first be <i>registered</i> via the {@link #register(Selector,int,Object)
037         * register} method.  This method returns a new {@link SelectionKey} object
038         * that represents the channel's registration with the selector.
039         *
040         * <p> Once registered with a selector, a channel remains registered until it
041         * is <i>deregistered</i>.  This involves deallocating whatever resources were
042         * allocated to the channel by the selector.
043         *
044         * <p> A channel cannot be deregistered directly; instead, the key representing
045         * its registration must be <i>cancelled</i>.  Cancelling a key requests that
046         * the channel be deregistered during the selector's next selection operation.
047         * A key may be cancelled explicitly by invoking its {@link
048         * SelectionKey#cancel() cancel} method.  All of a channel's keys are cancelled
049         * implicitly when the channel is closed, whether by invoking its {@link
050         * Channel#close close} method or by interrupting a thread blocked in an I/O
051         * operation upon the channel.
052         *
053         * <p> If the selector itself is closed then the channel will be deregistered,
054         * and the key representing its registration will be invalidated, without
055         * further delay.
056         *
057         * <p> A channel may be registered at most once with any particular selector.
058         *
059         * <p> Whether or not a channel is registered with one or more selectors may be
060         * determined by invoking the {@link #isRegistered isRegistered} method.
061         *
062         * <p> Selectable channels are safe for use by multiple concurrent
063         * threads. </p>
064         *
065         *
066         * <a name="bm">
067         * <h4>Blocking mode</h4>
068         *
069         * A selectable channel is either in <i>blocking</i> mode or in
070         * <i>non-blocking</i> mode.  In blocking mode, every I/O operation invoked
071         * upon the channel will block until it completes.  In non-blocking mode an I/O
072         * operation will never block and may transfer fewer bytes than were requested
073         * or possibly no bytes at all.  The blocking mode of a selectable channel may
074         * be determined by invoking its {@link #isBlocking isBlocking} method.
075         *
076         * <p> Newly-created selectable channels are always in blocking mode.
077         * Non-blocking mode is most useful in conjunction with selector-based
078         * multiplexing.  A channel must be placed into non-blocking mode before being
079         * registered with a selector, and may not be returned to blocking mode until
080         * it has been deregistered.
081         *
082         *
083         * @author Mark Reinhold
084         * @author JSR-51 Expert Group
085         * @version 1.41, 07/05/05
086         * @since 1.4
087         *
088         * @see SelectionKey
089         * @see Selector
090         */
091
092        public abstract class SelectableChannel extends
093                AbstractInterruptibleChannel implements  Channel {
094
095            /**
096             * Initializes a new instance of this class.
097             */
098            protected SelectableChannel() {
099            }
100
101            /**
102             * Returns the provider that created this channel.
103             *
104             * @return  The provider that created this channel
105             */
106            public abstract SelectorProvider provider();
107
108            /**
109             * Returns an <a href="SelectionKey.html#opsets">operation set</a>
110             * identifying this channel's supported operations.  The bits that are set
111             * in this integer value denote exactly the operations that are valid for
112             * this channel.  This method always returns the same value for a given
113             * concrete channel class. </p>
114             *
115             * @return  The valid-operation set
116             */
117            public abstract int validOps();
118
119            // Internal state:
120            //   keySet, may be empty but is never null, typ. a tiny array
121            //   boolean isRegistered, protected by key set
122            //   regLock, lock object to prevent duplicate registrations
123            //   boolean isBlocking, protected by regLock
124
125            /**
126             * Tells whether or not this channel is currently registered with any
127             * selectors.  A newly-created channel is not registered.
128             *
129             * <p> Due to the inherent delay between key cancellation and channel
130             * deregistration, a channel may remain registered for some time after all
131             * of its keys have been cancelled.  A channel may also remain registered
132             * for some time after it is closed.  </p>
133             *
134             * @return <tt>true</tt> if, and only if, this channel is registered
135             */
136            public abstract boolean isRegistered();
137
138            //
139            // sync(keySet) { return isRegistered; }
140
141            /**
142             * Retrieves the key representing the channel's registration with the given
143             * selector.  </p>
144             *
145             * @return  The key returned when this channel was last registered with the
146             *          given selector, or <tt>null</tt> if this channel is not
147             *          currently registered with that selector
148             */
149            public abstract SelectionKey keyFor(Selector sel);
150
151            //
152            // sync(keySet) { return findKey(sel); }
153
154            /**
155             * Registers this channel with the given selector, returning a selection
156             * key.
157             *
158             * <p> If this channel is currently registered with the given selector then
159             * the selection key representing that registration is returned.  The key's
160             * interest set will have been changed to <tt>ops</tt>, as if by invoking
161             * the {@link SelectionKey#interestOps(int) interestOps(int)} method.  If
162             * the <tt>att</tt> argument is not <tt>null</tt> then the key's attachment
163             * will have been set to that value.  A {@link CancelledKeyException} will
164             * be thrown if the key has already been cancelled.
165             *
166             * <p> Otherwise this channel has not yet been registered with the given
167             * selector, so it is registered and the resulting new key is returned.
168             * The key's initial interest set will be <tt>ops</tt> and its attachment
169             * will be <tt>att</tt>.
170             *
171             * <p> This method may be invoked at any time.  If this method is invoked
172             * while another invocation of this method or of the {@link
173             * #configureBlocking(boolean) configureBlocking} method is in progress
174             * then it will first block until the other operation is complete.  This
175             * method will then synchronize on the selector's key set and therefore may
176             * block if invoked concurrently with another registration or selection
177             * operation involving the same selector. </p>
178             *
179             * <p> If this channel is closed while this operation is in progress then
180             * the key returned by this method will have been cancelled and will
181             * therefore be invalid. </p>
182             *
183             * @param  sel
184             *         The selector with which this channel is to be registered
185             *
186             * @param  ops
187             *         The interest set for the resulting key
188             *
189             * @param  att
190             *         The attachment for the resulting key; may be <tt>null</tt>
191             *
192             * @throws  ClosedChannelException
193             *          If this channel is closed
194             *
195             * @throws  IllegalBlockingModeException
196             *          If this channel is in blocking mode
197             *
198             * @throws  IllegalSelectorException
199             *          If this channel was not created by the same provider
200             *          as the given selector
201             *
202             * @throws  CancelledKeyException
203             *          If this channel is currently registered with the given selector
204             *          but the corresponding key has already been cancelled
205             *
206             * @throws  IllegalArgumentException
207             *          If a bit in the <tt>ops</tt> set does not correspond to an
208             *          operation that is supported by this channel, that is, if
209             *          <tt>set & ~validOps() != 0</tt>
210             *
211             * @return  A key representing the registration of this channel with
212             *          the given selector
213             */
214            public abstract SelectionKey register(Selector sel, int ops,
215                    Object att) throws ClosedChannelException;
216
217            //
218            // sync(regLock) {
219            //   sync(keySet) { look for selector }
220            //   if (channel found) { set interest ops -- may block in selector;
221            //                        return key; }
222            //   create new key -- may block somewhere in selector;
223            //   sync(keySet) { add key; }
224            //   attach(attachment);
225            //   return key;
226            // }
227
228            /**
229             * Registers this channel with the given selector, returning a selection
230             * key.
231             *
232             * <p> An invocation of this convenience method of the form
233             *
234             * <blockquote><tt>sc.register(sel, ops)</tt></blockquote>
235             *
236             * behaves in exactly the same way as the invocation
237             *
238             * <blockquote><tt>sc.{@link
239             * #register(java.nio.channels.Selector,int,java.lang.Object)
240             * register}(sel, ops, null)</tt></blockquote>
241             * 
242             * @param  sel
243             *         The selector with which this channel is to be registered
244             *
245             * @param  ops
246             *         The interest set for the resulting key
247             *
248             * @throws  ClosedChannelException
249             *          If this channel is closed
250             *
251             * @throws  IllegalBlockingModeException
252             *          If this channel is in blocking mode
253             *
254             * @throws  IllegalSelectorException
255             *          If this channel was not created by the same provider
256             *          as the given selector
257             *
258             * @throws  CancelledKeyException
259             *          If this channel is currently registered with the given selector
260             *          but the corresponding key has already been cancelled
261             *
262             * @throws  IllegalArgumentException
263             *          If a bit in <tt>ops</tt> does not correspond to an operation
264             *          that is supported by this channel, that is, if <tt>set &
265             *          ~validOps() != 0</tt>
266             *
267             * @return  A key representing the registration of this channel with
268             *          the given selector
269             */
270            public final SelectionKey register(Selector sel, int ops)
271                    throws ClosedChannelException {
272                return register(sel, ops, null);
273            }
274
275            /**
276             * Adjusts this channel's blocking mode.
277             *
278             * <p> If this channel is registered with one or more selectors then an
279             * attempt to place it into blocking mode will cause an {@link
280             * IllegalBlockingModeException} to be thrown.
281             *
282             * <p> This method may be invoked at any time.  The new blocking mode will
283             * only affect I/O operations that are initiated after this method returns.
284             * For some implementations this may require blocking until all pending I/O
285             * operations are complete.
286             *
287             * <p> If this method is invoked while another invocation of this method or
288             * of the {@link #register(Selector, int) register} method is in progress
289             * then it will first block until the other operation is complete. </p>
290             *
291             * @param  block  If <tt>true</tt> then this channel will be placed in
292             *                blocking mode; if <tt>false</tt> then it will be placed
293             *                non-blocking mode
294             *
295             * @return  This selectable channel
296             *
297             * @throws  ClosedChannelException
298             *          If this channel is closed
299             *
300             * @throws  IllegalBlockingModeException
301             *          If <tt>block</tt> is <tt>true</tt> and this channel is
302             *          registered with one or more selectors
303             *
304             * @throws IOException
305             *         If an I/O error occurs
306             */
307            public abstract SelectableChannel configureBlocking(boolean block)
308                    throws IOException;
309
310            //
311            // sync(regLock) {
312            //   sync(keySet) { throw IBME if block && isRegistered; }
313            //   change mode;
314            // }
315
316            /**
317             * Tells whether or not every I/O operation on this channel will block
318             * until it completes.  A newly-created channel is always in blocking mode.
319             *
320             * <p> If this channel is closed then the value returned by this method is
321             * not specified. </p>
322             *
323             * @return <tt>true</tt> if, and only if, this channel is in blocking mode
324             */
325            public abstract boolean isBlocking();
326
327            /**
328             * Retrieves the object upon which the {@link #configureBlocking
329             * configureBlocking} and {@link #register register} methods synchronize.
330             * This is often useful in the implementation of adaptors that require a
331             * specific blocking mode to be maintained for a short period of time.
332             * </p>
333             *
334             * @return  The blocking-mode lock object
335             */
336            public abstract Object blockingLock();
337
338        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.