Package de.uplanet.io

Class IOHelper

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

@Scriptable public final class IOHelper extends Object
IOHelper class utility.
  • Field Details

  • Constructor Details

    • IOHelper

      public IOHelper()
  • Method Details

    • existsFileIgnoreCase

      public static boolean existsFileIgnoreCase(Path p_dir, String p_strFileName) throws IOException
      Case insensitively check if in the given directory exists a file or directory with the given name.

      The search is not recursive over directories.

      If p_dir is a symbolic link the link is followed.

      Note: This operation may be expensive if the directory contains many files.

      Parameters:
      p_dir - The directory to be searched.
      p_strFileName - The name of the file to be searched for.
      Returns:
      true if the file exists, or false otherwise.
      Throws:
      IOException - if one occurs
    • copyStream

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

      Performs buffered copy from one stream to another.

      The destination buffer is being flushed when copying is ready.

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

      public static void copyChannel(FileChannel p_dest, FileChannel p_src, long p_lUnusedParameter) throws IOException
      Throws:
      IOException
    • isDirectoryEmpty

      public static boolean isDirectoryEmpty(File p_dir) throws IOException
      Check if the given directory is empty or not.
      Parameters:
      p_dir - The directory.
      Returns:
      true if the given directory is empty, or false otherwise.
      Throws:
      IOException - If an I/O error occurred.
    • isDirectoryEmpty

      public static boolean isDirectoryEmpty(Path p_dir) throws IOException
      Check if the given directory is empty or not.
      Parameters:
      p_dir - The directory.
      Returns:
      true if the given directory is empty, or false otherwise.
      Throws:
      IOException - If an I/O error occurred.
    • getFile

      public static File getFile(File p_dir, String... p_path)
      Get a file object that represents a file or directory in a sub-path below a given directory.

      Note: The file object is constructed without any checks if the path references an existing file system object or if it is below the given directory. For a security enhanced version see getFileBelow(File, String...).

      Parameters:
      p_dir - The start directory.
      p_path - The sub-path.
      Returns:
      A file object that represents a file or directory in a sub-path below the given directory.
    • getFileBelow

      public static File getFileBelow(String p_strPath)
      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:
      IllegalArgumentException - If p_dir is null, or if p_strPath is null or empty.
      SecurityException - If p_strPath references a file location outside the given directory.
    • isFileBelow

      public static boolean isFileBelow(File p_dir, File p_file) throws 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:
      IOException - If an I/O error occurred.
      IllegalArgumentException - If p_dir is null, or if p_strPath is null or empty.
    • getFileBelow

      public static File getFileBelow(File p_dir, String... p_path)
      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_path - 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:
      IllegalArgumentException - If p_dir is null, or if the directory p_dir does not exist, or if p_path is null or contains empty path elements.
      SecurityException - If p_path references a file location outside the given directory.
    • moveFile

      public static void moveFile(File p_fileDst, File p_fileSrc) throws IOException
      Move a file from one place to another.
      Parameters:
      p_fileDst - The destination file (must not already exist).
      p_fileSrc - The source file.
      Throws:
      FileNotFoundException - If the source file does not exist.
      IOException - If an I/O-error occurs, or if the destination file already exists.
    • copyFile

      public static void copyFile(File p_fileDst, File p_fileSrc) throws IOException
      Performs buffered copy from one file to another.

      Java NIO and Path use:
      see Files#copy(InputStream, Path, java.nio.file.CopyOption...)

      Example: Files.copy(p_fileSrc, p_fileDst, StandardCopyOption.REPLACE_EXISTING)

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

      public static void copyDir(File p_dest, File p_src) throws IOException
      Copy a directory recursively. p_dest is the parent directory of the new directory.
      Parameters:
      p_dest - The destination directory.
      p_src - The directory to copy.
      Throws:
      IOException - If an I/O-error occurs.
    • copyDir

      public static void copyDir(File p_dest, File p_src, boolean p_bCopyMarkedDirs) throws 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:
      IOException - If an I/O-error occurs.
    • copyDir

      public static void copyDir(File p_dest, File p_src, FileFilter p_filter) throws IOException
      Copy a directory recursively. Copy only the files matching the specified file filter.
      p_dest is the parent directory of the new directory.
      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:
      IOException - If an I/O-error occurs.
    • copyDir

      public static void copyDir(File p_dirDst, File p_dirSrc, FileFilter p_filter, boolean p_bCopyMarkedDirs) throws IOException
      Copy a directory recursively. Copy only the files matching the specified file filter.
      p_dest is the parent directory of the new directory.
      Parameters:
      p_dirDst - The destination directory.
      p_dirSrc - The 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 with a .noxexport file as the only content.
      Throws:
      IOException - If an I/O-error occurs.
    • clearDir

      public static void clearDir(File p_dir)
      Clears a directory recursively but doesn't delete it.
      Parameters:
      p_dir - The directory to clear.
    • copyFileOrDirectory

      public static void copyFileOrDirectory(File p_src, File p_dest, String p_strItem) throws IOException
      Copy a directory from a src dir to a dest dir recursively.
      Parameters:
      p_src - The source directory of the files.
      p_dest - The destination path for the files.
      p_strItem - File or directory to copy, relative to p_src.
      Throws:
      IOException
    • setFilePermissions

      public static void setFilePermissions(String p_strPath, int p_iUser, int p_iGroup, int p_iOthers, boolean p_bRecursive) throws 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:
      IOException - error during execution of the external permissions command
    • setFileOwner

      @Deprecated public static void setFileOwner(String p_strPath, String p_strOwner, boolean p_bRecursive) throws IOException
      Deprecated.
      Since 7.0 use version with group name instead
      Set the owner of a file or directory.
      Wildcard characters do not work!!!
      The owner group will not be changed.
      Parameters:
      p_strPath - path to a file or directory
      p_strOwner - owner user
      p_bRecursive - change permissions recursively for directories
      Throws:
      IOException - error during execution of the external permissions command
    • setFileOwner

      public static void setFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive) throws 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 user
      p_strGroup - owner group to set, null if none
      p_bRecursive - change permissions recursively for directories
      Throws:
      IOException - error during execution of the external permissions command
    • setFileOwner

      public static void setFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive, boolean p_bUseSudo) throws 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 user
      p_strGroup - owner group to set, null if none
      p_bRecursive - change permissions recursively for directories
      p_bUseSudo - run chown command as sudo
      Throws:
      IOException - error during execution of the external permissions command
    • deleteFile

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

      Note: do not use this method in scripts.

      See Also:
    • deleteFileRecursively

      public static boolean deleteFileRecursively(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.
    • deleteFileRecursively

      public static boolean deleteFileRecursively(Path p_path)
      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(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(String p_strFile)
      Delete files recursively
      Returns:
      true if successful
    • getNormalizedPath

      public static String getNormalizedPath(File p_file) throws 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:
      IOException - - If an I/O error occurred.
    • getPathRelative

      public static String getPathRelative(File p_dir, File p_file) throws 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:
      IOException - - If an I/O error occurred.
    • stripFileNumber

      public static File stripFileNumber(File p_file)
      Returns:
      the file with removed trailing file number
    • getFileNumber

      public static int getFileNumber(File p_file)
      Returns:
      the number at the end of the file name
    • getUniqueNumberedFile

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

      public static String getBase64StringFromFile(String p_strFile) throws IOException
      Returns:
      the contents of the given file in a string with base64 encoding
      Throws:
      IOException
    • stringFromBase64ByteArray

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

      public static long consumeAll(InputStream p_in) throws IOException
      Read and discard all bytes from the given input stream.
      Parameters:
      p_in - The input stream.
      Returns:
      The number of bytes read.
      Throws:
      IOException
    • getFileBytes

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

      @Deprecated public static byte[] getFileBytes(Path p_file) throws IOException
      Deprecated.
      Returns:
      The file contents as a byte array.
      Throws:
      IOException
    • writeFileBytes

      public static void writeFileBytes(File p_file, byte[] p_bytes) throws IOException
      Write the content of a small file.
      Parameters:
      p_file - The file.
      p_bytes - The bytes to be written.
      Throws:
      IOException - If an I/O error occurred.
    • writeFileBytes

      public static void writeFileBytes(Path p_file, byte[] p_bytes) throws IOException
      Write the content of a small file.
      Parameters:
      p_file - The file.
      p_bytes - The bytes to be written.
      Throws:
      IOException - If an I/O error occurred.
    • getFileText

      public static String getFileText(File p_file, String p_strEncoding) throws 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, or an empty string if the given file has size 0.
      Throws:
      FileNotFoundException - If the file does not exist.
      IOException - If an I/O error occurred, or the given file is too big.
    • getFileText

      public static String getFileText(File p_file, Charset p_encoding) throws IOException
      Get the content of a small text file.

      Note: Do not load large files as strings!

      Parameters:
      p_file - The text file.
      p_encoding - The file encoding.
      Returns:
      The content of the given text file, or an empty string if the given file has size 0.
      Throws:
      FileNotFoundException - If the file does not exist.
      IOException - If an I/O error occurred, or the given file is too big.
    • getFileText

      @Deprecated public static String getFileText(Path p_file, String p_strEncoding) throws IOException
      Throws:
      IOException
    • getFileText

      @Deprecated public static String getFileText(Path p_file, Charset p_encoding) throws IOException
      Deprecated.
      Throws:
      IOException
    • writeFileText

      public static void writeFileText(File p_file, String p_strText, String p_strEncoding) throws 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:
      IOException - If an I/O error occurred.
    • writeFileText

      public static void writeFileText(File p_file, String p_strText, Charset p_encoding) throws 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_encoding - The file encoding.
      Throws:
      IOException - If an I/O error occurred.
    • writeFileText

      public static void writeFileText(Path p_file, String p_strText, String p_strEncoding) throws 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:
      IOException - If an I/O error occurred.
    • writeFileText

      public static void writeFileText(Path p_file, String p_strText, Charset p_encoding) throws 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_encoding - The file encoding.
      Throws:
      IOException - If an I/O error occurred.
    • zipDir

      @Deprecated public static void zipDir(File p_fileZip, File p_dirSrc) throws IOException
      Deprecated.
      Throws:
      IOException
    • zipDir

      @Deprecated public static void zipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc) throws IOException
      Throws:
      IOException
    • zipDir

      @Deprecated public static void zipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc, boolean p_bZipMarkedDirs) throws IOException
      Throws:
      IOException
    • unzipDir

      @Deprecated public static void unzipDir(ZipInputStream p_zip, String p_strPrefixFilter, File p_dirDst)
    • unzipDir

      @Deprecated public static void unzipDir(File p_fileZip, File p_dirDst) throws IOException
      Deprecated.
      Throws:
      IOException
    • unzipDir

      @Deprecated public static void unzipDir(ZipFile p_zip, String p_strRootDir, File p_dirDst) throws IOException
      Throws:
      IOException
    • tarDir

      @Deprecated public static void tarDir(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar, String p_strRootDir, File p_dirSrc, boolean p_bTarMarkedDirs) throws IOException
      Throws:
      IOException
    • untarDir

      @Deprecated public static void untarDir(File p_fileArchive, String p_strRootDir, File p_dirDst) throws IOException
      Throws:
      IOException
    • getCRC32

      public static long getCRC32(File p_file) throws IOException
      Computes the CRC-32 of the given file.
      Throws:
      IOException
    • getCRC32

      public static long getCRC32(InputStream p_is) throws IOException
      Computes the CRC-32 of the given stream.
      Throws:
      IOException
    • isSameFile

      @Deprecated public static boolean isSameFile(File p_file1, File p_file2) throws IOException
      Deprecated.
      Checks if the two given file objects represent the same file in the file system.
      Parameters:
      p_file1 - The first file.
      p_file2 - The second file.
      Returns:
      true if the two file objects represent the same file in the file system, or false otherwise.
      Throws:
      IOException - If an I/O error occurred.
    • isChild

      public static boolean isChild(File p_dir, File p_file) throws 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:
      IOException
    • getFileSize

      public static long getFileSize(File p_file)
    • createTempDirectory

      public static File createTempDirectory(File p_dirParent, String p_strPrefix, String p_strSuffix) throws 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:
      IOException - - If an error occurred.
    • getUniqueFileName

      public static File getUniqueFileName(File p_dirDest, String p_strFilename) throws IOException
      Create a file object with a non existing file name on the destination directory.
      Parameters:
      p_dirDest - The destination directory.
      p_strFilename - The filename.
      Returns:
      The file object with a unique file name.
      Throws:
      IOException - - If an error occurred.
    • cleanupFileName

      public static String cleanupFileName(String p_strFileName)
      Delete all characters from the given file or directory name that are not supported by the current operating system.

      Note: Use with care

      Usually cleaning up file names depends on the actual use case as well as on the environment. This cannot always be accomplished by a common method like this.

      The caller is responsible to check if the result of this method call is empty or not.

      Parameters:
      p_strFileName - The name (i.e. the last part of the path) of the file or directory.
      Returns:
      The clean file name.