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 47513 2007-12-07 06:32:21Z cxh $
Author:
Christopher Brooks
See Also:
JTextAreaExec
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (cxh)

Field Summary
private  boolean _appendToStderrAndStdout
          If true, append to stderr and stdout as the commands are executed.
private static java.lang.String _eol
          End of line character.
 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
private  void _appendToBuffer(java.lang.String text)
          Append to the internal StringBuffer.
 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.


_appendToStderrAndStdout

private boolean _appendToStderrAndStdout
If true, append to stderr and stdout as the commands are executed.


_eol

private static java.lang.String _eol
End of line character. Under Unix: "\n", under Windows: "\n\r". We use a end of line charactor so that the files we generate have the proper end of line character for use by other native tools.

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.

_appendToBuffer

private void _appendToBuffer(java.lang.String text)
Append to the internal StringBuffer.

Parameters:
text - The text to append. If the text does not end with an end of line character(s), then _eol is appended.