public int waitForCondition(Channel c,long timeout,int condition_mask){
  long end_time=0;
  boolean end_time_set=false;
synchronized (c) {
    while (true) {
      int current_cond=0;
      int stdoutAvail=c.stdoutWritepos - c.stdoutReadpos;
      int stderrAvail=c.stderrWritepos - c.stderrReadpos;
      if (stdoutAvail > 0)       current_cond=current_cond | ChannelCondition.STDOUT_DATA;
      if (stderrAvail > 0)       current_cond=current_cond | ChannelCondition.STDERR_DATA;
      if (c.EOF)       current_cond=current_cond | ChannelCondition.EOF;
      if (c.getExitStatus() != null)       current_cond=current_cond | ChannelCondition.EXIT_STATUS;
      if (c.getExitSignal() != null)       current_cond=current_cond | ChannelCondition.EXIT_SIGNAL;
      if (c.state == Channel.STATE_CLOSED)       return current_cond | ChannelCondition.CLOSED | ChannelCondition.EOF;
      if ((current_cond & condition_mask) != 0)       return current_cond;
      if (timeout > 0) {
        if (!end_time_set) {
          end_time=System.currentTimeMillis() + timeout;
          end_time_set=true;
        }
 else {
          timeout=end_time - System.currentTimeMillis();
          if (timeout <= 0)           return current_cond | ChannelCondition.TIMEOUT;
        }
      }
      try {
        if (timeout > 0)         c.wait(timeout);
 else         c.wait();
      }
 catch (      InterruptedException e) {
      }
    }
  }
}

public int waitForCondition ( Channel c , long timeout , int condition_mask ) { long end_time = 0 ; boolean end_time_set = false ; synchronized ( c ) { while ( true ) { int current_cond = 0 ; int stdoutAvail = c . stdoutWritepos - c . stdoutReadpos ; int stderrAvail = c . stderrWritepos - c . stderrReadpos ; if ( stdoutAvail > 0 ) current_cond = current_cond | ChannelCondition . STDOUT_DATA ; if ( stderrAvail > 0 ) current_cond = current_cond | ChannelCondition . STDERR_DATA ; if ( c . EOF ) current_cond = current_cond | ChannelCondition . EOF ; if ( c . getExitStatus ( ) != null ) current_cond = current_cond | ChannelCondition . EXIT_STATUS ; if ( c . getExitSignal ( ) != null ) current_cond = current_cond | ChannelCondition . EXIT_SIGNAL ; if ( c . state == Channel . STATE_CLOSED ) return current_cond | ChannelCondition . CLOSED | ChannelCondition . EOF ; if ( ( current_cond & condition_mask ) != 0 ) return current_cond ; if ( timeout > 0 ) { if ( ! end_time_set ) { end_time = System . currentTimeMillis ( ) + timeout ; end_time_set = true ; } else { timeout = end_time - System . currentTimeMillis ( ) ; if ( timeout <= 0 ) return current_cond | ChannelCondition . TIMEOUT ; } } try { if ( timeout > 0 ) c . wait ( timeout ) ; else c . wait ( ) ; } catch ( InterruptedException e ) { } } } }
wait until for a condition .
