private void processWrite(SelectionKey key) throws IOException {

        WritableByteChannel ch = (WritableByteChannel) key.channel();
        synchronized (writeBuf) {
            writeBuf.flip();

            int bytesOp = 0, bytesTotal = 0;
            while (writeBuf.hasRemaining() && (bytesOp = ch.write(writeBuf)) > 0)
                bytesTotal += bytesOp;

            bytesOut.addAndGet(bytesTotal);
            //   int internalbyteSent= socketChannel.write(buffer);
            bytesSent.addAndGet(bytesTotal);

            if (writeBuf.remaining() == 0) {
                key.interestOps(key.interestOps() ^ SelectionKey.OP_WRITE);
            }

            if (bytesTotal > 0) writeBuf.notify();
            else if (bytesOp == -1) {
                log.info("peer closed write channel");
                ch.close();
            }

            writeBuf.compact();
        }
    }