Enum FileSystem
- All Implemented Interfaces:
Serializable
,Comparable<FileSystem>
toLegalFileName(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
-
Enum Constant Summary
Enum Constants -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
private final boolean
private final boolean
private static final FileSystem
The current FileSystem.private final int[]
private static final boolean
Istrue
if this is Linux.private static final boolean
Istrue
if this is Mac.private static final boolean
Istrue
if this is Windows.private final int
private final int
private final char
private final char
private static final String
The prefix String for all Windows OS.private final String[]
private final boolean
private final boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
FileSystem
(int blockSize, boolean caseSensitive, boolean casePreserving, int maxFileLength, int maxPathLength, int[] illegalFileNameChars, String[] reservedFileNames, boolean reservedFileNamesExtensions, boolean supportsDriveLetter, char nameSeparator) Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static FileSystem
current()
Gets the current file system.int
Gets the file allocation block size in bytes.static FileSystem
Gets the current file system.char[]
Gets a cloned copy of the illegal characters for this file system.int[]
Gets a cloned copy of the illegal code points for this file system.int
Gets the maximum length for file names.int
Gets the maximum length of the path to a file.char
Gets the name separator, '\\' on Windows, '/' on Linux.private static boolean
getOsMatchesName
(String osNamePrefix) Decides if the operating system matches.String[]
Gets a cloned copy of the reserved file names.private static String
getSystemProperty
(String property) Gets a System property, defaulting tonull
if the property cannot be read.private static int
indexOf
(CharSequence cs, int searchChar, int start) Copied from Apache Commons Lang CharSequenceUtils.boolean
Tests whether this file system preserves case.boolean
Tests whether this file system is case-sensitive.private boolean
isIllegalFileNameChar
(int c) Tests if the given character is illegal in a file name,false
otherwise.boolean
isLegalFileName
(CharSequence candidate) Tests if a candidate file name (without a path) such as"filename.ext"
or"filename"
is a potentially legal file name.private static boolean
isOsNameMatch
(String osName, String osNamePrefix) Decides if the operating system matches.boolean
isReservedFileName
(CharSequence candidate) Tests whether the given string is a reserved file name.normalizeSeparators
(String path) Converts all separators to the Windows separator of backslash.private static String
Null-safe replace.boolean
Tests whether this file system support driver letters.toLegalFileName
(String candidate, char replacement) Converts a candidate file name (without a path) like"filename.ext"
or"filename"
to a legal file name.(package private) CharSequence
static FileSystem
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 Details
-
GENERIC
Generic file system. -
LINUX
Linux file system. -
MAC_OSX
MacOS file system. -
WINDOWS
Windows file system.The reserved characters are defined in the Naming Conventions (microsoft.com).
- See Also:
-
-
Field Details
-
IS_OS_LINUX
private static final boolean IS_OS_LINUXIs
true
if this is Linux.The field will return
false
ifOS_NAME
isnull
. -
IS_OS_MAC
private static final boolean IS_OS_MACIs
true
if this is Mac.The field will return
false
ifOS_NAME
isnull
. -
OS_NAME_WINDOWS_PREFIX
The prefix String for all Windows OS.- See Also:
-
IS_OS_WINDOWS
private static final boolean IS_OS_WINDOWSIs
true
if this is Windows.The field will return
false
ifOS_NAME
isnull
. -
CURRENT
The current FileSystem. -
blockSize
private final int blockSize -
casePreserving
private final boolean casePreserving -
caseSensitive
private final boolean caseSensitive -
illegalFileNameChars
private final int[] illegalFileNameChars -
maxFileNameLength
private final int maxFileNameLength -
maxPathLength
private final int maxPathLength -
reservedFileNames
-
reservedFileNamesExtensions
private final boolean reservedFileNamesExtensions -
supportsDriveLetter
private final boolean supportsDriveLetter -
nameSeparator
private final char nameSeparator -
nameSeparatorOther
private final char nameSeparatorOther
-
-
Constructor Details
-
FileSystem
private FileSystem(int blockSize, boolean caseSensitive, boolean casePreserving, int maxFileLength, int maxPathLength, int[] illegalFileNameChars, String[] reservedFileNames, boolean reservedFileNamesExtensions, boolean supportsDriveLetter, char nameSeparator) Constructs a new instance.- Parameters:
blockSize
- file allocation block size in bytes.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.reservedFileNamesExtensions
- TODOsupportsDriveLetter
- Whether this file system support driver letters.nameSeparator
- The name separator, '\\' on Windows, '/' on Linux.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
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:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
current
Gets the current file system.- Returns:
- the current file system
-
getCurrent
Gets the current file system.- Returns:
- the current file system
-
getOsMatchesName
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
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
-
indexOf
Copied from Apache Commons Lang CharSequenceUtils. Returns the index withincs
of the first occurrence of the specified character, starting the search at the specified index.If a character with value
searchChar
occurs in the character sequence represented by thecs
object at an index no smaller thanstart
, then the index of the first such occurrence is returned. For values ofsearchChar
in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:
is true. For other values of(this.charAt(k) == searchChar) && (k >= start)
searchChar
, it is the smallest value k such that:(this.codePointAt(k) == searchChar) && (k >= start)
is true. In either case, if no such character occurs inm
cs
at or after positionstart
, then-1
is returned.There is no restriction on the value of
start
. If it is negative, it has the same effect as if it were zero: the entireCharSequence
may be searched. If it is greater than the length ofcs
, it has the same effect as if it were equal to the length ofcs
:-1
is returned.All indices are specified in
char
values (Unicode code units).- Parameters:
cs
- theCharSequence
to be processed, not nullsearchChar
- the char to be searched forstart
- the start index, negative starts at the string start- Returns:
- the index where the search char was found, -1 if not found
- Since:
- 3.6 updated to behave more like
String
-
isOsNameMatch
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
-
replace
Null-safe replace.- Parameters:
path
- the path to be changed, null ignored.oldChar
- the old character.newChar
- the new character.- Returns:
- the new path.
-
getBlockSize
public int getBlockSize()Gets the file allocation block size in bytes.- Returns:
- the file allocation block size in bytes.
- Since:
- 2.12.0
-
getIllegalFileNameChars
public char[] getIllegalFileNameChars()Gets a cloned copy of the illegal characters for this file system.- Returns:
- the illegal characters for this file system.
-
getIllegalFileNameCodePoints
public int[] getIllegalFileNameCodePoints()Gets a cloned copy of the illegal code points for this file system.- Returns:
- the illegal code points for this file system.
- Since:
- 2.12.0
-
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.
-
getNameSeparator
public char getNameSeparator()Gets the name separator, '\\' on Windows, '/' on Linux.- Returns:
- '\\' on Windows, '/' on Linux.
- Since:
- 2.12.0
-
getReservedFileNames
Gets a cloned copy of the reserved file names.- Returns:
- the reserved file names.
-
isCasePreserving
public boolean isCasePreserving()Tests whether this file system preserves case.- Returns:
- Whether this file system preserves case.
-
isCaseSensitive
public boolean isCaseSensitive()Tests whether this file system is case-sensitive.- Returns:
- Whether this file system is case-sensitive.
-
isIllegalFileNameChar
private boolean isIllegalFileNameChar(int c) Tests 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
Tests 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
Tests 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.
-
normalizeSeparators
Converts all separators to the Windows separator of backslash.- Parameters:
path
- the path to be changed, null ignored- Returns:
- the updated path
- Since:
- 2.12.0
-
supportsDriveLetter
public boolean supportsDriveLetter()Tests whether this file system support driver letters.Windows supports driver letters as do other operating systems. Whether these other OS's still support Java like OS/2, is a different matter.
- Returns:
- whether this file system support driver letters.
- Since:
- 2.9.0
- See Also:
-
toLegalFileName
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
-
trimExtension
-