edu.emory.mathcs.util.natives
Class ExecUtils

java.lang.Object
  extended byedu.emory.mathcs.util.natives.ExecUtils

public class ExecUtils
extends java.lang.Object

Utility methods to interact with and manage native processes started from Java.

Version:
1.0
Author:
Dawid Kurzyniec

Nested Class Summary
static interface ExecUtils.BrokenPipeHandler
          User-specified IO exception handler for exceptions during I/O redirection.
static class ExecUtils.CmdResult
          Represents the result of a native command.
static interface ExecUtils.ProcessExitHandler
          User-specified handler invoked when associated native process exits.
 
Method Summary
static ExecUtils.CmdResult execCommand(java.lang.Process process)
          Attach to the specified process and return its results.
static ExecUtils.CmdResult execCommand(java.lang.Process process, java.io.InputStream stdin)
          Attach to the specified process, feed specified standard input, and return process' results.
static ExecUtils.CmdResult execCommand(java.lang.String cmd)
          Execute specified command and return its results.
static void handleProcess(java.lang.Process process, java.io.InputStream stdin, boolean inAutoFlush, boolean inAutoClose, ExecUtils.BrokenPipeHandler inBrokenHandler, java.io.OutputStream stdout, boolean outAutoFlush, boolean outAutoClose, ExecUtils.BrokenPipeHandler outBrokenHandler, java.io.OutputStream stderr, boolean errAutoFlush, boolean errAutoClose, ExecUtils.BrokenPipeHandler errBrokenHandler, ExecUtils.ProcessExitHandler exitHandler)
           
static void handleProcess(java.lang.Process process, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
           
static void handleProcess(java.lang.Process process, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr, boolean autoFlush, boolean autoClose, ExecUtils.BrokenPipeHandler brokenPipeHandler, ExecUtils.ProcessExitHandler exitHandler)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

execCommand

public static ExecUtils.CmdResult execCommand(java.lang.String cmd)
                                       throws java.io.IOException,
                                              java.lang.InterruptedException
Execute specified command and return its results. Waits for the command to complete and returns its completion status and data written to standard output and error streams. The process' standard input is set to EOF. Example:
 System.out.println(ExecUtils.execCommand("/bin/ls").getOut());
 

Parameters:
cmd - the command to execute
Returns:
the results of the command execution
Throws:
java.io.IOException - if an I/O error occurs
java.lang.InterruptedException - if thread is interrupted before command completes

execCommand

public static ExecUtils.CmdResult execCommand(java.lang.Process process)
                                       throws java.io.IOException,
                                              java.lang.InterruptedException
Attach to the specified process and return its results. Waits for the process to complete and returns its completion status and data written to standard output and error streams. The process' standard input is set to EOF. Example:
 Process p = runtime.exec("/bin/ls");
 System.out.println(ExecUtils.execCommand(p).getOut());
 

Parameters:
process - the process to attach to
Returns:
the results of the process
Throws:
java.io.IOException - if an I/O error occurs
java.lang.InterruptedException - if thread is interrupted before process ends

execCommand

public static ExecUtils.CmdResult execCommand(java.lang.Process process,
                                              java.io.InputStream stdin)
                                       throws java.io.IOException,
                                              java.lang.InterruptedException
Attach to the specified process, feed specified standard input, and return process' results. Waits for the process to complete and returns completion status and data written to standard output and error streams.

Parameters:
process - the process to attach to
stdin - the data to redirect to process' standard input
Returns:
the results of the process
Throws:
java.io.IOException - if an I/O error occurs
java.lang.InterruptedException - if thread is interrupted before process ends
See Also:
execCommand(Process)

handleProcess

public static void handleProcess(java.lang.Process process,
                                 java.io.InputStream stdin,
                                 java.io.OutputStream stdout,
                                 java.io.OutputStream stderr)
                          throws java.io.IOException
Throws:
java.io.IOException

handleProcess

public static void handleProcess(java.lang.Process process,
                                 java.io.InputStream stdin,
                                 java.io.OutputStream stdout,
                                 java.io.OutputStream stderr,
                                 boolean autoFlush,
                                 boolean autoClose,
                                 ExecUtils.BrokenPipeHandler brokenPipeHandler,
                                 ExecUtils.ProcessExitHandler exitHandler)
                          throws java.io.IOException
Throws:
java.io.IOException

handleProcess

public static void handleProcess(java.lang.Process process,
                                 java.io.InputStream stdin,
                                 boolean inAutoFlush,
                                 boolean inAutoClose,
                                 ExecUtils.BrokenPipeHandler inBrokenHandler,
                                 java.io.OutputStream stdout,
                                 boolean outAutoFlush,
                                 boolean outAutoClose,
                                 ExecUtils.BrokenPipeHandler outBrokenHandler,
                                 java.io.OutputStream stderr,
                                 boolean errAutoFlush,
                                 boolean errAutoClose,
                                 ExecUtils.BrokenPipeHandler errBrokenHandler,
                                 ExecUtils.ProcessExitHandler exitHandler)
                          throws java.io.IOException
Throws:
java.io.IOException