ptolemy.util
Class StringBufferExec

java.lang.Object
  extended by ptolemy.util.StreamExec
      extended by ptolemy.util.StringBufferExec
All Implemented Interfaces:
ExecuteCommands

public class StringBufferExec
extends StreamExec

Execute commands in a subprocess and accumulate the output in a StringBuffer.

As an alternative to this class, see JTextAreaExec, which uses Swing; and StreamExec, which writes to stderr and stdout.

Sample usage:

 List execCommands = new LinkedList();
 execCommands.add("date");
 execCommands.add("sleep 3");
 execCommands.add("date");
 execCommands.add("notACommand");

 final StringBufferExec exec = new StringBufferExec();
 exec.setCommands(execCommands);

 exec.start();
 

Loosely based on Example1.java from http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

See also http://developer.java.sun.com/developer/qow/archive/135/index.jsp and http://jw.itworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Since:
Ptolemy II 6.1
Version:
$Id: StringBufferExec.java,v 1.8 2007/12/07 06:32:09 cxh Exp $
Author:
Christopher Brooks
See Also:
JTextAreaExec
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (cxh)

Field Summary
 java.lang.StringBuffer buffer
          The StringBuffer to which the output is appended.
 
Constructor Summary
StringBufferExec()
          Create a StringBufferExec.
StringBufferExec(boolean appendToStderrAndStdout)
          Create a StringBufferExec and optionally append to stderr and stdout as the commands are executed.
 
Method Summary
 void stderr(java.lang.String text)
          Append the text message to the StringBuffer.
 void stdout(java.lang.String text)
          Append the text message to the StringBuffer.
 
Methods inherited from class ptolemy.util.StreamExec
_setProgressBarMaximum, appendToPath, cancel, clear, getenv, getLastSubprocessReturnCode, setCommands, setWorkingDirectory, start, updateEnvironment, updateStatusBar
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

public java.lang.StringBuffer buffer
The StringBuffer to which the output is appended. This variable is public so that callers can clear the buffer as necessary.

Constructor Detail

StringBufferExec

public StringBufferExec()
Create a StringBufferExec. As the commands are executed, output is appended to the StringBuffer - no output will appear on stderr and stdout.


StringBufferExec

public StringBufferExec(boolean appendToStderrAndStdout)
Create a StringBufferExec and optionally append to stderr and stdout as the commands are executed.

Parameters:
appendToStderrAndStdout - If true, then as the commands are executed, the output is append to stderr and stdout.
Method Detail

stderr

public void stderr(java.lang.String text)
Append the text message to the StringBuffer. The output automatically gets a trailing end of line character(s) appended. Optionally, the text also appears on stderr

Specified by:
stderr in interface ExecuteCommands
Overrides:
stderr in class StreamExec
Parameters:
text - The text to append.

stdout

public void stdout(java.lang.String text)
Append the text message to the StringBuffer. The output automatically gets end of line character(s) appended. Optionally, the text also appears on stdout.

Specified by:
stdout in interface ExecuteCommands
Overrides:
stdout in class StreamExec
Parameters:
text - The text to append.