59 |
{ |
{ |
60 |
super (provider); |
super (provider); |
61 |
|
|
62 |
keys = new HashSet (); |
keys = new HashSet(); |
63 |
selected = new HashSet (); |
selected = new HashSet(); |
64 |
} |
} |
65 |
|
|
66 |
protected void finalize() throws Throwable |
protected void finalize() throws Throwable |
100 |
{ |
{ |
101 |
int[] result; |
int[] result; |
102 |
int counter = 0; |
int counter = 0; |
103 |
Iterator it = keys.iterator (); |
Iterator it = keys.iterator(); |
104 |
|
|
105 |
// Count the number of file descriptors needed |
// Count the number of file descriptors needed |
106 |
while (it.hasNext ()) |
while (it.hasNext()) |
107 |
{ |
{ |
108 |
SelectionKeyImpl key = (SelectionKeyImpl) it.next (); |
SelectionKeyImpl key = (SelectionKeyImpl) it.next(); |
109 |
|
|
110 |
if ((key.interestOps () & ops) != 0) |
if ((key.interestOps() & ops) != 0) |
111 |
{ |
{ |
112 |
counter++; |
counter++; |
113 |
} |
} |
114 |
} |
} |
115 |
|
|
116 |
result = new int[counter]; |
result = new int [counter]; |
117 |
|
|
118 |
counter = 0; |
counter = 0; |
119 |
it = keys.iterator (); |
it = keys.iterator(); |
120 |
|
|
121 |
// Fill the array with the file descriptors |
// Fill the array with the file descriptors |
122 |
while (it.hasNext ()) |
while (it.hasNext()) |
123 |
{ |
{ |
124 |
SelectionKeyImpl key = (SelectionKeyImpl) it.next (); |
SelectionKeyImpl key = (SelectionKeyImpl) it.next(); |
125 |
|
|
126 |
if ((key.interestOps () & ops) != 0) |
if ((key.interestOps() & ops) != 0) |
127 |
{ |
{ |
128 |
result[counter] = key.getNativeFD(); |
result [counter] = key.getNativeFD(); |
129 |
counter++; |
counter++; |
130 |
} |
} |
131 |
} |
} |
136 |
public int select (long timeout) |
public int select (long timeout) |
137 |
{ |
{ |
138 |
if (!isOpen()) |
if (!isOpen()) |
139 |
throw new ClosedSelectorException (); |
throw new ClosedSelectorException(); |
140 |
|
|
141 |
if (keys == null) |
if (keys == null) |
142 |
{ |
{ |
143 |
return 0; |
return 0; |
144 |
} |
} |
145 |
|
|
146 |
deregisterCancelledKeys(); |
deregisterCancelledKeys(); |
147 |
|
|
156 |
int result = implSelect (read, write, except, timeout); |
int result = implSelect (read, write, except, timeout); |
157 |
end(); |
end(); |
158 |
|
|
159 |
Iterator it = keys.iterator (); |
Iterator it = keys.iterator(); |
160 |
|
|
161 |
while (it.hasNext ()) |
while (it.hasNext()) |
162 |
{ |
{ |
163 |
int ops = 0; |
int ops = 0; |
164 |
SelectionKeyImpl key = (SelectionKeyImpl) it.next (); |
SelectionKeyImpl key = (SelectionKeyImpl) it.next(); |
165 |
|
|
166 |
// If key is already selected retrieve old ready ops. |
// If key is already selected retrieve old ready ops. |
167 |
if (selected.contains (key)) |
if (selected.contains (key)) |
168 |
{ |
{ |
169 |
ops = key.readyOps (); |
ops = key.readyOps(); |
170 |
} |
} |
171 |
|
|
172 |
// Set new ready read/accept ops |
// Set new ready read/accept ops |
173 |
for (int i = 0; i < read.length; i++) |
for (int i = 0; i < read.length; i++) |
174 |
{ |
{ |
175 |
if (key.getNativeFD() == read[i]) |
if (key.getNativeFD() == read [i]) |
176 |
{ |
{ |
177 |
if (key.channel () instanceof ServerSocketChannelImpl) |
if (key.channel() instanceof ServerSocketChannelImpl) |
178 |
{ |
{ |
179 |
ops = ops | SelectionKey.OP_ACCEPT; |
ops = ops | SelectionKey.OP_ACCEPT; |
180 |
} |
} |
188 |
// Set new ready write ops |
// Set new ready write ops |
189 |
for (int i = 0; i < write.length; i++) |
for (int i = 0; i < write.length; i++) |
190 |
{ |
{ |
191 |
if (key.getNativeFD() == write[i]) |
if (key.getNativeFD() == write [i]) |
192 |
{ |
{ |
193 |
ops = ops | SelectionKey.OP_WRITE; |
ops = ops | SelectionKey.OP_WRITE; |
194 |
|
|
195 |
// if (key.channel ().isConnected ()) |
// if (key.channel().isConnected()) |
196 |
// { |
// { |
197 |
// ops = ops | SelectionKey.OP_WRITE; |
// ops = ops | SelectionKey.OP_WRITE; |
198 |
// } |
// } |
212 |
} |
} |
213 |
|
|
214 |
// Set new ready ops |
// Set new ready ops |
215 |
key.readyOps (key.interestOps () & ops); |
key.readyOps (key.interestOps() & ops); |
216 |
} |
} |
217 |
|
|
218 |
deregisterCancelledKeys(); |
deregisterCancelledKeys(); |
233 |
{ |
{ |
234 |
Iterator it = cancelledKeys().iterator(); |
Iterator it = cancelledKeys().iterator(); |
235 |
|
|
236 |
while (it.hasNext ()) |
while (it.hasNext()) |
237 |
{ |
{ |
238 |
keys.remove ((SelectionKeyImpl) it.next ()); |
keys.remove ((SelectionKeyImpl) it.next()); |
239 |
it.remove (); |
it.remove(); |
240 |
} |
} |
241 |
} |
} |
242 |
|
|