1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.ch; 27 28 import java.nio.channels.CancelledKeyException; 29 import java.nio.channels.SelectableChannel; 30 import java.nio.channels.SelectionKey; 31 import java.nio.channels.Selector; 32 import java.nio.channels.spi.AbstractSelectionKey; 33 34 35 /** 36 * An implementation of SelectionKey. 37 */ 38 39 public final class SelectionKeyImpl 40 extends AbstractSelectionKey 41 { 42 43 final SelChImpl channel; // package-private 44 public final SelectorImpl selector; 45 46 // Index for a pollfd array in Selector that this key is registered with 47 private int index; 48 49 private volatile int interestOps; 50 private volatile int readyOps; 51 52 SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) { 53 channel = ch; 54 selector = sel; 55 } 56 57 @Override 58 public SelectableChannel channel() { 59 return (SelectableChannel)channel; 60 } 61 62 @Override 63 public Selector selector() { 64 return (Selector)selector; 65 } 66 67 int getIndex() { // package-private 68 return index; 69 } 70 71 void setIndex(int i) { // package-private 72 index = i; 73 } 74 75 private void ensureValid() { 76 if (!isValid()) 77 throw new CancelledKeyException(); 78 } 79 80 @Override 81 public int interestOps() { 82 ensureValid(); 83 return interestOps; 84 } 85 86 @Override 87 public SelectionKey interestOps(int ops) { 88 ensureValid(); 89 return nioInterestOps(ops); 90 } 91 92 @Override 93 public int readyOps() { 94 ensureValid(); 95 return readyOps; 96 } 97 98 // The nio versions of these operations do not care if a key 99 // has been invalidated. They are for internal use by nio code. 100 101 public void nioReadyOps(int ops) { 102 readyOps = ops; 103 } 104 105 public int nioReadyOps() { 106 return readyOps; 107 } 108 109 public SelectionKey nioInterestOps(int ops) { 110 if ((ops & ~channel().validOps()) != 0) 111 throw new IllegalArgumentException(); 112 channel.translateAndSetInterestOps(ops, this); 113 interestOps = ops; 114 return this; 115 } 116 117 public int nioInterestOps() { 118 return interestOps; 119 } 120 121 @Override 122 public String toString() { 123 StringBuilder sb = new StringBuilder(); 124 sb.append("channel=") 125 .append(channel) 126 .append(", selector=") 127 .append(selector); 128 if (isValid()) { 129 sb.append(", interestOps=") 130 .append(interestOps) 131 .append(", readyOps=") 132 .append(readyOps); 133 } else { 134 sb.append(", invalid"); 135 } 136 return sb.toString(); 137 } 138 139 // used by Selector implementations to record when the key was selected 140 int lastPolled; 141 }