package net.bramp.ffmpeg.io;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author bramp
 */
public abstract class ProcessUtils {
  private ProcessUtils() {}

  private static class ProcessThread extends Thread {
    final Process p;
    boolean finished = false;
    int exitValue = -1;

    private ProcessThread(Process p) {
      this.p = p;
    }

    @Override
    public void run() {
      try {
        exitValue = p.waitFor();
        finished = true;
      } catch (InterruptedException e) {
        // Ignore
      }
    }

    public boolean hasFinished() {
      return finished;
    }

    public int exitValue() {
      return exitValue;
    }
  }

  /**
   * Waits until the process finishs or the timeout occurs
   * 
   * @param p
   * @param timeout
   * @param unit
   * @return the process' exit value
   * @throws TimeoutException if the timeout occurs
   */
  public static int waitForWithTimeout(final Process p, long timeout, TimeUnit unit)
      throws TimeoutException {

    ProcessThread t = new ProcessThread(p);
    t.start();
    try {
      unit.timedJoin(t, timeout);

    } catch (InterruptedException e) {
      t.interrupt();
    }

    if (!t.hasFinished()) {
      throw new TimeoutException("Process did not finish within timeout");
    }

    return t.exitValue();
  }
}

@@ -727,19 +727,17 @@
 s until 
-the
+a
  process
@@ -747,16 +747,15 @@
 nish
+e
 s or 
-the
+a
  tim
@@ -785,16 +785,24 @@
 @param p
+ process
 %0A   * @p
@@ -813,16 +813,38 @@
  timeout
+ timeout in given unit
 %0A   * @p
@@ -848,24 +848,34 @@
  @param unit
+ time unit
 %0A   * @retur
@@ -891,9 +891,8 @@
 cess
-'
  exi
@@ -932,19 +932,17 @@
 tion if 
-the
+a
  timeout

Update Javadoc (#36)