private Command readBinaryCommand() throws IOException {
    SocketChannel channel = socket.getChannel();
    if (channel == null || !channel.isOpen()) {
      throw new IllegalStateException("cannot read from channel");
    }
    Command cmd = null;
    boolean done = false;
    boolean read = false;
    while (!done) {
      if (!buffer.hasRemaining()) {
        buffer.clear();
        read = true;
      } else if (!read) {
        // compact is meant for partial writes, but we want to use
        // it partial reads, so the new limit should be the position
        // after the compact operation and we want to start reading
        // from the beginning
        buffer.compact();
        buffer.limit(buffer.position());
        buffer.position(0);
      }
      if (read) {
        int bytesRead = channel.read(buffer);
        if (bytesRead == -1) {
          throw new IOException("EOF");
        }
        buffer.flip();
      }
      // read again if we did not read enough bytes
      if (buffer.limit() < HEADER_LENGTH) {
        buffer.compact();
        read = true;
        continue;
      }
      int requestLength = buffer.remaining();
      byte magic = buffer.get();
      if (magic != REQUEST_MAGIC) {
        throw new IllegalStateException("Not a valid request, magic byte incorrect");
      }
      byte opCode = buffer.get();
      if (ConnectionHandler.getLogger().finerEnabled()) {
        String str = Command.buffertoString(buffer);
        ConnectionHandler.getLogger().finer("Request:" + buffer + str);
      }
      int bodyLength = buffer.getInt(AbstractCommand.TOTAL_BODY_LENGTH_INDEX);
      if ((HEADER_LENGTH + bodyLength) > requestLength) {
        // set the position back to the start of the request
        buffer.position(buffer.position() - 2 /* since we read two bytes */);
        buffer.compact();
        // ensure that the buffer is big enough
        if (buffer.capacity() < (HEADER_LENGTH + bodyLength)) {
          // allocate bigger buffer and copy the bytes to the bigger buffer
          ByteBuffer oldBuffer = buffer;
          oldBuffer.position(0);
          buffer = ByteBuffer.allocate(HEADER_LENGTH + bodyLength);
          buffer.put(oldBuffer);
        }
        read = true;
        continue;
      }
      cmd = Command.getCommandFromOpCode(opCode);
      done = true;
    }
    if (ConnectionHandler.getLogger().fineEnabled()) {
      ConnectionHandler.getLogger().fine("read command " + cmd);
    }
    return cmd;
  }
Reads the first line from the request and interprets the {@link Command} from the memcached
client
geode-memcached/src/main/java/org/apache/geode/internal/memcached/RequestReader.java