| 15 | import java.net.Socket; |
| 16 | |
| 17 | class SocketSelector extends Selector { |
| 18 | protected volatile long state; |
| 19 | protected final Object lock = new Object(); |
| 20 | protected boolean woken = false; |
| 21 | |
| 22 | public SocketSelector() throws IOException { |
| 23 | Socket.init(); |
| 24 | |
| 25 | state = natInit(); |
| 26 | } |
| 27 | |
| 28 | public boolean isOpen() { |
| 29 | return state != 0; |
| 30 | } |
| 31 | |
| 32 | public Selector wakeup() { |
| 33 | synchronized (lock) { |
| 34 | if (isOpen() && (! woken)) { |
| 35 | woken = true; |
| 36 | |
| 37 | natWakeup(state); |
| 38 | } |
| 39 | } |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | private boolean clearWoken() { |
| 44 | synchronized (lock) { |
| 45 | if (woken) { |
| 46 | woken = false; |
| 47 | return true; |
| 48 | } else { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public synchronized int selectNow() throws IOException { |
| 55 | return doSelect(-1); |
| 56 | } |
| 57 | |
| 58 | public synchronized int select() throws IOException { |
| 59 | return doSelect(0); |
| 60 | } |
| 61 | |
| 62 | public synchronized int select(long interval) throws IOException { |
| 63 | if (interval < 0) throw new IllegalArgumentException(); |
| 64 | |
| 65 | return doSelect(interval); |
| 66 | } |
| 67 | |
| 68 | public int doSelect(long interval) throws IOException { |
| 69 | if (! isOpen()) { |
| 70 | throw new ClosedSelectorException(); |
| 71 | } |
| 72 | |
| 73 | selectedKeys.clear(); |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected