org.opencms.util
Class CmsFileUtil

java.lang.Object
  extended byorg.opencms.util.CmsFileUtil

public final class CmsFileUtil
extends Object

Provides File utility functions.

Since:
6.0.0
Version:
$Revision: 1.21 $
Author:
Alexander Kandzior

Method Summary
static void copy(String fromFile, String toFile)
          Simply version of a 1:1 binary file copy.
static String formatFilesize(long filesize, Locale locale)
          Returns the formatted filesize to Bytes, KB, MB or GB depending on the given value.
static List getFiles(String name, FileFilter filter, boolean includeSubtree)
          Returns a list of all filtered files in the RFS.
static String getResourcePathFromClassloader(String fileName)
          Returns the absolute path name for the given relative path name if it was found by the context Classloader of the current Thread.
static String normalizePath(String path)
          Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path, the path separator char is File.separatorChar.
static String normalizePath(String path, char separatorChar)
          Normalizes a file path that might contain '../' or './' or '//' elements to a normal absolute path.
static void purgeDirectory(File directory)
          Deletes a directory in the file system and all subfolders of that directory.
static byte[] readFile(String filename)
          Reads a file from the RFS and returns the file content.
static String readFile(String filename, String encoding)
          Reads a file from the RFS and converts it to a String with the specified encoding.
static String searchWebInfFolder(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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copy

public static void copy(String fromFile,
                        String toFile)
                 throws 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:
IOException - if any IO error occurs during the copy operation

formatFilesize

public static String formatFilesize(long filesize,
                                    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

getFiles

public static List getFiles(String name,
                            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

getResourcePathFromClassloader

public static String getResourcePathFromClassloader(String fileName)
Returns the absolute path name for the given relative path name if it was found by the context Classloader of the current Thread.

The argument has to denote a resource within the Classloaders scope. A URLClassLoader implementation for example would try to match a given path name to some resource under it's URL entries.

As the result is internally obtained as an URL it is reduced to a file path by the call to URL.getFile(). Therefore the returned String will start with a '/' (no problem for java.io).

Parameters:
fileName - the filename to return the path from the Classloader for
Returns:
the absolute path name for the given relative path name if it was found by the context Classloader of the current Thread or an empty String if it was not found
See Also:
Thread.getContextClassLoader()

normalizePath

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

Parameters:
path - the path to normalize
Returns:
the normalized path

normalizePath

public static String normalizePath(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

purgeDirectory

public static void purgeDirectory(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(String filename)
                       throws IOException
Reads a file from the RFS and returns the file content.

Parameters:
filename - the file to read
Returns:
the read file content
Throws:
IOException - in case of file access errors

readFile

public static String readFile(String filename,
                              String encoding)
                       throws IOException
Reads a file from the RFS 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:
IOException - in case of file access errors

searchWebInfFolder

public static String searchWebInfFolder(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