Methods Summary |
---|
private void | closeCheck()
if (!isOpen()) {
throw new ClosedSelectorException();
}
|
private void | doCancel()
Set<SelectionKey> cancelledKeys = cancelledKeys();
synchronized (cancelledKeys) {
if (cancelledKeys.size() > 0) {
for (SelectionKey currentkey : cancelledKeys) {
deregister((AbstractSelectionKey) currentkey);
keys.remove(currentkey);
selectedKeys.remove(currentkey);
}
}
cancelledKeys.clear();
}
|
protected void | implCloseSelector()
doCancel();
for (SelectionKey sk : keys) {
deregister((AbstractSelectionKey) sk);
}
wakeup();
|
private boolean | isConnected(SelectionKeyImpl key)
SelectableChannel channel = key.channel();
if (channel instanceof SocketChannel) {
return ((SocketChannel) channel).isConnected();
}
return true;
|
public synchronized java.util.Set | keys()
closeCheck();
return unmodifiableKeys;
|
private void | prepareChannels()
readableFDs.add(sourcefd);
List<SelectionKey> readChannelList = new ArrayList<SelectionKey>();
readChannelList.add(source.keyFor(this));
List<SelectionKey> writeChannelList = new ArrayList<SelectionKey>();
synchronized (keysLock) {
for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
SelectionKeyImpl key = (SelectionKeyImpl) i.next();
key.oldInterestOps = key.interestOps();
boolean isReadableChannel = ((SelectionKey.OP_ACCEPT | SelectionKey.OP_READ) & key.oldInterestOps) != 0;
boolean isWritableChannel = ((SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE) & key.oldInterestOps) != 0;
SelectableChannel channel = key.channel();
if (isReadableChannel) {
readChannelList.add(channel.keyFor(this));
readableFDs.add(((FileDescriptorHandler)channel).getFD());
}
if (isWritableChannel) {
writeChannelList.add(channel.keyFor(this));
writableFDs.add(((FileDescriptorHandler)channel).getFD());
}
}
}
readableChannels = readChannelList.toArray(new SelectionKey[0]);
writableChannels = writeChannelList.toArray(new SelectionKey[0]);
readable = readableFDs.toArray(new FileDescriptor[0]);
writable = writableFDs.toArray(new FileDescriptor[0]);
|
private int | processSelectResult(int[] readyChannels)
if (0 == readyChannels.length) {
return 0;
}
// if the mock channel is selected, read the content.
if (READABLE == readyChannels[0]) {
ByteBuffer readbuf = ByteBuffer.allocate(MOCK_READBUF_SIZE);
while (source.read(readbuf) > 0) {
readbuf.flip();
}
}
int selected = 0;
for (int i = 1; i < readyChannels.length; i++) {
SelectionKeyImpl key = (SelectionKeyImpl) (i >= readable.length ? writableChannels[i
- readable.length]
: readableChannels[i]);
if (null == key) {
continue;
}
boolean isOldSelectedKey = selectedKeys.contains(key);
int selectedOp = 0;
// set ready ops
switch (readyChannels[i]) {
case NA:
selectedOp = 0;
break;
case READABLE:
selectedOp = (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)
& key.oldInterestOps;
break;
case WRITEABLE:
if (isConnected(key)) {
selectedOp = SelectionKey.OP_WRITE & key.oldInterestOps;
} else {
selectedOp = SelectionKey.OP_CONNECT & key.oldInterestOps;
}
break;
}
if (0 != selectedOp) {
if (isOldSelectedKey && key.readyOps() != selectedOp) {
key.setReadyOps(key.readyOps() | selectedOp);
selected++;
} else if (!isOldSelectedKey) {
key.setReadyOps(selectedOp);
selectedKeys.add(key);
selected++;
}
}
}
readableChannels = null;
writableChannels = null;
return selected;
|
protected java.nio.channels.SelectionKey | register(java.nio.channels.spi.AbstractSelectableChannel channel, int operations, java.lang.Object attachment)
if (!provider().equals(channel.provider())) {
throw new IllegalSelectorException();
}
synchronized (this) {
synchronized (keys) {
SelectionKey sk = new SelectionKeyImpl(channel, operations,
attachment, this);
keys.add(sk);
return sk;
}
}
|
public int | select()
return selectInternal(SELECT_BLOCK);
|
public int | select(long timeout)
if (timeout < 0) {
throw new IllegalArgumentException();
}
return selectInternal((0 == timeout) ? SELECT_BLOCK : timeout);
|
private int | selectInternal(long timeout)
closeCheck();
synchronized (this) {
synchronized (keys) {
synchronized (selectedKeys) {
doCancel();
int[] readyChannels = null;
boolean isBlock = (SELECT_NOW != timeout);
// BEGIN android-removed
// copied from newer version of harmony
// if (keys.size() == 0) {
// return 0;
// }
// END android-removed
prepareChannels();
try {
if (isBlock) {
begin();
}
readyChannels = Platform.getNetworkSystem().select(readable, writable, timeout);
} finally {
// clear results for next select
readableFDs.clear();
writableFDs.clear();
if (isBlock) {
end();
}
}
return processSelectResult(readyChannels);
}
}
}
|
public int | selectNow()
return selectInternal(SELECT_NOW);
|
public synchronized java.util.Set | selectedKeys()
closeCheck();
return unaddableSelectedKeys;
|
public java.nio.channels.Selector | wakeup()
try {
sink.write(ByteBuffer.allocate(MOCK_WRITEBUF_SIZE));
} catch (IOException e) {
// do nothing
}
return this;
|