CommandContext readCommandContext() throws IOException {
    // client info - command line arguments and environment
    List<String> remoteArgs = new ArrayList();
    Properties remoteEnv = new Properties();
    String cwd = null; // working directory
    String command = null; // alias or class name
    // read everything from the client up to and including the command
    while (command == null) {
      int bytesToRead = in.readInt();
      byte chunkType = in.readByte();

      byte[] b = new byte[bytesToRead];
      in.readFully(b);
      String line = new String(b, "UTF-8");

      switch (chunkType) {
        case NGConstants.CHUNKTYPE_ARGUMENT:
          //	command line argument
          remoteArgs.add(line);
          break;

        case NGConstants.CHUNKTYPE_ENVIRONMENT:
          //	parse environment into property
          int equalsIndex = line.indexOf('=');
          if (equalsIndex > 0) {
            remoteEnv.setProperty(line.substring(0, equalsIndex), line.substring(equalsIndex + 1));
          }
          break;

        case NGConstants.CHUNKTYPE_COMMAND:
          // 	command (alias or classname)
          command = line;
          break;

        case NGConstants.CHUNKTYPE_WORKINGDIRECTORY:
          //	client working directory
          cwd = line;
          break;

        default: // freakout?
      }
    }

    // Command and environment is read. Move other communication with client, which is heartbeats
    // and
    // stdin, to background thread
    startBackgroundReceive();

    return new CommandContext(command, cwd, remoteEnv, remoteArgs);
  }
CommandContext readCommandContext ( ) throws IOException { List < String > remoteArgs = new ArrayList ( ) ; Properties remoteEnv = new Properties ( ) ; String cwd = null ; String command = null ; while ( command == null ) { int bytesToRead = in . readInt ( ) ; byte chunkType = in . readByte ( ) ; byte [ ] b = new byte [ bytesToRead ] ; in . readFully ( b ) ; String line = new String ( b , "UTF-8" ) ; switch ( chunkType ) { case NGConstants . CHUNKTYPE_ARGUMENT : remoteArgs . add ( line ) ; break ; case NGConstants . CHUNKTYPE_ENVIRONMENT : int equalsIndex = line . indexOf ( '=' ) ; if ( equalsIndex > 0 ) { remoteEnv . setProperty ( line . substring ( 0 , equalsIndex ) , line . substring ( equalsIndex + 1 ) ) ; } break ; case NGConstants . CHUNKTYPE_COMMAND : command = line ; break ; case NGConstants . CHUNKTYPE_WORKINGDIRECTORY : cwd = line ; break ; default : } } startBackgroundReceive ( ) ; return new CommandContext ( command , cwd , remoteEnv , remoteArgs ) ; }
Get nail command context from the header and start reading for stdin and heartbeats