Source for java.nio.channels.SelectionKey

   1: /* SelectionKey.java --
   2:    Copyright (C) 2002, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package java.nio.channels;
  39: 
  40: 
  41: /**
  42:  * @author Michael Koch
  43:  * @since 1.4
  44:  */
  45: public abstract class SelectionKey
  46: {
  47:   public static final int OP_ACCEPT = 16;
  48:   public static final int OP_CONNECT = 8;
  49:   public static final int OP_READ = 1;
  50:   public static final int OP_WRITE = 4;
  51:   Object attached;
  52: 
  53:   /**
  54:    * Initializes the selection key.
  55:    */
  56:   protected SelectionKey()
  57:   {
  58:   }
  59: 
  60:   /**
  61:    * Attaches obj to the key and returns the old attached object.
  62:    */
  63:   public final synchronized Object attach(Object obj)
  64:   {
  65:     Object old = attached;
  66:     attached = obj;
  67:     return old;
  68:   }
  69: 
  70:   /**
  71:    * Returns the object attached to the key.
  72:    */
  73:   public final synchronized Object attachment()
  74:   {
  75:     return attached;
  76:   }
  77: 
  78:   /**
  79:    * Tests if the channel attached to this key is ready to accept
  80:    * a new socket connection.
  81:    *
  82:    * @exception CancelledKeyException If this key has been cancelled
  83:    */
  84:   public final boolean isAcceptable()
  85:   {
  86:     return (readyOps() & OP_ACCEPT) != 0;
  87:   }
  88: 
  89:   /**
  90:    * Tests whether this key's channel has either finished,
  91:    * or failed to finish, its socket-connection operation.
  92:    *
  93:    * @exception CancelledKeyException If this key has been cancelled
  94:    */
  95:   public final boolean isConnectable()
  96:   {
  97:     return (readyOps() & OP_CONNECT) != 0;
  98:   }
  99: 
 100:   /**
 101:    * Tests if the channel attached to the key is readable.
 102:    *
 103:    * @exception CancelledKeyException If this key has been cancelled
 104:    */
 105:   public final boolean isReadable()
 106:   {
 107:     return (readyOps() & OP_READ) != 0;
 108:   }
 109: 
 110:   /**
 111:    * Tests if the channel attached to the key is writable.
 112:    *
 113:    * @exception CancelledKeyException If this key has been cancelled
 114:    */
 115:   public final boolean isWritable()
 116:   {
 117:     return (readyOps() & OP_WRITE) != 0;
 118:   }
 119: 
 120:   /**
 121:    * Requests that the registration of this key's channel with
 122:    * its selector be cancelled.
 123:    */
 124:   public abstract void cancel();
 125: 
 126:   /**
 127:    * return the channel attached to the key.
 128:    */
 129:   public abstract SelectableChannel channel();
 130: 
 131:   /**
 132:    * Returns the key's interest set.
 133:    *
 134:    * @exception CancelledKeyException If this key has been cancelled
 135:    */
 136:   public abstract int interestOps();
 137: 
 138:   /**
 139:    * Sets this key's interest set to the given value.
 140:    *
 141:    * @exception CancelledKeyException If this key has been cancelled
 142:    * @exception IllegalArgumentException If a bit in the set does not
 143:    * correspond to an operation that is supported by this key's channel,
 144:    * that is, if set & ~(channel().validOps()) != 0
 145:    */
 146:   public abstract SelectionKey interestOps(int ops);
 147: 
 148:   /**
 149:    * Tells whether or not this key is valid.
 150:    */
 151:   public abstract boolean isValid();
 152: 
 153:   /**
 154:    * Retrieves this key's ready-operation set.
 155:    *
 156:    * @exception CancelledKeyException If this key has been cancelled
 157:    */
 158:   public abstract int readyOps();
 159: 
 160:   /**
 161:    * Returns the selector for which this key was created.
 162:    */
 163:   public abstract Selector selector();
 164: }