/**
  * Creates a new ConnectionHandler object
  *
  * @param sChannel the SocketChannel of the client
  * @param data a reference to a ReactorData object
  */
 private ConnectionHandler(SocketChannel sChannel, ReactorData<T> data, SelectionKey key) {
   _sChannel = sChannel;
   _data = data;
   _protocol = _data.getProtocolMaker().create();
   _tokenizer = _data.getTokenizerMaker().create();
   _skey = key;
 }
  /**
   * Reads incoming data from the client:
   *
   * <UL>
   *   <LI>Reads some bytes from the SocketChannel
   *   <LI>create a protocolTask, to process this data, possibly generating an answer
   *   <LI>Inserts the Task to the ThreadPool
   * </UL>
   *
   * @throws
   * @throws IOException in case of an IOException during reading
   */
  public void read() {
    // do not read if OLD.protocol has terminated. only write of pending data is
    // allowed
    if (_protocol.shouldClose()) {
      return;
    }

    SocketAddress address = _sChannel.socket().getRemoteSocketAddress();
    logger.info("Reading from " + address);

    ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
    int numBytesRead = 0;
    try {
      numBytesRead = _sChannel.read(buf);
    } catch (IOException e) {
      numBytesRead = -1;
    }
    // is the channel closed??
    if (numBytesRead == -1) {
      // No more bytes can be read from the channel
      logger.info("client on " + address + " has disconnected");
      closeConnection();
      // tell the OLD.protocol that the connection terminated.
      _protocol.connectionTerminated();
      return;
    }

    // add the buffer to the OLD.protocol task
    buf.flip();
    _task.addBytes(buf);
    // add the OLD.protocol task to the OLD.reactor
    _data.getExecutor().execute(_task);
  }
 /**
  * switches the handler to read only mode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToReadOnlyMode() {
   _skey.interestOps(SelectionKey.OP_READ);
   _data.getSelector().wakeup();
 }
 /**
  * switches the handler to write only mode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToWriteOnlyMode() {
   _skey.interestOps(SelectionKey.OP_WRITE);
   _data.getSelector().wakeup();
 }
 /**
  * switches the handler to read / write TODO Auto-generated catch blockmode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToReadWriteMode() {
   _skey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
   _data.getSelector().wakeup();
 }