private void writeMessage(SocketEntry entry, SocketChannel sc) throws IOException {
   byte[] message = entry.nextMessage();
   if (message != null) {
     ByteBuffer buffer = ByteBuffer.wrap(message);
     sc.write(buffer);
     if (logger.isDebugEnabled()) {
       logger.debug(
           "Send message with length "
               + message.length
               + " to "
               + entry.getPeerAddress()
               + ": "
               + new OctetString(message).toHexString());
     }
     sc.register(selector, SelectionKey.OP_READ);
   }
 }
 private void dispatchMessage(
     TcpAddress incomingAddress, ByteBuffer byteBuffer, long bytesRead) {
   byteBuffer.flip();
   if (logger.isDebugEnabled()) {
     logger.debug(
         "Received message from "
             + incomingAddress
             + " with length "
             + bytesRead
             + ": "
             + new OctetString(byteBuffer.array(), 0, (int) bytesRead).toHexString());
   }
   ByteBuffer bis;
   if (isAsyncMsgProcessingSupported()) {
     byte[] bytes = new byte[(int) bytesRead];
     System.arraycopy(byteBuffer.array(), 0, bytes, 0, (int) bytesRead);
     bis = ByteBuffer.wrap(bytes);
   } else {
     bis = ByteBuffer.wrap(byteBuffer.array(), 0, (int) bytesRead);
   }
   fireProcessMessage(incomingAddress, bis);
 }
 private void readMessage(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress)
     throws IOException {
   // note that socket has been used
   SocketEntry entry = (SocketEntry) sockets.get(incomingAddress);
   if (entry != null) {
     entry.used();
     ByteBuffer readBuffer = entry.getReadBuffer();
     if (readBuffer != null) {
       readChannel.read(readBuffer);
       if (readBuffer.hasRemaining()) {
         readChannel.register(selector, SelectionKey.OP_READ, entry);
       } else {
         dispatchMessage(incomingAddress, readBuffer, readBuffer.capacity());
       }
       return;
     }
   }
   ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
   byteBuffer.limit(messageLengthDecoder.getMinHeaderLength());
   long bytesRead = readChannel.read(byteBuffer);
   if (logger.isDebugEnabled()) {
     logger.debug("Reading header " + bytesRead + " bytes from " + incomingAddress);
   }
   MessageLength messageLength = new MessageLength(0, Integer.MIN_VALUE);
   if (bytesRead == messageLengthDecoder.getMinHeaderLength()) {
     messageLength = messageLengthDecoder.getMessageLength(ByteBuffer.wrap(buf));
     if (logger.isDebugEnabled()) {
       logger.debug("Message length is " + messageLength);
     }
     if ((messageLength.getMessageLength() > getMaxInboundMessageSize())
         || (messageLength.getMessageLength() <= 0)) {
       logger.error(
           "Received message length "
               + messageLength
               + " is greater than inboundBufferSize "
               + getMaxInboundMessageSize());
       synchronized (entry) {
         entry.getSocket().close();
         logger.info("Socket to " + entry.getPeerAddress() + " closed due to an error");
       }
     } else {
       byteBuffer.limit(messageLength.getMessageLength());
       bytesRead += readChannel.read(byteBuffer);
       if (bytesRead == messageLength.getMessageLength()) {
         dispatchMessage(incomingAddress, byteBuffer, bytesRead);
       } else {
         byte[] message = new byte[byteBuffer.limit()];
         byteBuffer.flip();
         byteBuffer.get(message, 0, byteBuffer.limit() - byteBuffer.remaining());
         entry.setReadBuffer(ByteBuffer.wrap(message));
       }
       readChannel.register(selector, SelectionKey.OP_READ, entry);
     }
   } else if (bytesRead < 0) {
     logger.debug("Socket closed remotely");
     sk.cancel();
     readChannel.close();
     TransportStateEvent e =
         new TransportStateEvent(
             DefaultTcpTransportMapping.this,
             incomingAddress,
             TransportStateEvent.STATE_DISCONNECTED_REMOTELY,
             null);
     fireConnectionStateChanged(e);
   }
 }