Code example for SelectionKey

Methods: cancel, interestOps

0
            Assert.assertFalse(mChannel.isChunked());
            // register for a write notification if a write fails 
            mKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
        } 
        else { 
            mKey.interestOps(SelectionKey.OP_READ);
        } 
    } 
 
    @Override 
    public void write(final ByteBuffer b) {
        if (mServer.getAffinity() != Thread.currentThread()) {
            mServer.run(new Runnable() {
                @Override 
                public void run() { 
                    write(b);
                } 
            }); 
            return; 
        } 
        try { 
            if (!mChannel.isConnected()) {
                Assert.assertFalse(mChannel.isChunked());
                return; 
            } 
 
            // keep writing until the the socket can't write any more, or the 
            // data is exhausted. 
            mChannel.write(b);
            handleRemaining(b.remaining());
        } 
        catch (IOException ex) {
            close(); 
            reportEndPending(ex);
            reportClose(ex);
        } 
    } 
 
    private ByteBufferList pending;
     
    int mToAlloc = 0;
    int onReadable() { 
        spitPending(); 
        // even if the socket is paused, 
        // it may end up getting a queued readable event if it is 
        // already in the selector's ready queue. 
        if (mPaused)
            return 0; 
        int total = 0;
        try { 
            boolean closed = false;
            int maxAlloc = 256 * 1024; // 256K
            // keep udp at roughly the mtu, which is 1540 or something 
            // letting it grow freaks out nio apparently. 
            if (mChannel.isChunked())
                maxAlloc = 8192;
             
            ByteBuffer b = ByteBuffer.allocate(Math.min(Math.max(mToAlloc, 2 << 11), maxAlloc));
            // keep track of the max mount read during this read cycle 
            // so we can be quicker about allocations during the next 
            // time this socket reads. 
            int read = mChannel.read(b);
            if (read < 0) {
                closeInternal(); 
                closed = true;
            } 
            else { 
                total += read;
            } 
            if (read > 0) {
                mToAlloc = read * 2;
                b.limit(b.position());
                b.position(0);
                ByteBufferList list = new ByteBufferList(b);
                Util.emitAllData(this, list);
                if (b.remaining() != 0) {
                    Assert.assertTrue(pending == null);
                    pending = list;
                } 
            } 
 
            if (closed) {
                reportEndPending(null); 
                reportClose(null); 
            } 
        } 
        catch (Exception e) {
            closeInternal(); 
            reportEndPending(e);
            reportClose(e);
        } 
         
        return total;
    } 
     
    boolean closeReported;
    protected void reportClose(Exception e) {
        if (closeReported)
            return; 
        closeReported = true;
        if (mClosedHander != null) {
            mClosedHander.onCompleted(e);
            mClosedHander = null;
        } 
    } 
 
    @Override 
    public void close() { 
        closeInternal(); 
        reportClose(null); 
    } 
 
    public void closeInternal() { 
        mKey.cancel();
        try { 
            mChannel.close();
        } 
        catch (IOException e) {
        }