Class MotionDetection.ProcessManager

  • Enclosing class:
    MotionDetection

    private static class MotionDetection.ProcessManager
    extends java.lang.Object
    ProcessManager is a class that combines the features of ProcessBuilder and Process and adds methods to handle the input and error streams. This is an abbreviated version of this class used to simplify the code used in several places in the MotionDetection program.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.ProcessBuilder builder
      ProcessBuilder
      private java.lang.Process process
      Process
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessManager​(java.lang.String... command)
      Creates a new ProcessManager with the specified commands
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static MotionDetection.ProcessManager createHandledProcess​(java.lang.String... command)
      Creates a ProcessManager with the error stream redirected to the input stream and starts that manager.
      void handleInput()
      Creates a thread to read the output of the Process and send it to standard output.
      MotionDetection.ProcessManager redirectErrorStream​(boolean redirect)
      Sets the redirectErrorStream property, if true stdout and stderr from this process will be merged.
      java.lang.Process start()
      Starts the process using the commands set by the constructor.
      int waitFor()
      Causes the current thread to wait until the process has terminated.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • builder

        private final java.lang.ProcessBuilder builder
        ProcessBuilder
      • process

        private volatile java.lang.Process process
        Process
    • Constructor Detail

      • ProcessManager

        public ProcessManager​(java.lang.String... command)
        Creates a new ProcessManager with the specified commands
        Parameters:
        command - a string array containing the program name and its arguments
    • Method Detail

      • createHandledProcess

        public static MotionDetection.ProcessManager createHandledProcess​(java.lang.String... command)
                                                                   throws java.io.IOException
        Creates a ProcessManager with the error stream redirected to the input stream and starts that manager.
        Parameters:
        command - a string array containing the program name and its arguments
        Returns:
        the created ProcessManager
        Throws:
        java.io.IOException - if an error occurs in starting the process
      • redirectErrorStream

        public MotionDetection.ProcessManager redirectErrorStream​(boolean redirect)
        Sets the redirectErrorStream property, if true stdout and stderr from this process will be merged.
        Parameters:
        redirect - the new property value
        Returns:
        the ProcessBuilder of this manager
      • handleInput

        public void handleInput()
        Creates a thread to read the output of the Process and send it to standard output.
        Throws:
        java.lang.IllegalStateException - if the process associated with this ProcessManager has not yet been created
      • start

        public java.lang.Process start()
                                throws java.io.IOException
        Starts the process using the commands set by the constructor.
        Returns:
        the Process
        Throws:
        java.io.IOException - if an I/O error occurs
        java.lang.NullPointerException - if the command list is null
        java.lang.IndexOutOfBoundsException - if the command list is empty
      • waitFor

        public int waitFor()
                    throws java.lang.InterruptedException
        Causes the current thread to wait until the process has terminated.
        Returns:
        the exit value of the process
        Throws:
        java.lang.InterruptedException - if the current thread is interrupted