/**
   * THIS COMMAND CAN ONLY BE CALLED FROM streamCommands UNLESS THE nextCommand OBJECT IS
   * SYNCHRONIZED.
   *
   * <p>Returns the next command with the following priority: 1. nextCommand object if set. 2. Front
   * of the commandBuffer collection. 3. Next line in the commandStream.
   *
   * @return
   */
  private GcodeCommand getNextCommand() {
    if (nextCommand != null) {
      return nextCommand;
    } else if (!this.commandBuffer.isEmpty()) {
      nextCommand = new GcodeCommand(commandBuffer.pop());
    } else
      try {
        if (rawCommandStream != null && rawCommandStream.ready()) {
          nextCommand = new GcodeCommand(rawCommandStream.readLine());
        } else if (commandStream != null && commandStream.ready()) {
          nextCommand = commandStream.getNextCommand();
        }
      } catch (IOException ex) {
        // Fall through to null handling.
      }

    if (nextCommand != null) {
      nextCommand.setCommandNumber(getNextCommandId());
      if (nextCommand.getCommandString().endsWith("\n")) {
        nextCommand.setCommand(nextCommand.getCommandString().trim());
      }
      return nextCommand;
    }
    return null;
  }