if (!isOpen()) {
throw new ClosedChannelException();
}
if (!isBound) {
throw new NotYetBoundException();
}
SocketChannel sockChannel = SocketChannel.open();
Socket socketGot = sockChannel.socket();
try {
begin();
synchronized (acceptLock) {
synchronized (blockingLock()) {
boolean isBlocking = isBlocking();
if (!isBlocking) {
// for non blocking mode, use select to see whether
// there are any pending connections.
int[] tryResult = Platform.getNetworkSystem().select(
new FileDescriptor[] { this.fd },
new FileDescriptor[0], 0);
if (0 == tryResult.length || 0 == tryResult[0]) {
// no pending connections, returns immediately.
return null;
}
}
// do accept.
do {
try {
((ServerSocketAdapter) socket).accept(socketGot,
(SocketChannelImpl) sockChannel);
// select successfully, break out immediately.
break;
} catch (SocketTimeoutException e) {
// continue to accept if the channel is in blocking
// mode.
}
} while (isBlocking);
}
}
} finally {
end(socketGot.isConnected());
}
return sockChannel;