@Immutable public final class FilenameHelper extends Object
| Modifier and Type | Field and Description |
|---|---|
static char |
EXTENSION_SEPARATOR
The file extension separation character.
|
static char |
HIDDEN_FILE_PREFIX
The prefix used for Unix hidden files
|
static char |
ILLEGAL_FILENAME_CHAR_REPLACEMENT
The replacement character used for illegal file name characters.
|
static String |
PATH_CURRENT
Special name of the current path
|
static String |
PATH_PARENT
Special name of the parent path
|
static char |
UNIX_SEPARATOR
The Unix path separator character.
|
static String |
UNIX_SEPARATOR_STR
The Unix path separator string.
|
static String |
UNIX_UNC_PREFIX
The prefix to identify UNC paths on Unix based systems
|
static char |
WINDOWS_SEPARATOR
The Windows separator character.
|
static String |
WINDOWS_SEPARATOR_STR
The Windows separator string.
|
static String |
WINDOWS_UNC_PREFIX
The prefix to identify UNC paths on Windows based systems
|
static String |
WINDOWS_UNC_PREFIX_LOCAL1
The prefix to identify local UNC paths on Windows based systems
|
static String |
WINDOWS_UNC_PREFIX_LOCAL2
The prefix to identify local UNC paths on Windows based systems
|
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsPathSeparatorChar(String s)
Check if the passed String contains at least one path separator char
(either Windows or Unix style).
|
static boolean |
endsWithPathSeparatorChar(CharSequence s)
Check if the passed character sequence ends with a path separation
character.
|
static String |
ensurePathEndingWithoutSeparator(String sPath)
Ensure that the passed path does NOT end with a directory separator
character.
|
static String |
ensurePathEndingWithSeparator(String sPath)
Ensure that the passed path ends with a directory separator character.
|
static String |
ensurePathStartingWithSeparator(String sPath)
Ensure that the passed path starts with a directory separator character.
|
static String |
getAbsoluteWithEnsuredParentDirectory(File aParentDirectory,
String sFilePath)
Get a concatenated absolute path consisting of the parent directory and the
file path.
|
static String |
getAsSecureValidASCIIFilename(String sFilename)
Replace all non-ASCII characters from the filename (e.g.
|
static String |
getAsSecureValidASCIIFilename(String sFilename,
char cReplacementChar)
Replace all non-ASCII characters from the filename (e.g.
|
static String |
getAsSecureValidFilename(String sFilename)
Convert the passed filename into a valid filename by performing the
following actions:
Remove everything after a potential \0 character
Remove all characters that are invalid at the end of a file name
Replace all characters that are invalid inside a filename with a
underscore
If the filename is invalid on Windows platforms it is prefixed with an
underscore.
|
static String |
getBaseName(File aFile)
Get the passed filename without path and without extension.
|
static String |
getBaseName(String sAbsoluteFilename)
Get the passed filename without path and without extension.
|
static String |
getCleanConcatenatedUrlPath(String sURL,
String sPath)
Concatenate a base URL and a sub path incl. the path cleansing.
|
static String |
getCleanPath(File aFile)
Get a clean path of the passed file resolving all "." and ".." paths.
|
static String |
getCleanPath(String sPath)
Clean the path by removing all ".." and "." path elements.
|
static String |
getExtension(File aFile)
Get the extension of the passed file.
|
static String |
getExtension(String sFilename)
Get the extension of the passed filename.
|
static int |
getIndexOfExtension(String sFilename)
Returns the index of the last extension separator character, which is a
dot.
|
static int |
getIndexOfLastSeparator(String sFilename)
Returns the index of the last directory separator character.
|
static String |
getPath(String sAbsoluteFilename)
Get the path of the passed file name without any eventually contained
filename.
|
static String |
getPathUsingUnixSeparator(File aFile)
Ensure that the path (not the absolute path!)
|
static String |
getPathUsingUnixSeparator(String sAbsoluteFilename)
Ensure that the passed path is using the Unix style separator "/" instead
of the Operating System dependent one.
|
static String |
getPathUsingWindowsSeparator(File aFile)
Ensure that the path (not the absolute path!)
|
static String |
getPathUsingWindowsSeparator(String sAbsoluteFilename)
Ensure that the passed path is using the Windows style separator "\"
instead of the Operating System dependent one.
|
static String |
getRelativeToParentDirectory(File aFile,
File aParentDirectory)
Tries to express the passed file path relative to the passed parent
directory.
|
static String |
getSecureFilename(String sFilename)
Avoid 0 byte attack.
|
static String |
getWithoutExtension(File aFile)
Get the name of the passed file without the extension.
|
static String |
getWithoutExtension(String sFilename)
Get the passed filename without the extension.
|
static String |
getWithoutPath(File aFile)
Get the name of the passed file without any eventually leading path.
|
static String |
getWithoutPath(String sAbsoluteFilename)
Get the name of the passed file without any eventually leading path.
|
static boolean |
hasExtension(File aFile,
String... aExtensions)
Check if the passed file has one of the passed extensions.
|
static boolean |
hasExtension(String sFilename,
String... aExtensions)
Check if the passed filename has one of the passed extensions.
|
static boolean |
isEqualIgnoreFileSeparator(String sAbsoluteFilename1,
String sAbsoluteFilename2)
Check whether the two passed file names are equal, independent of the used
separators (/ or \).
|
static boolean |
isHiddenFilename(File aFile)
Check if the passed filename is a Unix hidden filename.
|
static boolean |
isHiddenFilename(String sFilename)
Check if the passed filename is a Unix hidden filename.
|
static boolean |
isPathSeparatorChar(char c)
Check if the passed character is a path separation character.
|
static boolean |
isSecureFilenameCharacter(char c)
Check if the passed character is secure to be used in filenames.
|
static boolean |
isSystemInternalDirectory(CharSequence s)
Check if the passed string is a system directory.
|
static boolean |
isSystemInternalDirectory(File aFile)
Check if the passed file is a system directory.
|
static boolean |
isSystemInternalDirectory(Path aFile)
Check if the passed file is a system directory.
|
static boolean |
isUNCPath(File aFile)
Check if the passed file is an UNC path.
|
static boolean |
isUNCPath(String sFilename)
Check if the passed file is an UNC path.
|
static boolean |
isValidFilename(String sFilename)
Check if the passed file name is valid.
|
static boolean |
isValidFilenameWithPaths(String sFilename)
Check if the passed filename path is valid.
|
static boolean |
isWindowsLocalUNCPath(File aFile)
Check if the passed file is a Windows local UNC path.
|
static boolean |
isWindowsLocalUNCPath(String sFilename)
Check if the passed file is a Windows local UNC path.
|
static boolean |
startsWithPathSeparatorChar(CharSequence s)
Check if the passed character sequence starts with a path separation
character.
|
public static final char EXTENSION_SEPARATOR
public static final char ILLEGAL_FILENAME_CHAR_REPLACEMENT
public static final String PATH_CURRENT
public static final String PATH_PARENT
public static final char UNIX_SEPARATOR
public static final String UNIX_SEPARATOR_STR
public static final char WINDOWS_SEPARATOR
public static final String WINDOWS_SEPARATOR_STR
public static final String UNIX_UNC_PREFIX
public static final String WINDOWS_UNC_PREFIX
public static final String WINDOWS_UNC_PREFIX_LOCAL1
public static final String WINDOWS_UNC_PREFIX_LOCAL2
public static final char HIDDEN_FILE_PREFIX
public static int getIndexOfExtension(@Nullable String sFilename)
This method also checks that there is no directory separator after the last
dot. To do this it uses getIndexOfLastSeparator(String) which will
handle a file in either Unix or Windows format.
The output will be the same irrespective of the machine that the code is running on.
sFilename - The filename to find the last path separator in. May be
null.CGlobal.ILLEGAL_UINT if there is no such character or the
input parameter is null.getIndexOfLastSeparator(String)@Nullable public static String getWithoutExtension(@Nullable File aFile)
aFile - The file to extract the extension from. May be null.null
the return value is null.getWithoutExtension(String)@Nullable public static String getWithoutExtension(@Nullable String sFilename)
sFilename - The filename to extract the extension from. May be null
or empty.null
the return value is null.getIndexOfExtension(String)@Nonnull public static String getExtension(@Nullable File aFile)
aFile - The file to extract the extension from. May be null.null.getExtension(String)@Nonnull public static String getExtension(@Nullable String sFilename)
sFilename - The filename to extract the extension from. May be null
or empty.null.getIndexOfExtension(String)public static boolean hasExtension(@Nullable File aFile, @Nonnull String... aExtensions)
aFile - The file to check the extension from. May be null or
empty.aExtensions - An array of extensions (without the leading dot) which are matched
case insensitive. May not be null.true if the file has one of the passed extensions,
else false.getExtension(File)public static boolean hasExtension(@Nullable String sFilename, @Nonnull String... aExtensions)
sFilename - The filename to check the extension from. May be null
or empty.aExtensions - An array of extensions (without the leading dot) which are matched
case insensitive. May not be null.true if the filename has one of the passed extensions,
else false.getExtension(String)public static int getIndexOfLastSeparator(@Nullable String sFilename)
sFilename - The filename to find the last path separator in, null
returns CGlobal.ILLEGAL_UINT.CGlobal.ILLEGAL_UINT if there is no such character@Nullable public static String getWithoutPath(@Nullable File aFile)
aFile - The file. May be null.null if the passed parameter is
null.@Nullable public static String getWithoutPath(@Nullable String sAbsoluteFilename)
sAbsoluteFilename - The fully qualified file name. May be null.null if the passed parameter is
null.getIndexOfLastSeparator(String)@Nullable public static String getPath(@Nullable String sAbsoluteFilename)
sAbsoluteFilename - The fully qualified file name. May be null.null if the passed parameter is
null.getIndexOfLastSeparator(String)@Nullable public static String getBaseName(@Nullable File aFile)
/dir1/dir2/file.txt becomes fileaFile - The file to get the base name from. May be null.null if
the parameter was null.getWithoutExtension(String)@Nullable public static String getBaseName(@Nullable String sAbsoluteFilename)
/dir1/dir2/file.txt becomes filesAbsoluteFilename - The filename to get the base name from. May be null.null if
the parameter was null.getWithoutPath(String),
getWithoutExtension(String)@Nullable public static String getPathUsingUnixSeparator(@Nullable File aFile)
aFile - The file to use. May be nullnull if the passed file is null.getPathUsingUnixSeparator(String)@Nullable public static String getPathUsingUnixSeparator(@Nullable String sAbsoluteFilename)
sAbsoluteFilename - The file name to use. May be nullnull if the passed path is null.getPathUsingUnixSeparator(File)@Nullable public static String getPathUsingWindowsSeparator(@Nullable File aFile)
aFile - The file to use. May be nullnull if the passed file is null.getPathUsingWindowsSeparator(String)@Nullable public static String getPathUsingWindowsSeparator(@Nullable String sAbsoluteFilename)
sAbsoluteFilename - The file name to use. May be nullnull if the passed path is null.getPathUsingWindowsSeparator(File)public static boolean isEqualIgnoreFileSeparator(@Nullable String sAbsoluteFilename1, @Nullable String sAbsoluteFilename2)
sAbsoluteFilename1 - First file name. May be null.sAbsoluteFilename2 - Second file name. May be null.true if they are equal, false otherwise.getPathUsingUnixSeparator(String)@Nullable public static String getSecureFilename(@Nullable String sFilename)
sFilename - The file name to check. May be null.null if the input string is null or
everything up to the 0-byte.public static boolean isValidFilename(@Nullable String sFilename)
isValidFilenameWithPaths(String).sFilename - The filename to check. May be null.false if the passed filename is null or
empty or invalid. true if the filename is not empty
and valid.containsPathSeparatorChar(String)public static boolean isValidFilenameWithPaths(@Nullable String sFilename)
isValidFilename(String) this method can also handle filenames
including paths.sFilename - The filename to be checked for validity.true if all path elements of the filename are valid,
false if at least one element is invalidisValidFilename(String)@Nullable @Nonempty public static String getAsSecureValidFilename(@Nullable String sFilename)
sFilename - The filename to be made value. May be null.null if the input filename was null or if
it consisted only of characters invalid for a filename; the
potentially modified filename otherwise but never an empy
string.getSecureFilename(String)public static boolean isSecureFilenameCharacter(char c)
c - The character to checktrue if it is valid, false if not@Nullable @Nonempty public static String getAsSecureValidASCIIFilename(@Nullable String sFilename)
getAsSecureValidFilename(String).sFilename - Input file name. May not be null.null if the input filename was null. The
file name containing only ASCII characters. The returned value is
never an empty String.getAsSecureValidASCIIFilename(String, char)@Nullable @Nonempty public static String getAsSecureValidASCIIFilename(@Nullable String sFilename, char cReplacementChar)
getAsSecureValidFilename(String).sFilename - Input file name. May not be null.cReplacementChar - The replacement character to be used for insecure filenames.null if the input filename was null. The
file name containing only ASCII characters. The returned value is
never an empty String.getAsSecureValidASCIIFilename(String),
getAsSecureValidFilename(String),
isSecureFilenameCharacter(char)public static boolean isPathSeparatorChar(char c)
c - The character to check.true if the character is a path separation character,
false otherwise.public static boolean startsWithPathSeparatorChar(@Nullable CharSequence s)
s - The character sequence to check. May be null or empty.true if the character sequences starts with a Windows-
or Unix-style path character.isPathSeparatorChar(char)public static boolean endsWithPathSeparatorChar(@Nullable CharSequence s)
s - The character sequence to check. May be null or empty.true if the character sequences ends with a Windows-
or Unix-style path character.isPathSeparatorChar(char)public static boolean containsPathSeparatorChar(@Nullable String s)
s - The string to check. May be null.true if the passed string is not null and
contains at least one separator.public static boolean isSystemInternalDirectory(@Nullable File aFile)
aFile - The file to be checked. May be null.true if the passed file name (not the path) matches
any of the special directory names, false of the
passed file is null or does not denote a special
directory.isSystemInternalDirectory(CharSequence)public static boolean isSystemInternalDirectory(@Nullable Path aFile)
aFile - The file to be checked. May be null.true if the passed file name (not the path) matches
any of the special directory names, false of the
passed file is null or does not denote a special
directory.isSystemInternalDirectory(CharSequence)public static boolean isSystemInternalDirectory(@Nullable CharSequence s)
s - The value to be checked. May be null.true if the passed string matches any of the special
directory names, false of the passed string is
null or does not denote a special directory.isSystemInternalDirectory(File)public static boolean isUNCPath(@Nonnull File aFile)
aFile - The file to be checked. May not be null.true if the file points to an UNC path,
false if not.isUNCPath(String)public static boolean isUNCPath(@Nonnull String sFilename)
sFilename - The absolute filename to be checked. May not be null.true if the file points to an UNC path,
false if not.isUNCPath(File)public static boolean isWindowsLocalUNCPath(@Nonnull File aFile)
aFile - The file to be checked. May not be null.true if the file points to an UNC path,
false if not.isWindowsLocalUNCPath(String)public static boolean isWindowsLocalUNCPath(@Nonnull String sFilename)
sFilename - The absolute filename to be checked. May not be null.true if the file points to a Windows local UNC path,
false if not.isWindowsLocalUNCPath(File)@Nonnull public static String getCleanPath(@Nonnull File aFile)
FileHelper.getCanonicalPath(File) fails,
getCleanPath(String) is used as a fallback.aFile - The file to be cleaned. May not be null.null.getPathUsingUnixSeparator(String),
getCleanPath(String)@Nullable public static String getCleanPath(@Nullable String sPath)
sPath - The path to be cleaned.null if the input parameter was
null.getSecureFilename(String),
getPathUsingUnixSeparator(String)@Nonnull public static String getCleanConcatenatedUrlPath(@Nonnull String sURL, @Nonnull String sPath)
getCleanPath (sURL + "/" + sPath)sURL - The base URL. May not be null.sPath - The path to append. May not be null.getCleanPath(String)@Nullable @CheckReturnValue public static String ensurePathStartingWithSeparator(@Nullable String sPath)
sPath - The path to be checked.startsWithPathSeparatorChar(CharSequence)@Nullable @CheckReturnValue public static String ensurePathEndingWithoutSeparator(@Nullable String sPath)
sPath - The path to be checked.endsWithPathSeparatorChar(CharSequence)@Nullable @CheckReturnValue public static String ensurePathEndingWithSeparator(@Nullable String sPath)
sPath - The path to be checked.endsWithPathSeparatorChar(CharSequence)@Nullable public static String getRelativeToParentDirectory(@Nonnull File aFile, @Nullable File aParentDirectory)
aFile - The file which is to be described relatively. May not be
null.aParentDirectory - The parent directory of the file to which the relative path
expression will relate to. May be null.null if the passed file contains a path traversal at
the beginninggetCleanPath(File),
startsWithPathSeparatorChar(CharSequence)@Nullable public static String getAbsoluteWithEnsuredParentDirectory(@Nonnull File aParentDirectory, @Nonnull String sFilePath)
null is returned.aParentDirectory - The parent directory to be ensured. May not be null.sFilePath - The file path to be appended to the passed parent directory. May not
be null.null if the parent directory would be changed with the
passed file path - the concatenated cleaned path otherwise (using
Unix separators).getRelativeToParentDirectory(File, File),
getCleanPath(File)public static boolean isHiddenFilename(@Nullable File aFile)
aFile - The file to check. May be null.true if the file is not null and the name
starts with a dot.isHiddenFilename(String)public static boolean isHiddenFilename(@Nullable String sFilename)
sFilename - The filename to check. May be null.true if the filename is neither null nor
empty and starts with a dot.isHiddenFilename(File)Copyright © 2014–2017 Philip Helger. All rights reserved.