com.izforge.izpack.util
Class IoHelper

java.lang.Object
  extended by com.izforge.izpack.util.IoHelper

public class IoHelper
extends java.lang.Object

Class with some IO related helper.


Field Summary
private static java.util.Properties envVars
           
private static java.lang.String MASKED_SLASH_PLACEHOLDER
          Placeholder during translatePath computing
 
Constructor Summary
private IoHelper()
          Default constructor
 
Method Summary
static void chmod(java.io.File file, java.lang.String permissions)
          Changes the permissions of the given file to the given POSIX permissions.
static void chmod(java.lang.String path, java.lang.String permissions)
          Changes the permissions of the given file to the given POSIX permissions.
static void copyFile(java.io.File inFile, java.io.File outFile)
          Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.
static void copyFile(java.io.File inFile, java.io.File outFile, java.lang.String permissions)
          Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.
static void copyFile(java.io.File inFile, java.io.File outFile, java.lang.String permissions, VariableSubstitutor vs)
          Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.
static void copyFile(java.io.File inFile, java.io.File outFile, java.lang.String permissions, VariableSubstitutor vs, java.lang.String type)
          Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.
static void copyFile(java.io.File inFile, java.io.File outFile, VariableSubstitutor vss)
          Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.
static void copyFile(java.lang.String inFile, java.lang.String outFile)
          Copies the contents of inFile into outFile.
static java.io.File copyToTempFile(java.io.File template, java.lang.String defaultExtension)
          Creates a temp file with delete on exit rule.
static java.io.File copyToTempFile(java.io.File template, java.lang.String defaultExtension, VariableSubstitutor vss)
          Creates a temp file with delete on exit rule.
static java.io.File copyToTempFile(java.lang.String template, java.lang.String defaultExtension)
          Creates a temp file with delete on exit rule.
static java.io.File existingParent(java.io.File path)
          Returns the first existing parent directory in a path
private static long extractLong(java.lang.String in, int assumedPlace, int halfRange, java.lang.String useNotIdentifier)
          Extracts a long value from a string in a special manner.
static java.lang.String getenv(java.lang.String key)
          Returns the value of the environment variable given by key.
static long getFreeSpace(java.lang.String path)
          Returns the free (disk) space for the given path.
static java.lang.String getPrimaryGroup()
          Returns the primary group of the current user.
private static void loadEnv()
          Loads all environment variables via an exec.
static java.lang.String replaceString(java.lang.String destination, java.lang.String what, java.lang.String with)
          Returns a string resulting from replacing all occurrences of what in this string with with.
private static void setEnvVar(java.lang.String var)
          Extracts key and value from the given string var.
static boolean supported(java.lang.String method)
          Returns whether the given method will be supported with the given environment.
static java.lang.String translatePath(java.lang.String destination, VariableSubstitutor vs)
          Translates a relative path to a local system path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MASKED_SLASH_PLACEHOLDER

private static final java.lang.String MASKED_SLASH_PLACEHOLDER
Placeholder during translatePath computing

See Also:
Constant Field Values

envVars

private static java.util.Properties envVars
Constructor Detail

IoHelper

private IoHelper()
Default constructor

Method Detail

copyFile

public static void copyFile(java.lang.String inFile,
                            java.lang.String outFile)
                     throws java.io.IOException
Copies the contents of inFile into outFile.

Parameters:
inFile - path of file which should be copied
outFile - path of file to create and copy the contents of inFile into
Throws:
java.io.IOException

copyFile

public static void copyFile(java.io.File inFile,
                            java.io.File outFile)
                     throws java.io.IOException
Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output.

Parameters:
inFile - File object for input
outFile - File object for output
Throws:
java.io.IOException - if an I/O error occurs

copyFile

public static void copyFile(java.io.File inFile,
                            java.io.File outFile,
                            java.lang.String permissions)
                     throws java.io.IOException
Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output. If permissions is not null, a chmod will be done on the output file.

Parameters:
inFile - File object for input
outFile - File object for output
permissions - permissions for the output file
Throws:
java.io.IOException - if an I/O error occurs

copyFile

public static void copyFile(java.io.File inFile,
                            java.io.File outFile,
                            VariableSubstitutor vss)
                     throws java.io.IOException
Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output. If the VariableSubstitutor is not null, a substition will be done during copy.

Parameters:
inFile - File object for input
outFile - File object for output
vss - substitutor which is used during copying
Throws:
java.io.IOException - if an I/O error occurs

copyFile

public static void copyFile(java.io.File inFile,
                            java.io.File outFile,
                            java.lang.String permissions,
                            VariableSubstitutor vs)
                     throws java.io.IOException
Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output. If the VariableSubstitutor is not null, a substition will be done during copy. If permissions is not null, a chmod will be done on the output file.

Parameters:
inFile - File object for input
outFile - File object for output
permissions - permissions for the output file
vs - substitutor which is used during copying
Throws:
java.io.IOException - if an I/O error occurs

copyFile

public static void copyFile(java.io.File inFile,
                            java.io.File outFile,
                            java.lang.String permissions,
                            VariableSubstitutor vs,
                            java.lang.String type)
                     throws java.io.IOException
Creates an in- and output stream for the given File objects and copies all the data from the specified input to the specified output. If the VariableSubstitutor is not null, a substition will be done during copy. If permissions is not null, a chmod will be done on the output file. If type is not null, that type is used as file type at substitution.

Parameters:
inFile - File object for input
outFile - File object for output
permissions - permissions for the output file
vs - substitutor which is used during copying
type - file type for the substitutor
Throws:
java.io.IOException - if an I/O error occurs

copyToTempFile

public static java.io.File copyToTempFile(java.io.File template,
                                          java.lang.String defaultExtension)
                                   throws java.io.IOException
Creates a temp file with delete on exit rule. The extension is extracted from the template if possible, else the default extension is used. The contents of template will be copied into the temporary file.

Parameters:
template - file to copy from and define file extension
defaultExtension - file extension if no is contained in template
Returns:
newly created and filled temporary file
Throws:
java.io.IOException

copyToTempFile

public static java.io.File copyToTempFile(java.io.File template,
                                          java.lang.String defaultExtension,
                                          VariableSubstitutor vss)
                                   throws java.io.IOException
Creates a temp file with delete on exit rule. The extension is extracted from the template if possible, else the default extension is used. The contents of template will be copied into the temporary file. If the variable substitutor is not null, variables will be replaced during copying.

Parameters:
template - file to copy from and define file extension
defaultExtension - file extension if no is contained in template
vss - substitutor which is used during copying
Returns:
newly created and filled temporary file
Throws:
java.io.IOException

copyToTempFile

public static java.io.File copyToTempFile(java.lang.String template,
                                          java.lang.String defaultExtension)
                                   throws java.io.IOException
Creates a temp file with delete on exit rule. The extension is extracted from the template if possible, else the default extension is used. The contents of template will be copied into the temporary file.

Parameters:
template - file to copy from and define file extension
defaultExtension - file extension if no is contained in template
Returns:
newly created and filled temporary file
Throws:
java.io.IOException

chmod

public static void chmod(java.io.File file,
                         java.lang.String permissions)
                  throws java.io.IOException
Changes the permissions of the given file to the given POSIX permissions.

Parameters:
file - the file for which the permissions should be changed
permissions - POSIX permissions to be set
Throws:
java.io.IOException - if an I/O error occurs

chmod

public static void chmod(java.lang.String path,
                         java.lang.String permissions)
                  throws java.io.IOException
Changes the permissions of the given file to the given POSIX permissions. This method will be raised an exception, if the OS is not UNIX.

Parameters:
path - the absolute path of the file for which the permissions should be changed
permissions - POSIX permissions to be set
Throws:
java.io.IOException - if an I/O error occurs

getFreeSpace

public static long getFreeSpace(java.lang.String path)
Returns the free (disk) space for the given path. If it is not ascertainable -1 returns.

Parameters:
path - path for which the free space should be detected
Returns:
the free space for the given path

supported

public static boolean supported(java.lang.String method)
Returns whether the given method will be supported with the given environment. Some methods of this class are not supported on all operation systems.

Parameters:
method - name of the method
Returns:
true if the method will be supported with the current enivronment else false
Throws:
java.lang.RuntimeException - if the given method name does not exist

existingParent

public static java.io.File existingParent(java.io.File path)
Returns the first existing parent directory in a path

Parameters:
path - path which should be scanned
Returns:
the first existing parent directory in a path

extractLong

private static long extractLong(java.lang.String in,
                                int assumedPlace,
                                int halfRange,
                                java.lang.String useNotIdentifier)
Extracts a long value from a string in a special manner. The string will be broken into tokens with a standard StringTokenizer. Arround the assumed place (with the given half range) the tokens are scaned reverse for a token which represents a long. if useNotIdentifier is not null, tokens which are contains this string will be ignored. The first founded long returns.

Parameters:
in - the string which should be parsed
assumedPlace - token number which should contain the value
halfRange - half range for detection range
useNotIdentifier - string which determines tokens which should be ignored
Returns:
founded long

getPrimaryGroup

public static java.lang.String getPrimaryGroup()
Returns the primary group of the current user. This feature will be supported only on Unix. On other systems null returns.

Returns:
the primary group of the current user

replaceString

public static java.lang.String replaceString(java.lang.String destination,
                                             java.lang.String what,
                                             java.lang.String with)
Returns a string resulting from replacing all occurrences of what in this string with with. In opposite to the String.replaceAll method this method do not use regular expression or other methods which are only available in JRE 1.4 and later. This method was special made to mask masked slashes to avert a conversion during path translation.

Parameters:
destination - string for which the replacing should be performed
what - what string should be replaced
with - with what string what should be replaced
Returns:
a new String object if what was found in the given string, else the given string self

translatePath

public static java.lang.String translatePath(java.lang.String destination,
                                             VariableSubstitutor vs)
Translates a relative path to a local system path.

Parameters:
destination - The path to translate.
Returns:
The translated path.

getenv

public static java.lang.String getenv(java.lang.String key)
Returns the value of the environment variable given by key. This method is a work around for VM versions which do not support getenv in an other way. At the first call all environment variables will be loaded via an exec. On Windows keys are not case sensitive.

Parameters:
key - variable name for which the value should be resolved
Returns:
the value of the environment variable given by key

loadEnv

private static void loadEnv()
Loads all environment variables via an exec.


setEnvVar

private static void setEnvVar(java.lang.String var)
Extracts key and value from the given string var. The key should be separated from the value by a sign. On Windows all chars of the key are translated to upper case.

Parameters:
var -