de.uplanet.io
Class IOHelper

java.lang.Object
  extended by de.uplanet.io.IOHelper

@Scriptable
public final class IOHelper
extends java.lang.Object

IOHelper class utility.

Version:
$Revision: 87659 $

Field Summary
static int EXECUTE
          constant for execution right
static int READ
          constant for read access right
static int WRITE
          constant for write access right
 
Constructor Summary
IOHelper()
           
 
Method Summary
static void clearDir(java.io.File p_dir)
          Clears a directory recursively but doesn't delete it.
static boolean clearDirectory(java.io.File p_dir)
          Recursively clear the given directory.
static void copyDir(java.io.File p_dest, java.io.File p_src)
          copy a directory recursively.
static void copyDir(java.io.File p_dest, java.io.File p_src, boolean p_bCopyMarkedDirs)
          copy a directory recursively.
static void copyDir(java.io.File p_dest, java.io.File p_src, java.io.FileFilter p_filter)
          copy a directory recursively.
static void copyDir(java.io.File p_dirDst, java.io.File p_dirSrc, java.io.FileFilter p_filter, boolean p_bCopyMarkedDirs)
          copy a directory recursively.
static void copyFile(java.io.File p_fileDst, java.io.File p_fileSrc)
          Performs buffered copy from one file to another.
static void copyFileOrDirectory(java.io.File p_src, java.io.File p_dest, java.lang.String p_strItem)
          copy a directory from a src dir to a dest dir recursively.
static void copyStream(java.io.OutputStream p_dest, java.io.InputStream p_src)
          Performs buffered copy from one stream to another.
static java.io.File createTempDirectory(java.io.File p_dirParent, java.lang.String p_strPrefix, java.lang.String p_strSuffix)
          Create a new directory with a unique name.
static boolean deleteFile(java.io.File p_file)
          Same as deleteFileRecursively(File).
static boolean deleteFile(java.lang.String p_strFile)
          Delete files recursively
static boolean deleteFileRecursively(java.io.File p_file)
          Delete files recursively.
static java.lang.String getBase64StringFromFile(java.lang.String p_strFile)
           
static long getCRC32(java.io.File p_file)
          Computes the CRC-32 of the given file.
static java.io.File getFileBelow(java.io.File p_dir, java.lang.String p_strPath)
          Get a file object that represents a file or directory in a sub-path below a given directory.
static java.io.File getFileBelow(java.lang.String p_strPath)
          Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.
static byte[] getFileBytes(java.io.File p_file)
           
static int getFileNumber(java.io.File p_file)
           
static long getFileSize(java.io.File p_file)
           
static java.lang.String getFileText(java.io.File p_file, java.lang.String p_strEncoding)
          Get the content of a small text file.
static java.lang.String getNormalizedPath(java.io.File p_file)
          Get the normalized path of an existing file or directory.
static java.lang.String getPathRelative(java.io.File p_dir, java.io.File p_file)
          Get the path of an existing file or directory relative to a given existing directory.
static java.io.File getUniqueNumberedFile(java.io.File p_file)
           
static boolean isChild(java.io.File p_dir, java.io.File p_file)
          Checks, if the specified file is a child of the specified directory or of its child directories.
static boolean isFileBelow(java.io.File p_dir, java.io.File p_file)
          Check if the given file's location is below the given directory.
static void setFileOwner(java.lang.String p_strPath, java.lang.String p_strOwner, boolean p_bRecursive)
          Set the owner of a file or directory.
static void setFilePermissions(java.lang.String p_strPath, int p_iUser, int p_iGroup, int p_iOthers, boolean p_bRecursive)
          Set access permissions of a file or directory.
static java.lang.String stringFromBase64ByteArray(java.lang.String p_strBase64, java.lang.String p_strCharset)
           
static java.io.File stripFileNumber(java.io.File p_file)
           
static void tarDir(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar, java.lang.String p_strRootDir, java.io.File p_dirSrc, boolean p_bTarMarkedDirs)
          Deprecated. Use TarHelper.tarDir(TarArchiveOutputStream, String, File, boolean, Set) instead.
static void untarDir(java.io.File p_fileArchive, java.lang.String p_strRootDir, java.io.File p_dirDst)
          Deprecated. Use TarHelper.untarDir(File, String, File) instead.
static void unzipDir(java.io.File p_fileZip, java.io.File p_dirDst)
          Deprecated. Use ZipHelper.unzipDir(File, File) instead.
static void unzipDir(java.util.zip.ZipFile p_zip, java.lang.String p_strRootDir, java.io.File p_dirDst)
          Deprecated. Use ZipHelper.unzipDir(ZipFile, String, File) instead.
static void unzipDir(java.util.zip.ZipInputStream p_zip, java.lang.String p_strPrefixFilter, java.io.File p_dirDst)
          Deprecated. Use ZipHelper.unzipDir(ZipInputStream, String, File) instead.
static void writeFileText(java.io.File p_file, java.lang.String p_strText, java.lang.String p_strEncoding)
          Write the content of a small text file.
static void zipDir(java.io.File p_fileZip, java.io.File p_dirSrc)
          Deprecated. Use ZipHelper.zipDir(File, File) instead.
static void zipDir(java.util.zip.ZipOutputStream p_zip, java.lang.String p_strRootDir, java.io.File p_dirSrc)
          Deprecated. Use ZipHelper.zipDir(ZipOutputStream, String, File) instead.
static void zipDir(java.util.zip.ZipOutputStream p_zip, java.lang.String p_strRootDir, java.io.File p_dirSrc, boolean p_bZipMarkedDirs)
          Deprecated. Use ZipHelper.zipDir(ZipOutputStream, String, File, boolean, Set) instead.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

READ

public static final int READ
constant for read access right

See Also:
Constant Field Values

WRITE

public static final int WRITE
constant for write access right

See Also:
Constant Field Values

EXECUTE

public static final int EXECUTE
constant for execution right

See Also:
Constant Field Values
Constructor Detail

IOHelper

public IOHelper()
Method Detail

copyStream

public static void copyStream(java.io.OutputStream p_dest,
                              java.io.InputStream p_src)
                       throws java.io.IOException

Performs buffered copy from one stream to another.

The destination buffer is being flushed when copying is ready.

Parameters:
p_src - The source stream.
p_dest - The destination stream.
Throws:
java.io.IOException - If an I/O-error occurs.

getFileBelow

public static java.io.File getFileBelow(java.lang.String p_strPath)
                                 throws java.io.IOException
Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.

Parameters:
p_strPath - The sub-path within the given directory.
Returns:
A file object that represents a file or directory in a sub-path of the given directory.
Throws:
java.io.IOException - If an I/O error occurred.
java.lang.IllegalArgumentException - If p_dir is null, or if p_strPath is null or empty.
java.lang.SecurityException - If p_strPath references a file location outside the given directory.

isFileBelow

public static boolean isFileBelow(java.io.File p_dir,
                                  java.io.File p_file)
                           throws java.io.IOException
Check if the given file's location is below the given directory.

Parameters:
p_dir - The containing directory.
p_file - The sub-path within the given directory.
Returns:
true if the given file's location is below the given, directory, or false otherwise.
Throws:
java.io.IOException - If an I/O error occurred.
java.lang.IllegalArgumentException - If p_dir is null, or if p_strPath is null or empty.

getFileBelow

public static java.io.File getFileBelow(java.io.File p_dir,
                                        java.lang.String p_strPath)
                                 throws java.io.IOException
Get a file object that represents a file or directory in a sub-path below a given directory.

Parameters:
p_dir - The containing directory.
p_strPath - The sub-path within the given directory.
Returns:
A file object that represents a file or directory in a sub-path of the given directory.
Throws:
java.io.IOException - If an I/O error occurred.
java.lang.IllegalArgumentException - If p_dir is null, or if p_strPath is null or empty.
java.lang.SecurityException - If p_strPath references a file location outside the given directory.

copyFile

public static void copyFile(java.io.File p_fileDst,
                            java.io.File p_fileSrc)
                     throws java.io.IOException

Performs buffered copy from one file to another.

Parameters:
p_fileDst - The destination file.
p_fileSrc - The source file.
Throws:
java.io.IOException - If an I/O-error occurs.

copyDir

public static void copyDir(java.io.File p_dest,
                           java.io.File p_src)
                    throws java.io.IOException
copy a directory recursively.
p_dest is the parent directory of the new directory

Parameters:
p_dest - destination directory
p_src - directory to copy
Throws:
java.io.IOException - If an I/O-error occurs.

copyDir

public static void copyDir(java.io.File p_dest,
                           java.io.File p_src,
                           boolean p_bCopyMarkedDirs)
                    throws java.io.IOException
copy a directory recursively.
p_dest is the parent directory of the new directory

Parameters:
p_dest - destination directory
p_src - directory to copy
p_bCopyMarkedDirs - copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created but left empty.
Throws:
java.io.IOException - If an I/O-error occurs.

copyDir

public static void copyDir(java.io.File p_dest,
                           java.io.File p_src,
                           java.io.FileFilter p_filter)
                    throws java.io.IOException
copy a directory recursively. Copy only the files matching the specified file filter.
p_dest is the parent directory of the new directory. There c

Parameters:
p_dest - destination directory
p_src - directory to copy
p_filter - File filter that filters the files that will be copied. If null is specified, no filter applies.
Throws:
java.io.IOException - If an I/O-error occurs.

copyDir

public static void copyDir(java.io.File p_dirDst,
                           java.io.File p_dirSrc,
                           java.io.FileFilter p_filter,
                           boolean p_bCopyMarkedDirs)
                    throws java.io.IOException
copy a directory recursively. Copy only the files matching the specified file filter.
p_dest is the parent directory of the new directory. There c

Parameters:
p_dirDst - destination directory
p_dirSrc - directory to copy
p_filter - File filter that filters the files that will be copied. If null is specified, no filter applies.
p_bCopyMarkedDirs - copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created but left empty.
Throws:
java.io.IOException - If an I/O-error occurs.

clearDir

public static void clearDir(java.io.File p_dir)
Clears a directory recursively but doesn't delete it.

Parameters:
p_dir - The directory to clear.

copyFileOrDirectory

public static void copyFileOrDirectory(java.io.File p_src,
                                       java.io.File p_dest,
                                       java.lang.String p_strItem)
                                throws java.io.IOException
copy a directory from a src dir to a dest dir recursively.

Parameters:
p_src - source directory of the files
p_dest - destination path for the files
p_strItem - file or directory to copy, relative to p_src
Throws:
java.io.IOException

setFilePermissions

public static void setFilePermissions(java.lang.String p_strPath,
                                      int p_iUser,
                                      int p_iGroup,
                                      int p_iOthers,
                                      boolean p_bRecursive)
                               throws java.io.IOException
Set access permissions of a file or directory.
Wildcard characters do not work!!!

Parameters:
p_strPath - path to a file or directory
p_iUser - permissions for the user who owns the files
p_iGroup - permissions for the group
p_iOthers - permissions for all other users
p_bRecursive - change permissions recursively for directories
Throws:
java.io.IOException - error during execution of the external permissions command

setFileOwner

public static void setFileOwner(java.lang.String p_strPath,
                                java.lang.String p_strOwner,
                                boolean p_bRecursive)
                         throws java.io.IOException
Set the owner of a file or directory.
Wildcard characters do not work!!!

Parameters:
p_strPath - path to a file or directory
p_strOwner - owner
p_bRecursive - change permissions recursively for directories
Throws:
java.io.IOException - error during execution of the external permissions command

deleteFile

public static boolean deleteFile(java.io.File p_file)
Same as deleteFileRecursively(File).

See Also:
deleteFileRecursively(File)

deleteFileRecursively

public static boolean deleteFileRecursively(java.io.File p_file)
Delete files recursively.

Returns:
true if successful, or false if the file does not exist or any subdirectory could not be deleted.

clearDirectory

public static boolean clearDirectory(java.io.File p_dir)
Recursively clear the given directory.

The directory itself is not being deleted.

Returns:
true if successful, false if the directory does not exist or any sub directory could not be deleted.

deleteFile

public static boolean deleteFile(java.lang.String p_strFile)
Delete files recursively

Returns:
true if successful

getNormalizedPath

public static java.lang.String getNormalizedPath(java.io.File p_file)
                                          throws java.io.IOException
Get the normalized path of an existing file or directory.

The returned path's file separators are replaced with forward slashes ('/') if the operating system's file separator is not the forward slash.

If p_file denotes a directory the returned path has a trailing forward slash.

Parameters:
p_file - The file.
Returns:
The normalized path.
Throws:
java.io.IOException - - If an I/O error occurred.

getPathRelative

public static java.lang.String getPathRelative(java.io.File p_dir,
                                               java.io.File p_file)
                                        throws java.io.IOException
Get the path of an existing file or directory relative to a given existing directory.

The returned path's file separators are normalized to forward slash ('/') if the operating system's file separator is not the forward slash.

If p_file denotes a directory the returned path, if not null, has a trailing forward slash.

Parameters:
p_dir - The base directory.
p_file - The file for which the path relative to p_dir should be determined.
Returns:
The path of p_file relative to p_dir, or null if p_dir is not a parent of p_file.
Throws:
java.io.IOException - - If an I/O error occurred.

stripFileNumber

public static java.io.File stripFileNumber(java.io.File p_file)
Returns:
the file with removed trailing file number

getFileNumber

public static int getFileNumber(java.io.File p_file)
Returns:
the number at the end of the file name

getUniqueNumberedFile

public static java.io.File getUniqueNumberedFile(java.io.File p_file)
Returns:
A file path base on the given file that does not exist.

getBase64StringFromFile

public static java.lang.String getBase64StringFromFile(java.lang.String p_strFile)
                                                throws java.io.IOException
Returns:
the contents of the given file in a string with base64 encoding
Throws:
java.io.IOException

stringFromBase64ByteArray

public static java.lang.String stringFromBase64ByteArray(java.lang.String p_strBase64,
                                                         java.lang.String p_strCharset)
                                                  throws java.io.IOException
Returns:
the string content of a base64 encoded byte array using the given character set
Throws:
java.io.IOException

getFileBytes

public static byte[] getFileBytes(java.io.File p_file)
                           throws java.io.IOException
Returns:
The file contents as a byte array.
Throws:
java.io.IOException

getFileText

public static java.lang.String getFileText(java.io.File p_file,
                                           java.lang.String p_strEncoding)
                                    throws java.io.IOException
Get the content of a small text file.

Note: Do not load large files as strings!

Parameters:
p_file - The text file.
p_strEncoding - The file encoding.
Returns:
The content of the given text file.
Throws:
java.io.IOException - If an I/O error occurred, or the given file is too big.

writeFileText

public static void writeFileText(java.io.File p_file,
                                 java.lang.String p_strText,
                                 java.lang.String p_strEncoding)
                          throws java.io.IOException
Write the content of a small text file.

Note: Do not write large files as strings!

Parameters:
p_file - The text file.
p_strText - The new text file content.
p_strEncoding - The file encoding.
Throws:
java.io.IOException - If an I/O error occurred.

zipDir

@Deprecated
public static void zipDir(java.io.File p_fileZip,
                                     java.io.File p_dirSrc)
                   throws java.io.IOException
Deprecated. Use ZipHelper.zipDir(File, File) instead.

Throws:
java.io.IOException

zipDir

@Deprecated
public static void zipDir(java.util.zip.ZipOutputStream p_zip,
                                     java.lang.String p_strRootDir,
                                     java.io.File p_dirSrc)
                   throws java.io.IOException
Deprecated. Use ZipHelper.zipDir(ZipOutputStream, String, File) instead.

Throws:
java.io.IOException

zipDir

@Deprecated
public static void zipDir(java.util.zip.ZipOutputStream p_zip,
                                     java.lang.String p_strRootDir,
                                     java.io.File p_dirSrc,
                                     boolean p_bZipMarkedDirs)
                   throws java.io.IOException
Deprecated. Use ZipHelper.zipDir(ZipOutputStream, String, File, boolean, Set) instead.

Throws:
java.io.IOException

unzipDir

@Deprecated
public static void unzipDir(java.util.zip.ZipInputStream p_zip,
                                       java.lang.String p_strPrefixFilter,
                                       java.io.File p_dirDst)
                     throws java.io.IOException
Deprecated. Use ZipHelper.unzipDir(ZipInputStream, String, File) instead.

Throws:
java.io.IOException

unzipDir

@Deprecated
public static void unzipDir(java.io.File p_fileZip,
                                       java.io.File p_dirDst)
                     throws java.io.IOException
Deprecated. Use ZipHelper.unzipDir(File, File) instead.

Throws:
java.io.IOException

unzipDir

@Deprecated
public static void unzipDir(java.util.zip.ZipFile p_zip,
                                       java.lang.String p_strRootDir,
                                       java.io.File p_dirDst)
                     throws java.io.IOException
Deprecated. Use ZipHelper.unzipDir(ZipFile, String, File) instead.

Throws:
java.io.IOException

tarDir

@Deprecated
public static void tarDir(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar,
                                     java.lang.String p_strRootDir,
                                     java.io.File p_dirSrc,
                                     boolean p_bTarMarkedDirs)
                   throws java.io.IOException
Deprecated. Use TarHelper.tarDir(TarArchiveOutputStream, String, File, boolean, Set) instead.

Throws:
java.io.IOException

untarDir

@Deprecated
public static void untarDir(java.io.File p_fileArchive,
                                       java.lang.String p_strRootDir,
                                       java.io.File p_dirDst)
                     throws java.io.IOException
Deprecated. Use TarHelper.untarDir(File, String, File) instead.

Throws:
java.io.IOException

getCRC32

public static long getCRC32(java.io.File p_file)
                     throws java.io.IOException
Computes the CRC-32 of the given file.

Throws:
java.io.IOException

isChild

public static boolean isChild(java.io.File p_dir,
                              java.io.File p_file)
                       throws java.io.IOException
Checks, if the specified file is a child of the specified directory or of its child directories.

Parameters:
p_dir - The directory directory to check for.
p_file - The file to check.
Throws:
java.io.IOException

getFileSize

public static long getFileSize(java.io.File p_file)

createTempDirectory

public static java.io.File createTempDirectory(java.io.File p_dirParent,
                                               java.lang.String p_strPrefix,
                                               java.lang.String p_strSuffix)
                                        throws java.io.IOException
Create a new directory with a unique name.

The caller is responsible for deleting the directory and it's contents.

Parameters:
p_dirParent - The directory where the new temp directory should be created.
p_strPrefix - A name prefix to be used (may be null).
p_strSuffix - A name suffix to be used (may be null).
Returns:
A reference to the newly created directory.
Throws:
java.io.IOException - - If an error occurred.


Last updated: 2011-08-11 10-04
Copyright © 2000-2011 United Planet. All Rights Reserved.