NioSocket抽象类
1 public abstract class SelectableChannel extends AbstractChannel implements Channel 2 { 3 public abstract void configureBlocking (boolean block) throws IOException; 4 public abstract boolean isBlocking( ); 5 public abstract Object blockingLock( ); 6 } 7 8 public abstract class ServerSocketChannel extends AbstractSelectableChannel 9 { 10 public static ServerSocketChannel open( ) throws IOException 11 public abstract ServerSocket socket( ); 12 public abstract ServerSocket accept( ) throws IOException; 13 public final int validOps( ) 14 } 15 16 ------------------------------------------------------------------------------------------------------------- 17 18 public abstract class SocketChannel extends AbstractSelectableChannel 19 implements ByteChannel, ScatteringByteChannel, GatheringByteChannel 20 { 21 public static SocketChannel open( ) throws IOException 22 public static SocketChannel open (InetSocketAddress remote) 23 throws IOException 24 public abstract Socket socket( ); 25 public abstract boolean connect (SocketAddress remote) 26 throws IOException; 27 public abstract boolean isConnectionPending( ); 28 public abstract boolean finishConnect( ) throws IOException; 29 public abstract boolean isConnected( ); 30 public final int validOps( ) 31 } 32 33 --------------------------------------------------------------------------------------------------------------- 34 35 Create: 36 37 ServerSocketChannel.open() or SocketChannel.open() 38 39 SocketChannel.socket() -> Socket 40 Socket.getChannel() -> SocketChannel 41 42 虽然每个SocketChannel对象都会创建一个对等的Socket对象,反过来却不成 43 立。直接创建的Socket对象不会关联SocketChannel对象,它们的getChannel( ) 44 方法只返回null。 45 46 ---------------------------------------------------------------------------------------------------------------- 47 48 public abstract class DatagramChannel extends AbstractSelectableChannel 49 implements ByteChannel, ScatteringByteChannel, GatheringByteChannel 50 { 51 public static DatagramChannel open( ) throws IOException; 52 public abstract DatagramSocket socket( ); 53 public abstract DatagramChannel connect (SocketAddress remote) throws IOException; 54 public abstract boolean isConnected( ); 55 public abstract DatagramChannel disconnect( ) throws IOException; 56 public abstract SocketAddress receive (ByteBuffer dst) throws IOException; 57 public abstract int send (ByteBuffer src, SocketAddress target); 58 public abstract int read (ByteBuffer dst) throws IOException; 59 public abstract long read (ByteBuffer [] dsts) throws IOException; 60 public abstract long read (ByteBuffer [] dsts, int offset, int length) throws IOException; 61 public abstract int write (ByteBuffer src) throws IOException; 62 public abstract long write(ByteBuffer[] srcs) throws IOException; 63 public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException; 64 public abstract SocketAddress receive (ByteBuffer dst) throws IOException; 65 public abstract int send (ByteBuffer src, SocketAddress target); 66 } 67 68 ----------------------------------------------------------------------------------------------------------------- 69 70 channel.send (buffer, addr); 71 addr = receivePacket (channel, buffer); 72 this.channel.socket( ).bind (new InetSocketAddress (port)); 73 74 ----------------------------------------------------------------------------------------------------------------- 75 76 FileChannel fc = raf.getChannel( ); 77 FileLock lock = fc.lock (INDEX_START, INDEX_SIZE, false); 78 lock.release( ); 79 80 ----------------------------------------------------------------------------------------------------------------- 81 82 public abstract class SelectableChannel extends AbstractChannel implements Channel 83 { 84 public abstract SelectionKey register (Selector sel, int ops) throws ClosedChannelException; 85 public abstract SelectionKey register (Selector sel, int ops, Object att) throws ClosedChannelException; 86 public abstract boolean isRegistered( ); 87 abstract SelectionKey keyFor (Selector sel); 88 abstract int validOps( ); 89 abstract void configureBlocking (boolean block) IOException; 90 abstract boolean isBlocking( ); 91 abstract Object blockingLock( ); 92 } 93 94 ------------------------------------------------------------------------------------------------------------------ 95 96 public abstract class Selector 97 { 98 public static Selector open( ) throws IOException 99 public abstract boolean isOpen( ); 100 public abstract void close( ) throws IOException; 101 public abstract SelectionProvider provider( ); 102 public abstract int select( ) throws IOException; 103 public abstract int select (long timeout) throws IOException; 104 public abstract int selectNow( ) throws IOException; 105 public abstract void wakeup( ); 106 public abstract Set keys( ); 107 public abstract Set selectedKeys( ); 108 } 109 110 ------------------------------------------------------------------------------------------------------------------- 111 112 public abstract class SelectionKey 113 { 114 public static final int OP_READ 115 public static final int OP_WRITE 116 public static final int OP_CONNECT 117 public static final int OP_ACCEPT 118 public abstract SelectableChannel channel( ); 119 public abstract Selector selector( ); 120 public abstract void cancel( ); 121 public abstract boolean isValid( ); 122 public abstract int interestOps( ); 123 public abstract void interestOps (int ops); 124 public abstract int readyOps( ); 125 public final boolean isReadable( ) 126 public final boolean isWritable( ) 127 public final boolean isConnectable( ) 128 public final boolean isAcceptable( ) 129 public final Object attach (Object ob) 130 public final Object attachment( ) 131 } 132 133 ----------------------------------------------------------------------------------------------------------------------- 134 135 选择器包含了注册到它们之上的通道的集合。在任意给定的时间里,对于一个给定的选择器和 136 一个给定的通道而言,只有一种注册关系是有效的。但是,将一个通道注册到多于一个的选择器上 137 允许的。这么做的话,在更新interest集合为指定的值的同时,将返回与之前相同的选择键。实际 138 上,后续的注册都只是简单地将与之前的注册关系相关的键进行更新。 139 140 一个例外的情形是当您试图将一个通道注册到一个相关的键已经被取消的选择器上,而通道仍 141 然处于被注册的状态的时候。通道不会在键被取消的时候立即注销。直到下一次操作发生为止,它 142 们仍然会处于被注册的状态。在这种情况下,未检查的CancelledKeyException将 143 会被抛出。请务必在键可能被取消的情况下检查SelectionKey对象的状态。 144 145 ----------------------------------------------------------------------------------------------------------------------- 146 147 有 四 种被 定 义 的 可 选 择 操 作 :读 (read) , 写 (write), 连 接 (connect) 和 接 受(accept) 148 149 ----------------------------------------------------------------------------------------------------------------------- 150 151 最简单的状态测试方法是: 152 SelectionKey.isAcceptable() 等价于 ((key.readyOps( ) & SelectionKey.OP_ACCEPT) != 0 153 SelectionKey.isConnectable() 等价于 ((key.readyOps( ) & SelectionKey.OP_CONNECT) != 0 154 SelectionKey.isReadable() 等价于 ((key.readyOps( ) & SelectionKey.OP_READ) != 0 155 SelectionKey.isWritable() 等价于 ((key.readyOps( ) & SelectionKey.OP_WRITE) != 0