1 17 18 19 package org.apache.tomcat.util.net; 20 21 import java.io.IOException ; 22 import java.nio.ByteBuffer ; 23 import java.nio.channels.ByteChannel ; 24 import java.nio.channels.SocketChannel ; 25 26 import org.apache.tomcat.util.net.NioEndpoint.Poller; 27 import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler; 28 import java.nio.channels.Selector ; 29 import java.nio.channels.SelectionKey ; 30 31 40 public class NioChannel implements ByteChannel { 41 42 protected static ByteBuffer emptyBuf = ByteBuffer.allocate(0); 43 44 protected SocketChannel sc = null; 45 46 protected ApplicationBufferHandler bufHandler; 47 48 protected Poller poller; 49 50 public NioChannel(SocketChannel channel, ApplicationBufferHandler bufHandler) throws IOException { 51 this.sc = channel; 52 this.bufHandler = bufHandler; 53 } 54 55 public void reset() throws IOException { 56 bufHandler.getReadBuffer().clear(); 57 bufHandler.getWriteBuffer().clear(); 58 } 59 60 public int getBufferSize() { 61 if ( bufHandler == null ) return 0; 62 int size = 0; 63 size += bufHandler.getReadBuffer()!=null?bufHandler.getReadBuffer().capacity():0; 64 size += bufHandler.getWriteBuffer()!=null?bufHandler.getWriteBuffer().capacity():0; 65 return size; 66 } 67 68 73 public boolean flush(Selector s) throws IOException { 74 return true; } 76 77 78 84 public void close() throws IOException { 85 getIOChannel().socket().close(); 86 getIOChannel().close(); 87 } 88 89 public void close(boolean force) throws IOException { 90 if (isOpen() || force ) close(); 91 } 92 98 public boolean isOpen() { 99 return sc.isOpen(); 100 } 101 102 110 public int write(ByteBuffer src) throws IOException { 111 return sc.write(src); 112 } 113 114 122 public int read(ByteBuffer dst) throws IOException { 123 return sc.read(dst); 124 } 125 126 public Object getAttachment(boolean remove) { 127 Poller pol = getPoller(); 128 Selector sel = pol!=null?pol.getSelector():null; 129 SelectionKey key = sel!=null?getIOChannel().keyFor(sel):null; 130 Object att = key!=null?key.attachment():null; 131 if (key != null && att != null && remove ) key.attach(null); 132 return att; 133 } 134 140 public ApplicationBufferHandler getBufHandler() { 141 return bufHandler; 142 } 143 144 public Poller getPoller() { 145 return poller; 146 } 147 153 public SocketChannel getIOChannel() { 154 return sc; 155 } 156 157 163 public boolean isClosing() { 164 return false; 165 } 166 167 173 public boolean isInitHandshakeComplete() { 174 return true; 175 } 176 177 public int handshake(boolean read, boolean write) throws IOException { 178 return 0; 179 } 180 181 public void setPoller(Poller poller) { 182 this.poller = poller; 183 } 184 185 public void setIOChannel(SocketChannel IOChannel) { 186 this.sc = IOChannel; 187 } 188 189 public String toString() { 190 return super.toString()+":"+this.sc.toString(); 191 } 192 193 } 194 | Popular Tags |