org.im4java.process
Class ProcessStarter

java.lang.Object
  extended by org.im4java.process.ProcessStarter
Direct Known Subclasses:
ImageCommand

public class ProcessStarter
extends java.lang.Object

This class implements the processing of os-commands using Runtime.exec(). The class does not use the newer ProcessBuilder, since it has no knowledge about the valid os-arguments of the generated command.

Version:
$Revision: 1.16 $
Author:
$Author: bablokb, Bruno Mascret (warnings, Image Magick Path) $

Field Summary
static int BUFFER_SIZE
          Buffer size of process input-stream (used for reading the output (sic!)
private  java.lang.String convertPath
          Path to convert
private  boolean iAsyncMode
          Execution-mode.
private  ErrorConsumer iErrorConsumer
          The ErrorConsumer for the stderr of the ProcessStarter.
private  InputProvider iInputProvider
          The InputProvider for the ProcessStarter (if used as a pipe).
private  OutputConsumer iOutputConsumer
          The OutputConsumer for the ProcessStarter (if used as a pipe).
private  java.util.LinkedList<ProcessListener> iProcessListener
          The ProcessListeners for this ProcessStarter.
 
Constructor Summary
protected ProcessStarter()
          Constructor.
 
Method Summary
 void addProcessListener(ProcessListener pProcessListener)
          Add a ProcessListener to this ProcessStarter.
 InputProvider getIInputProvider()
           
 java.util.LinkedList<ProcessListener> getIProcessListener()
          return iProcessListener
 boolean isAsyncMode()
           
private  void processError(java.io.InputStream pInputStream, ErrorConsumer pConsumer)
          Let the ErrorConsumer process the stderr-stream.
private  void processInput(java.io.OutputStream pOutputStream)
          Pipe input to the command.
private  void processOutput(java.io.InputStream pInputStream, OutputConsumer pConsumer)
          Let the OutputConsumer process the output of the command.
protected  int run(java.util.LinkedList<java.lang.String> pArgs)
          Execute the command.
 void setAsyncMode(boolean pAsyncMode)
           
 void setErrorConsumer(ErrorConsumer pErrorConsumer)
          Set the ErrorConsumer for the stderr of the ProcessStarter.
 void setInputProvider(InputProvider pInputProvider)
          Set the InputProvider for the ProcessStarter (if used as a pipe).
 void setOutputConsumer(OutputConsumer pOutputConsumer)
          Set the OutputConsumer for the ProcessStarter (if used as a pipe).
 java.lang.Process startProcess(java.util.LinkedList<java.lang.String> pArgs)
          Execute the command.
 int waitForProcess(java.lang.Process pProcess)
          Perform process input/output and wait for process to terminate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

public static final int BUFFER_SIZE
Buffer size of process input-stream (used for reading the output (sic!) of the process). Currently 64KB.

See Also:
Constant Field Values

iInputProvider

private InputProvider iInputProvider
The InputProvider for the ProcessStarter (if used as a pipe).


iOutputConsumer

private OutputConsumer iOutputConsumer
The OutputConsumer for the ProcessStarter (if used as a pipe).


iErrorConsumer

private ErrorConsumer iErrorConsumer
The ErrorConsumer for the stderr of the ProcessStarter.


iAsyncMode

private boolean iAsyncMode
Execution-mode. If true, run asynchronously.


convertPath

private java.lang.String convertPath
Path to convert


iProcessListener

private java.util.LinkedList<ProcessListener> iProcessListener
The ProcessListeners for this ProcessStarter.

Constructor Detail

ProcessStarter

protected ProcessStarter()
Constructor. Set the image magick path if windows

Method Detail

setInputProvider

public void setInputProvider(InputProvider pInputProvider)
Set the InputProvider for the ProcessStarter (if used as a pipe).

Parameters:
pInputProvider - the InputProvider to be set

getIInputProvider

public InputProvider getIInputProvider()
Returns:
the iInputProvider

setOutputConsumer

public void setOutputConsumer(OutputConsumer pOutputConsumer)
Set the OutputConsumer for the ProcessStarter (if used as a pipe).

Parameters:
pOutputConsumer - the OutputConsumer to be set

setErrorConsumer

public void setErrorConsumer(ErrorConsumer pErrorConsumer)
Set the ErrorConsumer for the stderr of the ProcessStarter.

Parameters:
pErrorConsumer - the ErrorConsumer to be set

getIProcessListener

public java.util.LinkedList<ProcessListener> getIProcessListener()
return iProcessListener

Returns:
ProcessListener linked list

addProcessListener

public void addProcessListener(ProcessListener pProcessListener)
Add a ProcessListener to this ProcessStarter.

Parameters:
pProcessListener - the ProcessListener to add

processInput

private void processInput(java.io.OutputStream pOutputStream)
                   throws java.io.IOException
Pipe input to the command. This is done asynchronously.

Parameters:
pOutputStream - the OutputConsumer to use
Throws:
java.io.IOException - if InputProvider.provideInput(OutputStream) failed

processOutput

private void processOutput(java.io.InputStream pInputStream,
                           OutputConsumer pConsumer)
                    throws java.io.IOException
Let the OutputConsumer process the output of the command.

Parameters:
pInputStream - the InputStream to use
pConsumer - the OutputConsumer to use
Throws:
java.io.IOException - if IO problems with BufferedInputStream

processError

private void processError(java.io.InputStream pInputStream,
                          ErrorConsumer pConsumer)
                   throws java.io.IOException
Let the ErrorConsumer process the stderr-stream.

Parameters:
pInputStream - the InputStream to use
pConsumer - the ErrorConsumer to use
Throws:
java.io.IOException - if IO problems with BufferedInputStream

run

protected int run(java.util.LinkedList<java.lang.String> pArgs)
           throws java.io.IOException,
                  java.lang.InterruptedException
Execute the command.

Parameters:
pArgs - arguments for command
Returns:
process return value
Throws:
java.io.IOException - IO problems
java.lang.InterruptedException - Interruptions problems

startProcess

public java.lang.Process startProcess(java.util.LinkedList<java.lang.String> pArgs)
                               throws java.io.IOException,
                                      java.lang.InterruptedException
Execute the command.

Parameters:
pArgs - list of arguments for the process
Returns:
return value of ProcessBuilder.start()
Throws:
java.io.IOException - IO problems
java.lang.InterruptedException - Interruption problems

waitForProcess

public int waitForProcess(java.lang.Process pProcess)
                   throws java.io.IOException,
                          java.lang.InterruptedException
Perform process input/output and wait for process to terminate.

Parameters:
pProcess - the process
Returns:
the process exit value
Throws:
java.io.IOException - IO problems
java.lang.InterruptedException - Interrupted Exception problems

setAsyncMode

public void setAsyncMode(boolean pAsyncMode)
Parameters:
pAsyncMode - the iAsyncMode to set

isAsyncMode

public boolean isAsyncMode()
Returns:
the iAsyncMode