2004-01-07 Michael Koch <konqueror@gmx.de>
[gcc.git] / libjava / java / nio / channels / spi / AbstractSelectableChannel.java
1 /* AbstractSelectableChannel.java
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
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
24 combination.
25
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. */
37
38 package java.nio.channels.spi;
39
40 import java.io.IOException;
41 import java.nio.channels.ClosedChannelException;
42 import java.nio.channels.SelectableChannel;
43 import java.nio.channels.SelectionKey;
44 import java.nio.channels.Selector;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.ListIterator;
48
49 public abstract class AbstractSelectableChannel extends SelectableChannel
50 {
51 private int registered;
52 private boolean blocking = true;
53 private Object LOCK = new Object();
54 private SelectorProvider provider;
55 private LinkedList keys;
56
57 /**
58 * Initializes the channel
59 */
60 protected AbstractSelectableChannel (SelectorProvider provider)
61 {
62 this.provider = provider;
63 this.keys = new LinkedList();
64 }
65
66 /**
67 * Retrieves the object upon which the configureBlocking and register
68 * methods synchronize.
69 */
70 public final Object blockingLock ()
71 {
72 return LOCK;
73 }
74
75 /**
76 * Adjusts this channel's blocking mode.
77 */
78 public final SelectableChannel configureBlocking (boolean blocking)
79 throws IOException
80 {
81 synchronized (blockingLock())
82 {
83 implConfigureBlocking(blocking);
84 this.blocking = blocking;
85 }
86
87 return this;
88 }
89
90 /**
91 * Closes this channel.
92 *
93 * @exception IOException If an error occurs
94 */
95 protected final void implCloseChannel () throws IOException
96 {
97 implCloseSelectableChannel ();
98 }
99
100 /**
101 * Closes this selectable channel.
102 */
103 protected abstract void implCloseSelectableChannel () throws IOException;
104
105 /**
106 * Adjusts this channel's blocking mode.
107 */
108 protected abstract void implConfigureBlocking (boolean block)
109 throws IOException;
110
111 /**
112 * Tells whether or not every I/O operation on this channel will block
113 * until it completes.
114 */
115 public final boolean isBlocking()
116 {
117 return blocking;
118 }
119
120 /**
121 * Tells whether or not this channel is currently registered with
122 * any selectors.
123 */
124 public final boolean isRegistered()
125 {
126 return !keys.isEmpty();
127 }
128
129 /**
130 * Retrieves the key representing the channel's registration with the
131 * given selector.
132 */
133 public final SelectionKey keyFor(Selector selector)
134 {
135 try
136 {
137 return register (selector, 0, null);
138 }
139 catch (Exception e)
140 {
141 return null;
142 }
143 }
144
145 /**
146 * Returns the provider that created this channel.
147 */
148 public final SelectorProvider provider ()
149 {
150 return provider;
151 }
152
153 private SelectionKey locate (Selector selector)
154 {
155 if (keys == null)
156 return null;
157
158 ListIterator it = keys.listIterator ();
159
160 while (it.hasNext ())
161 {
162 SelectionKey key = (SelectionKey) it.next();
163
164 if (key.selector() == selector)
165 return key;
166 }
167
168 return null;
169 }
170
171 private void add (SelectionKey key)
172 {
173 keys.add (key);
174 }
175
176 /**
177 * Registers this channel with the given selector, returning a selection key.
178 *
179 * @exception ClosedChannelException If the channel is already closed.
180 */
181 public final SelectionKey register (Selector selin, int ops, Object att)
182 throws ClosedChannelException
183 {
184 if (!isOpen ())
185 throw new ClosedChannelException();
186
187 SelectionKey key = null;
188 AbstractSelector selector = (AbstractSelector) selin;
189
190 synchronized (blockingLock())
191 {
192 key = locate (selector);
193
194 if (key != null)
195 {
196 key.attach (att);
197 }
198 else
199 {
200 key = selector.register (this, ops, att);
201
202 if (key != null)
203 add (key);
204 }
205 }
206
207 return key;
208 }
209 }