Class IOUtil


  • public final class IOUtil
    extends java.lang.Object
    I/O related utility methods. We should think of a better location for these methods.
    Version:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.String,​java.lang.String> defaultEnv  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IOUtil()
      Private constructor to prevent instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.util.Map<java.lang.String,​java.lang.String> getDefaultEnvMap()
      Gets the environment of the current process.
      static java.lang.String[] getEnvironmentSafely​(java.lang.String[] toAdd)
      Returns the environment of the current process, with some variables possibly added/overwritten.
      static java.lang.String getEnvSafely​(java.lang.String var)
      Returns the value of an environment variable.
      static void main​(java.lang.String[] args)
      Utility testing method.
      static int waitForProcess​(java.lang.Process p, java.lang.StringBuilder stdout, java.lang.StringBuilder stderr)
      Runs a process, possibly capturing its stdout and/or stderr.
      • Methods inherited from class java.lang.Object

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

      • defaultEnv

        private static java.util.Map<java.lang.String,​java.lang.String> defaultEnv
    • Constructor Detail

      • IOUtil

        private IOUtil()
        Private constructor to prevent instantiation.
    • Method Detail

      • getDefaultEnvMap

        private static java.util.Map<java.lang.String,​java.lang.String> getDefaultEnvMap()
        Gets the environment of the current process.
        Returns:
        A mapping of environment variable names to values.
      • getEnvSafely

        public static java.lang.String getEnvSafely​(java.lang.String var)
        Returns the value of an environment variable. This method is here so we don't get an exception when calling System.getenv() in Java 1.4 (which we support).
        Parameters:
        var - The environment variable.
        Returns:
        The value of the variable, or null if it is not defined.
      • getEnvironmentSafely

        public static java.lang.String[] getEnvironmentSafely​(java.lang.String[] toAdd)
        Returns the environment of the current process, with some variables possibly added/overwritten. This method works even with Java 1.4.
        Parameters:
        toAdd - The environment variables to add/overwrite in the returned array. This array should have an even length, with even indices containing variable names and odd indices containing the variable values.
        Returns:
        The environment variables. This array's entries will be of the form "name=value", so it can be passed directly into Runtime.exec().
      • waitForProcess

        public static int waitForProcess​(java.lang.Process p,
                                         java.lang.StringBuilder stdout,
                                         java.lang.StringBuilder stderr)
                                  throws java.io.IOException
        Runs a process, possibly capturing its stdout and/or stderr.
        Parameters:
        p - The process.
        stdout - A buffer in which to put stdout, or null if you don't want to keep it.
        stderr - A buffer in which to keep stderr, or null if you don't want to keep it.
        Returns:
        The return code of the process.
        Throws:
        java.io.IOException - If an IO error occurs.
      • main

        public static void main​(java.lang.String[] args)
        Utility testing method.
        Parameters:
        args - Command line arguments.