openlsd.common
Class LSDStreamGobbler

java.lang.Object
  extended by java.lang.Thread
      extended by openlsd.common.LSDStreamGobbler
All Implemented Interfaces:
java.lang.Runnable

public class LSDStreamGobbler
extends java.lang.Thread

Class to make a fix for Runtime.exec bug.
The API was not really well documented but now there is at least a warning (as of the 1.6 version) in the Process API:
"All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock."
What does this means ? Well, if you launch a process but you don't care about its ErrorStream for instance and so you don't try to read anything from it, you likely will have a chance to block the subprocess and even the main JVM, so having a deadlock. To avoid this, I use a specific thread to read from error stream and input stream (read anything without action), the StreamGobbler class.

Author:
Frederic BREGIER LGPL 2007

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 int status
          Returned status
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
LSDStreamGobbler(java.io.InputStream is)
          Init from only an IntputStream
LSDStreamGobbler(java.io.InputStream is, java.io.OutputStream redirect)
          Init from InputStream and OutputStream
LSDStreamGobbler(java.lang.Process p)
          Init from only a Process without taking into account InputStream.
 
Method Summary
 void run()
          Run the StreamGobbler or Wait for the process to finish, reading any err data.
static int waitForProcess(java.lang.Process p)
          Wait for the process to finish, reading any in/err data
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

status

public int status
Returned status

Constructor Detail

LSDStreamGobbler

public LSDStreamGobbler(java.lang.Process p)
Init from only a Process without taking into account InputStream. InputStream is not read since it will be read by an external program.

Parameters:
p -

LSDStreamGobbler

public LSDStreamGobbler(java.io.InputStream is)
Init from only an IntputStream

Parameters:
is -

LSDStreamGobbler

public LSDStreamGobbler(java.io.InputStream is,
                        java.io.OutputStream redirect)
Init from InputStream and OutputStream

Parameters:
is -
redirect -
Method Detail

run

public void run()
Run the StreamGobbler or Wait for the process to finish, reading any err data.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

waitForProcess

public static int waitForProcess(java.lang.Process p)
Wait for the process to finish, reading any in/err data

Parameters:
p -
Returns:
the status of the process