1 /* AbstractSelectableChannel.java
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.nio.channels.spi;
41 import java.io.IOException;
42 import java.nio.channels.ClosedChannelException;
43 import java.nio.channels.SelectableChannel;
44 import java.nio.channels.SelectionKey;
45 import java.nio.channels.Selector;
46 import java.nio.channels.IllegalBlockingModeException;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 import java.util.ListIterator;
51 public abstract class AbstractSelectableChannel extends SelectableChannel
53 private boolean blocking = true;
54 private Object LOCK = new Object();
55 private SelectorProvider provider;
56 private LinkedList keys = new LinkedList();
59 * Initializes the channel
61 * @param provider the provider that created this channel
63 protected AbstractSelectableChannel(SelectorProvider provider)
65 this.provider = provider;
69 * Retrieves the object upon which the configureBlocking and register
70 * methods synchronize.
72 * @return the blocking lock
74 public final Object blockingLock()
80 * Adjusts this channel's blocking mode.
82 * @param blocking true if blocking should be enabled, false otherwise
84 * @return this channel
86 * @exception IOException If an error occurs
88 public final SelectableChannel configureBlocking(boolean blocking)
91 synchronized (blockingLock())
93 if (this.blocking != blocking)
95 implConfigureBlocking(blocking);
96 this.blocking = blocking;
104 * Closes this channel.
106 * @exception IOException If an error occurs
108 protected final void implCloseChannel() throws IOException
112 implCloseSelectableChannel();
116 for (Iterator it = keys.iterator(); it.hasNext(); )
117 ((SelectionKey) it.next()).cancel();
122 * Closes this selectable channel.
124 * @exception IOException If an error occurs
126 protected abstract void implCloseSelectableChannel()
130 * Adjusts this channel's blocking mode.
132 * @param blocking true if blocking should be enabled, false otherwise
134 * @exception IOException If an error occurs
136 protected abstract void implConfigureBlocking(boolean blocking)
140 * Tells whether or not every I/O operation on this channel will block
141 * until it completes.
143 * @return true of this channel is blocking, false otherwise
145 public final boolean isBlocking()
151 * Tells whether or not this channel is currently registered with
154 * @return true if this channel is registered, false otherwise
156 public final boolean isRegistered()
158 return ! keys.isEmpty();
162 * Retrieves the key representing the channel's registration with the
165 * @param selector the selector to get a selection key for
167 * @return the selection key this channel is registered with
169 public final SelectionKey keyFor(Selector selector)
176 synchronized (blockingLock())
178 return locate(selector);
188 * Returns the provider that created this channel.
190 * @return the selector provider that created this channel
192 public final SelectorProvider provider()
197 private SelectionKey locate(Selector selector)
199 ListIterator it = keys.listIterator();
203 SelectionKey key = (SelectionKey) it.next();
205 if (key.selector() == selector)
213 * Registers this channel with the given selector, returning a selection key.
215 * @param selin the seletor to use
216 * @param ops the interested operations
217 * @param att an attachment for the returned selection key
219 * @return the registered selection key
221 * @exception ClosedChannelException If the channel is already closed.
222 * @exception IllegalBlockingModeException If the channel is configured in
225 public final SelectionKey register(Selector selin, int ops, Object att)
226 throws ClosedChannelException
229 throw new ClosedChannelException();
231 if ((ops & ~validOps()) != 0)
232 throw new IllegalArgumentException();
234 SelectionKey key = null;
235 AbstractSelector selector = (AbstractSelector) selin;
237 synchronized (blockingLock())
240 throw new IllegalBlockingModeException();
242 key = locate(selector);
244 if (key != null && key.isValid())
246 key.interestOps(ops);
251 key = selector.register(this, ops, att);
254 addSelectionKey(key);
261 void addSelectionKey(SelectionKey key)
266 // This method gets called by AbstractSelector.deregister().
267 void removeSelectionKey(SelectionKey key)