/** Send an acknowledgment. */
  @Override
  public void sendAck() throws IOException {

    if (!committed) {
      // Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0
      socket
          .getBufHandler()
          .getWriteBuffer()
          .put(Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length);
      writeToSocket(socket.getBufHandler().getWriteBuffer(), true, true);
    }
  }
  /** Callback to write data from the buffer. */
  private void flushBuffer() throws IOException {

    // prevent timeout for async,
    SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
    if (key != null) {
      NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
      attach.access();
    }

    // write to the socket, if there is anything to write
    if (socket.getBufHandler().getWriteBuffer().position() > 0) {
      socket.getBufHandler().getWriteBuffer().flip();
      writeToSocket(socket.getBufHandler().getWriteBuffer(), true, false);
    }
  }
 /** Recycle the output buffer. This should be called when closing the connection. */
 @Override
 public void recycle() {
   super.recycle();
   if (socket != null) {
     socket.getBufHandler().getWriteBuffer().clear();
     socket = null;
   }
   lastWrite.set(1);
 }
 private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException {
   while (length > 0) {
     int thisTime = length;
     if (socket.getBufHandler().getWriteBuffer().position()
             == socket.getBufHandler().getWriteBuffer().capacity()
         || socket.getBufHandler().getWriteBuffer().remaining() == 0) {
       flushBuffer();
     }
     if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
       thisTime = socket.getBufHandler().getWriteBuffer().remaining();
     }
     socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime);
     length = length - thisTime;
     offset = offset + thisTime;
   }
   NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment) socket.getAttachment(false);
   if (ka != null) ka.access(); // prevent timeouts for just doing client writes
 }