Skip to content

It would be nice if there was compatibility #360

@LiJoeAllen

Description

@LiJoeAllen
package net.bramp.ffmpeg.io;

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

/**
 * A collection of utility methods for dealing with processes.
 * 
 * @author bramp
 */
public final class ProcessUtils {
  private ProcessUtils() {
    throw new AssertionError("No instances for you!");
  }

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

    try {
      p.waitFor(timeout, unit);

    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }

    if (p.isAlive()) {
      throw new TimeoutException("Process did not finish within timeout");
    }

    return p.exitValue();
  }
}
package net.bramp.ffmpeg.io;

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

/**
 * A collection of utility methods for dealing with processes.
 *
 * @author bramp
 */
public final class ProcessUtils {
    private ProcessUtils() {
        throw new AssertionError("No instances for you!");
    }

    /**
     * Waits until a process finishes or a timeout occurs
     *
     * @param p       process
     * @param timeout timeout in given unit
     * @param unit    time unit
     * @return the process exit value
     * @throws TimeoutException if a timeout occurs
     */
    public static int waitForWithTimeout(final Process p, long timeout, TimeUnit unit)
            throws TimeoutException {
        long timeoutMillis = unit.toMillis(timeout);
        long startTime = System.currentTimeMillis();
        long waitMillis = 100; // Check interval in milliseconds

        while (true) {
            try {
                // Attempt to get exit value - this won't block if process has finished
                return p.exitValue();
            } catch (IllegalThreadStateException e) {
                // Process is still running
                if (System.currentTimeMillis() - startTime >= timeoutMillis) {
                    throw new TimeoutException("Process did not finish within timeout");
                }

                try {
                    // Sleep for a short interval before checking again
                    Thread.sleep(Math.min(waitMillis, timeoutMillis));
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    // If interrupted, we'll exit and check one last time
                    try {
                        return p.exitValue();
                    } catch (IllegalThreadStateException ise) {
                        throw new TimeoutException("Process interrupted and did not finish");
                    }
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions