org.activemath.presentation.io
Class ExecStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.activemath.presentation.io.ExecStream
All Implemented Interfaces:
Closeable, Flushable

public class ExecStream
extends OutputStream

Encapsulate an external process into an OutputStream. Execution begins as soon as any data is written to the stream, or when the stream is closed.
Typical usage is as follows:

  public static void main(String [] args) {
    ExecStream execStream;
    execStream = new ExecStream();

    try {
      String [] command = { "maxima" };
      String input = "2+3=4;";
      execStream.setCommand(command);
      execStream.write(input.getBytes("UTF-8"));
      execStream.close();
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }
 
In this example, since no output stream is given, the output is sent to "System.out". Same for the error stream, whose default is "System.err".

Version:
$Revision: 1.10 $ $Date: 2007/08/07 13:11:33 $

Field Summary
protected static Logger log
           
 
Constructor Summary
ExecStream()
           
ExecStream(OutputStream out)
           
ExecStream(OutputStream out, String[] command, File workingDirectory, OutputStream err, long timeout)
           
ExecStream(OutputStream out, String[] command, OutputStream err, long timeout)
           
 
Method Summary
 void close()
           
protected  void finalize()
           
 void flush()
           
 int getExitValue()
           
 void killProcess()
           
static void main(String[] args)
          For testing.
 void setCommand(String[] command)
           
 void setErrorStream(OutputStream err)
          Error stream for the process.
 void setOutputStream(OutputStream out)
           
 void setTimeout(long timeout)
          Process timeout.
 void setWorkingDirectory(File workingDirectory)
           
 void write(byte[] b)
           
 void write(byte[] b, int offset, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static Logger log
Constructor Detail

ExecStream

public ExecStream()

ExecStream

public ExecStream(OutputStream out)

ExecStream

public ExecStream(OutputStream out,
                  String[] command,
                  OutputStream err,
                  long timeout)

ExecStream

public ExecStream(OutputStream out,
                  String[] command,
                  File workingDirectory,
                  OutputStream err,
                  long timeout)
Method Detail

killProcess

public void killProcess()

finalize

protected final void finalize()
                       throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

setCommand

public void setCommand(String[] command)

setWorkingDirectory

public void setWorkingDirectory(File workingDirectory)

setOutputStream

public void setOutputStream(OutputStream out)

setErrorStream

public void setErrorStream(OutputStream err)
Error stream for the process.

Parameters:
err - stream to receive errors. Default is "System.err".

setTimeout

public void setTimeout(long timeout)
Process timeout.
If the process takes more than timeout milliseconds to execute, it's aborted.
A value of 0 means that the process should not be interrupted.

Parameters:
timeout - maximum allowed time for the process to finish.

getExitValue

public int getExitValue()
Returns:
exitValue of terminated process, or -1.

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int offset,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

main

public static void main(String[] args)
For testing. This sends a string to an external process, and redirects its output to the Java runtime's standard output.