Package org.apache.commons.io
Enum FileSystem
- java.lang.Object
-
- java.lang.Enum<FileSystem>
-
- org.apache.commons.io.FileSystem
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<FileSystem>
public enum FileSystem extends java.lang.Enum<FileSystem>
Abstracts an OS' file system details, currently supporting the single use case of converting a file name String to a legal file name withtoLegalFileName(String, char)
.The starting point of any operation is
getCurrent()
which gets you the enum for the file system that matches the OS hosting the running JVM.- Since:
- 2.7
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
casePreserving
private boolean
caseSensitive
private char[]
illegalFileNameChars
private static boolean
IS_OS_LINUX
Istrue
if this is Linux.private static boolean
IS_OS_MAC
Istrue
if this is Mac.private static boolean
IS_OS_WINDOWS
Istrue
if this is Windows.private int
maxFileNameLength
private int
maxPathLength
private static java.lang.String
OS_NAME
private static java.lang.String
OS_NAME_WINDOWS_PREFIX
The prefix String for all Windows OS.private java.lang.String[]
reservedFileNames
-
Constructor Summary
Constructors Modifier Constructor Description private
FileSystem(boolean caseSensitive, boolean casePreserving, int maxFileLength, int maxPathLength, char[] illegalFileNameChars, java.lang.String[] reservedFileNames)
Constructs a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static FileSystem
getCurrent()
Gets the current file system.char[]
getIllegalFileNameChars()
Gets a cloned copy of the illegal characters for this file system.int
getMaxFileNameLength()
Gets the maximum length for file names.int
getMaxPathLength()
Gets the maximum length of the path to a file.private static boolean
getOsMatchesName(java.lang.String osNamePrefix)
Decides if the operating system matches.java.lang.String[]
getReservedFileNames()
Gets a cloned copy of the reserved file names.private static java.lang.String
getSystemProperty(java.lang.String property)
Gets a System property, defaulting tonull
if the property cannot be read.boolean
isCasePreserving()
Whether this file system preserves case.boolean
isCaseSensitive()
Whether this file system is case-sensitive.private boolean
isIllegalFileNameChar(char c)
Returnstrue
if the given character is illegal in a file name,false
otherwise.boolean
isLegalFileName(java.lang.CharSequence candidate)
Checks if a candidate file name (without a path) such as"filename.ext"
or"filename"
is a potentially legal file name.private static boolean
isOsNameMatch(java.lang.String osName, java.lang.String osNamePrefix)
Decides if the operating system matches.boolean
isReservedFileName(java.lang.CharSequence candidate)
Returns whether the given string is a reserved file name.java.lang.String
toLegalFileName(java.lang.String candidate, char replacement)
Converts a candidate file name (without a path) like"filename.ext"
or"filename"
to a legal file name.static FileSystem
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static FileSystem[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
GENERIC
public static final FileSystem GENERIC
Generic file system.
-
LINUX
public static final FileSystem LINUX
Linux file system.
-
MAC_OSX
public static final FileSystem MAC_OSX
MacOS file system.
-
WINDOWS
public static final FileSystem WINDOWS
Windows file system.
-
-
Field Detail
-
IS_OS_LINUX
private static final boolean IS_OS_LINUX
Is
true
if this is Linux.The field will return
false
ifOS_NAME
isnull
.
-
IS_OS_MAC
private static final boolean IS_OS_MAC
Is
true
if this is Mac.The field will return
false
ifOS_NAME
isnull
.
-
OS_NAME_WINDOWS_PREFIX
private static final java.lang.String OS_NAME_WINDOWS_PREFIX
The prefix String for all Windows OS.- See Also:
- Constant Field Values
-
IS_OS_WINDOWS
private static final boolean IS_OS_WINDOWS
Is
true
if this is Windows.The field will return
false
ifOS_NAME
isnull
.
-
OS_NAME
private static final java.lang.String OS_NAME
-
casePreserving
private final boolean casePreserving
-
caseSensitive
private final boolean caseSensitive
-
illegalFileNameChars
private final char[] illegalFileNameChars
-
maxFileNameLength
private final int maxFileNameLength
-
maxPathLength
private final int maxPathLength
-
reservedFileNames
private final java.lang.String[] reservedFileNames
-
-
Constructor Detail
-
FileSystem
private FileSystem(boolean caseSensitive, boolean casePreserving, int maxFileLength, int maxPathLength, char[] illegalFileNameChars, java.lang.String[] reservedFileNames)
Constructs a new instance.- Parameters:
caseSensitive
- Whether this file system is case sensitive.casePreserving
- Whether this file system is case preserving.maxFileLength
- The maximum length for file names. The file name does not include folders.maxPathLength
- The maximum length of the path to a file. This can include folders.illegalFileNameChars
- Illegal characters for this file system.reservedFileNames
- The reserved file names.
-
-
Method Detail
-
values
public static FileSystem[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (FileSystem c : FileSystem.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static FileSystem valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is null
-
getCurrent
public static FileSystem getCurrent()
Gets the current file system.- Returns:
- the current file system
-
getOsMatchesName
private static boolean getOsMatchesName(java.lang.String osNamePrefix)
Decides if the operating system matches.- Parameters:
osNamePrefix
- the prefix for the os name- Returns:
- true if matches, or false if not or can't determine
-
getSystemProperty
private static java.lang.String getSystemProperty(java.lang.String property)
Gets a System property, defaulting to
null
if the property cannot be read.If a
SecurityException
is caught, the return value isnull
and a message is written toSystem.err
.- Parameters:
property
- the system property name- Returns:
- the system property value or
null
if a security problem occurs
-
isOsNameMatch
private static boolean isOsNameMatch(java.lang.String osName, java.lang.String osNamePrefix)
Decides if the operating system matches.This method is package private instead of private to support unit test invocation.
- Parameters:
osName
- the actual OS nameosNamePrefix
- the prefix for the expected OS name- Returns:
- true if matches, or false if not or can't determine
-
getIllegalFileNameChars
public char[] getIllegalFileNameChars()
Gets a cloned copy of the illegal characters for this file system.- Returns:
- the illegal characters for this file system.
-
getMaxFileNameLength
public int getMaxFileNameLength()
Gets the maximum length for file names. The file name does not include folders.- Returns:
- the maximum length for file names.
-
getMaxPathLength
public int getMaxPathLength()
Gets the maximum length of the path to a file. This can include folders.- Returns:
- the maximum length of the path to a file.
-
getReservedFileNames
public java.lang.String[] getReservedFileNames()
Gets a cloned copy of the reserved file names.- Returns:
- the reserved file names.
-
isCasePreserving
public boolean isCasePreserving()
Whether this file system preserves case.- Returns:
- Whether this file system preserves case.
-
isCaseSensitive
public boolean isCaseSensitive()
Whether this file system is case-sensitive.- Returns:
- Whether this file system is case-sensitive.
-
isIllegalFileNameChar
private boolean isIllegalFileNameChar(char c)
Returnstrue
if the given character is illegal in a file name,false
otherwise.- Parameters:
c
- the character to test- Returns:
true
if the given character is illegal in a file name,false
otherwise.
-
isLegalFileName
public boolean isLegalFileName(java.lang.CharSequence candidate)
Checks if a candidate file name (without a path) such as"filename.ext"
or"filename"
is a potentially legal file name. If the file name length exceedsgetMaxFileNameLength()
, or if it contains an illegal character then the check fails.- Parameters:
candidate
- a candidate file name (without a path) like"filename.ext"
or"filename"
- Returns:
true
if the candidate name is legal
-
isReservedFileName
public boolean isReservedFileName(java.lang.CharSequence candidate)
Returns whether the given string is a reserved file name.- Parameters:
candidate
- the string to test- Returns:
true
if the given string is a reserved file name.
-
toLegalFileName
public java.lang.String toLegalFileName(java.lang.String candidate, char replacement)
Converts a candidate file name (without a path) like"filename.ext"
or"filename"
to a legal file name. Illegal characters in the candidate name are replaced by thereplacement
character. If the file name length exceedsgetMaxFileNameLength()
, then the name is truncated togetMaxFileNameLength()
.- Parameters:
candidate
- a candidate file name (without a path) like"filename.ext"
or"filename"
replacement
- Illegal characters in the candidate name are replaced by this character- Returns:
- a String without illegal characters
-
-