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 for Solaris. 37 */ 38 39 public 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 public SelectableChannel channel() { 58 return (SelectableChannel)channel; 59 } 60 61 public Selector selector() { 62 return selector; 63 } 64 65 int getIndex() { // package-private 66 return index; 67 } 68 69 void setIndex(int i) { // package-private 70 index = i; 71 } 72 73 private void ensureValid() { 74 if (!isValid()) 75 throw new CancelledKeyException(); 76 } 77 78 public int interestOps() { 79 ensureValid(); 80 return interestOps; 81 } 82 83 public SelectionKey interestOps(int ops) { 84 ensureValid(); 85 return nioInterestOps(ops); 86 } 87 88 public int readyOps() { 89 ensureValid(); 90 return readyOps; 91 } 92 93 // The nio versions of these operations do not care if a key 94 // has been invalidated. They are for internal use by nio code. 95 96 public void nioReadyOps(int ops) { 97 readyOps = ops; 98 } 99 100 public int nioReadyOps() { 101 return readyOps; 102 } 103 104 public SelectionKey nioInterestOps(int ops) { 105 if ((ops & ~channel().validOps()) != 0) 106 throw new IllegalArgumentException(); 107 channel.translateAndSetInterestOps(ops, this); 108 interestOps = ops; 109 return this; 110 } 111 112 public int nioInterestOps() { 113 return interestOps; 114 } 115 116 @Override 117 public String toString() { 118 StringBuilder sb = new StringBuilder(); 119 sb.append("channel=") 120 .append(channel) 121 .append(", selector=") 122 .append(selector); 123 if (isValid()) { 124 sb.append(", interestOps=") 125 .append(interestOps) 126 .append(", readyOps=") 127 .append(readyOps); 128 } else { 129 sb.append(", invalid"); 130 } 131 return sb.toString(); 132 } 133 134 }