Class CmsFileUtil


  • public final class CmsFileUtil
    extends java.lang.Object
    Provides File utility functions.

    Since:
    6.0.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CmsFileUtil.FileWalkState
      Data bean which walkFileSystem passes to its callback.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String addTrailingSeparator​(java.lang.String path)
      Adds a trailing separator to a path if required.
      static void checkResources​(CmsObject cms, java.util.List<java.lang.String> resources)
      Checks if all resources are present.
      static void copy​(java.lang.String fromFile, java.lang.String toFile)
      Simply version of a 1:1 binary file copy.
      static java.lang.String formatFilesize​(long filesize, java.util.Locale locale)
      Returns the formatted filesize to Bytes, KB, MB or GB depending on the given value.
      static java.lang.String formatResourceNames​(CmsRequestContext context, java.util.List<CmsResource> resources)
      Returns a comma separated list of resource paths names, with the site root from the given OpenCms user context removed.
      static java.lang.String getEncoding​(CmsObject cms, CmsResource file)
      Returns the encoding of the file.
      static java.lang.String getExtension​(java.lang.String resourceName)
      Returns the extension of the given resource name, that is the part behind the last '.' char, converted to lower case letters.
      static java.lang.String getFileExtension​(java.lang.String filename)
      Deprecated.
      use getExtension(String) instead, it is better implemented
      static java.util.List<java.io.File> getFiles​(java.lang.String name, java.io.FileFilter filter, boolean includeSubtree)
      Returns a list of all filtered files in the RFS.
      static java.lang.String getRepositoryName​(java.lang.String repository, java.lang.String vfspath, boolean online)
      Returns the file name for a given VFS name that has to be written to a repository in the "real" file system, by appending the VFS root path to the given base repository path, also adding an folder for the "online" or "offline" project.
      static java.lang.String getRfsPath​(java.lang.String filename, java.lang.String extension, java.lang.String parameters)
      Creates unique, valid RFS name for the given filename that contains a coded version of the given parameters, with the given file extension appended.
      static java.lang.String normalizePath​(java.lang.String path)
      Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path, the path separator char used is File.separatorChar.
      static java.lang.String normalizePath​(java.lang.String path, char separatorChar)
      Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path.
      static java.lang.String normalizePath​(java.net.URL url)
      Returns the normalized file path created from the given URL.
      static java.lang.String normalizePath​(java.net.URL url, char separatorChar)
      Returns the normalized file path created from the given URL.
      static void purgeDirectory​(java.io.File directory)
      Deletes a directory in the file system and all subfolders of that directory.
      static byte[] readFile​(java.io.File file)
      Reads a file from the RFS and returns the file content.
      static byte[] readFile​(java.lang.String filename)
      Reads a file with the given name from the class loader and returns the file content.
      static java.lang.String readFile​(java.lang.String filename, java.lang.String encoding)
      Reads a file from the class loader and converts it to a String with the specified encoding.
      static byte[] readFully​(java.io.InputStream in)
      Reads all bytes from the given input stream, closes it and returns the result in an array.
      static byte[] readFully​(java.io.InputStream in, boolean closeInputStream)
      Reads all bytes from the given input stream, conditionally closes the given input stream and returns the result in an array.
      static byte[] readFully​(java.io.InputStream in, int size)
      Reads the specified number of bytes from the given input stream and returns the result in an array.
      static byte[] readFully​(java.io.InputStream in, int size, boolean closeStream)
      Reads the specified number of bytes from the given input stream, conditionally closes the stream and returns the result in an array.
      static java.lang.String removeLeadingSeparator​(java.lang.String path)
      Removes a leading separator from a path if required.
      static java.util.List<java.lang.String> removeRedundancies​(java.util.List<java.lang.String> resourcenames)
      Removes all resource names in the given List that are "redundant" because the parent folder name is also contained in the List.
      static java.util.List<CmsResource> removeRedundantResources​(java.util.List<CmsResource> resources)
      Removes all resources in the given List that are "redundant" because the parent folder is also contained in the List.
      static java.lang.String removeTrailingSeparator​(java.lang.String path)
      Removes a trailing separator from a path if required.
      static java.lang.String searchWebInfFolder​(java.lang.String startFolder)
      Searches for the OpenCms web application 'WEB-INF' folder during system startup, code or null if the 'WEB-INF' folder can not be found.
      static java.lang.String toggleTrailingSeparator​(java.lang.String path)  
      static void walkFileSystem​(java.io.File base, org.apache.commons.collections.Closure action)
      Traverses the file system starting from a base folder and executes a callback for every directory found.
      • Methods inherited from class java.lang.Object

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

      • addTrailingSeparator

        public static java.lang.String addTrailingSeparator​(java.lang.String path)
        Adds a trailing separator to a path if required.

        Parameters:
        path - the path to add the trailing separator to
        Returns:
        the path with a trailing separator
      • checkResources

        public static void checkResources​(CmsObject cms,
                                          java.util.List<java.lang.String> resources)
                                   throws CmsIllegalArgumentException
        Checks if all resources are present.

        Parameters:
        cms - an initialized OpenCms user context which must have read access to all resources
        resources - a list of vfs resource names to check
        Throws:
        CmsIllegalArgumentException - in case not all resources exist or can be read with the given OpenCms user context
      • copy

        public static void copy​(java.lang.String fromFile,
                                java.lang.String toFile)
                         throws java.io.IOException
        Simply version of a 1:1 binary file copy.

        Parameters:
        fromFile - the name of the file to copy
        toFile - the name of the target file
        Throws:
        java.io.IOException - if any IO error occurs during the copy operation
      • formatFilesize

        public static java.lang.String formatFilesize​(long filesize,
                                                      java.util.Locale locale)
        Returns the formatted filesize to Bytes, KB, MB or GB depending on the given value.

        Parameters:
        filesize - in bytes
        locale - the locale of the current OpenCms user or the System's default locale if the first choice is not at hand.
        Returns:
        the formatted filesize to Bytes, KB, MB or GB depending on the given value
      • formatResourceNames

        public static java.lang.String formatResourceNames​(CmsRequestContext context,
                                                           java.util.List<CmsResource> resources)
        Returns a comma separated list of resource paths names, with the site root from the given OpenCms user context removed.

        Parameters:
        context - the current users OpenCms context (optional, may be null)
        resources - a List of CmsResource instances to get the names from
        Returns:
        a comma separated list of resource paths names
      • getEncoding

        public static java.lang.String getEncoding​(CmsObject cms,
                                                   CmsResource file)
        Returns the encoding of the file. Encoding is read from the content-encoding property and defaults to the systems default encoding. Since properties can change without rewriting content, the actual encoding can differ.
        Parameters:
        cms - CmsObject used to read properties of the given file.
        file - the file for which the encoding is requested
        Returns:
        the file's encoding according to the content-encoding property, or the system's default encoding as default.
      • getExtension

        public static java.lang.String getExtension​(java.lang.String resourceName)
        Returns the extension of the given resource name, that is the part behind the last '.' char, converted to lower case letters.

        The extension of a file is the part of the name after the last dot, including the dot. The extension of a folder is empty. All extensions are returned as lower case

        Please note: No check is performed to ensure the given file name is not null.

        Examples:

        • /folder.test/ has an empty extension.
        • /folder.test/config has an empty extension.
        • /strange.filename. has an empty extension.
        • /document.PDF has the extension .pdf.
        Parameters:
        resourceName - the resource to get the extension for
        Returns:
        the extension of a resource
      • getFileExtension

        @Deprecated
        public static java.lang.String getFileExtension​(java.lang.String filename)
        Deprecated.
        use getExtension(String) instead, it is better implemented
        Returns the extension of the given file name, that is the part behind the last '.' char, converted to lower case letters.

        The result does contain the '.' char. For example, if the input is "opencms.html", then the result will be ".html".

        If the given file name does not contain a '.' char, the empty String "" is returned.

        Please note: No check is performed to ensure the given file name is not null.

        Parameters:
        filename - the file name to get the extension for
        Returns:
        the extension of the given file name
      • getFiles

        public static java.util.List<java.io.File> getFiles​(java.lang.String name,
                                                            java.io.FileFilter filter,
                                                            boolean includeSubtree)
        Returns a list of all filtered files in the RFS.

        If the name is not a folder the folder that contains the given file will be used instead.

        Despite the filter may not accept folders, every subfolder is traversed if the includeSubtree parameter is set.

        Parameters:
        name - a folder or file name
        filter - a filter
        includeSubtree - if to include subfolders
        Returns:
        a list of filtered File objects
      • getRepositoryName

        public static java.lang.String getRepositoryName​(java.lang.String repository,
                                                         java.lang.String vfspath,
                                                         boolean online)
        Returns the file name for a given VFS name that has to be written to a repository in the "real" file system, by appending the VFS root path to the given base repository path, also adding an folder for the "online" or "offline" project.

        Parameters:
        repository - the base repository path
        vfspath - the VFS root path to write to use
        online - flag indicates if the result should be used for the online project (true) or not
        Returns:
        The full uri to the JSP
      • getRfsPath

        public static java.lang.String getRfsPath​(java.lang.String filename,
                                                  java.lang.String extension,
                                                  java.lang.String parameters)
        Creates unique, valid RFS name for the given filename that contains a coded version of the given parameters, with the given file extension appended.

        This is used to create file names for the static export, or in a vfs disk cache.

        Parameters:
        filename - the base file name
        extension - the extension to use
        parameters - the parameters to code in the result file name
        Returns:
        a unique, valid RFS name for the given parameters
        See Also:
        CmsStaticExportManager
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path)
        Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path, the path separator char used is File.separatorChar.

        Parameters:
        path - the path to normalize
        Returns:
        the normalized path
        See Also:
        normalizePath(String, char)
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path,
                                                     char separatorChar)
        Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path.

        Can also handle Windows like path information containing a drive letter, like C:\path\..\.

        Parameters:
        path - the path to normalize
        separatorChar - the file separator char to use, for example File.separatorChar
        Returns:
        the normalized path
      • normalizePath

        public static java.lang.String normalizePath​(java.net.URL url)
        Returns the normalized file path created from the given URL.

        The path part URL.getPath() is used, unescaped and normalized using normalizePath(String, char) using File.separatorChar.

        Parameters:
        url - the URL to extract the path information from
        Returns:
        the normalized file path created from the given URL using File.separatorChar
        See Also:
        normalizePath(URL, char)
      • normalizePath

        public static java.lang.String normalizePath​(java.net.URL url,
                                                     char separatorChar)
        Returns the normalized file path created from the given URL.

        The path part URL.getPath() is used, unescaped and normalized using normalizePath(String, char).

        Parameters:
        url - the URL to extract the path information from
        separatorChar - the file separator char to use, for example File.separatorChar
        Returns:
        the normalized file path created from the given URL
      • purgeDirectory

        public static void purgeDirectory​(java.io.File directory)
        Deletes a directory in the file system and all subfolders of that directory.

        Parameters:
        directory - the directory to delete
      • readFile

        public static byte[] readFile​(java.io.File file)
                               throws java.io.IOException
        Reads a file from the RFS and returns the file content.

        Parameters:
        file - the file to read
        Returns:
        the read file content
        Throws:
        java.io.IOException - in case of file access errors
      • readFile

        public static byte[] readFile​(java.lang.String filename)
                               throws java.io.IOException
        Reads a file with the given name from the class loader and returns the file content.

        Parameters:
        filename - the file to read
        Returns:
        the read file content
        Throws:
        java.io.IOException - in case of file access errors
      • readFile

        public static java.lang.String readFile​(java.lang.String filename,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Reads a file from the class loader and converts it to a String with the specified encoding.

        Parameters:
        filename - the file to read
        encoding - the encoding to use when converting the file content to a String
        Returns:
        the read file convered to a String
        Throws:
        java.io.IOException - in case of file access errors
      • readFully

        public static byte[] readFully​(java.io.InputStream in)
                                throws java.io.IOException
        Reads all bytes from the given input stream, closes it and returns the result in an array.

        Parameters:
        in - the input stream to read the bytes from
        Returns:
        the byte content of the input stream
        Throws:
        java.io.IOException - in case of errors in the underlying java.io methods used
      • readFully

        public static byte[] readFully​(java.io.InputStream in,
                                       boolean closeInputStream)
                                throws java.io.IOException
        Reads all bytes from the given input stream, conditionally closes the given input stream and returns the result in an array.

        Parameters:
        in - the input stream to read the bytes from
        closeInputStream - if true the given stream will be closed afterwards
        Returns:
        the byte content of the input stream
        Throws:
        java.io.IOException - in case of errors in the underlying java.io methods used
      • readFully

        public static byte[] readFully​(java.io.InputStream in,
                                       int size)
                                throws java.io.IOException
        Reads the specified number of bytes from the given input stream and returns the result in an array.

        Parameters:
        in - the input stream to read the bytes from
        size - the number of bytes to read
        Returns:
        the byte content read from the input stream
        Throws:
        java.io.IOException - in case of errors in the underlying java.io methods used
      • readFully

        public static byte[] readFully​(java.io.InputStream in,
                                       int size,
                                       boolean closeStream)
                                throws java.io.IOException
        Reads the specified number of bytes from the given input stream, conditionally closes the stream and returns the result in an array.

        Parameters:
        in - the input stream to read the bytes from
        size - the number of bytes to read
        closeStream - if true the given stream will be closed
        Returns:
        the byte content read from the input stream
        Throws:
        java.io.IOException - in case of errors in the underlying java.io methods used
      • removeLeadingSeparator

        public static java.lang.String removeLeadingSeparator​(java.lang.String path)
        Removes a leading separator from a path if required.

        Parameters:
        path - the path to remove the leading separator from
        Returns:
        the path without a trailing separator
      • removeRedundancies

        public static java.util.List<java.lang.String> removeRedundancies​(java.util.List<java.lang.String> resourcenames)
        Removes all resource names in the given List that are "redundant" because the parent folder name is also contained in the List.

        The content of the input list is not modified.

        Parameters:
        resourcenames - a list of VFS pathnames to check for redundencies (Strings)
        Returns:
        a new list with all redundancies removed
        See Also:
        removeRedundantResources(List)
      • removeRedundantResources

        public static java.util.List<CmsResourceremoveRedundantResources​(java.util.List<CmsResource> resources)
        Removes all resources in the given List that are "redundant" because the parent folder is also contained in the List.

        The content of the input list is not modified.

        Parameters:
        resources - a list of CmsResource objects to check for redundancies
        Returns:
        a the given list with all redundancies removed
        See Also:
        removeRedundancies(List)
      • removeTrailingSeparator

        public static java.lang.String removeTrailingSeparator​(java.lang.String path)
        Removes a trailing separator from a path if required.

        In case we have the root folder "/", the separator is not removed.

        Parameters:
        path - the path to remove the trailing separator from
        Returns:
        the path without a trailing separator
      • searchWebInfFolder

        public static java.lang.String searchWebInfFolder​(java.lang.String startFolder)
        Searches for the OpenCms web application 'WEB-INF' folder during system startup, code or null if the 'WEB-INF' folder can not be found.

        Parameters:
        startFolder - the folder where to start searching
        Returns:
        String the path of the 'WEB-INF' folder in the 'real' file system, or null
      • walkFileSystem

        public static void walkFileSystem​(java.io.File base,
                                          org.apache.commons.collections.Closure action)
        Traverses the file system starting from a base folder and executes a callback for every directory found.

        Parameters:
        base - the base folder
        action - a callback which will be passed a FileWalkState object for every directory encountered