Add FileSystem#supportsDriveLetter().
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -109,6 +109,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Replace magic numbers with constants with the new IOUtils.CR and LF.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add FileSystem#supportsDriveLetter().
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

MODIFY
src/main/java/org/apache/commons/io/FileSystem.java
src/main/java/org/apache/commons/io/FileSystem.java
/**
* Generic file system.
*/
- GENERIC(false, false, Integer.MAX_VALUE, Integer.MAX_VALUE, new char[] { 0 }, new String[] {}),
+ GENERIC(false, false, Integer.MAX_VALUE, Integer.MAX_VALUE, new char[] { 0 }, new String[] {}, false),
/**
* Linux file system.
0,
'/'
// @formatter:on
- }, new String[] {}),
+ }, new String[] {}, false),
/**
* MacOS file system.
'/',
':'
// @formatter:on
- }, new String[] {}),
+ }, new String[] {}, false),
/**
* Windows file system.
// @formatter:on
}, // KEEP THIS ARRAY SORTED!
new String[] { "AUX", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "CON", "LPT1",
- "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN" });
+ "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN" }, true);
/**
* <p>
@@ -194,31 +194,29 @@ private static boolean isOsNameMatch(final String osName, final String osNamePre
private final int maxFileNameLength;
private final int maxPathLength;
private final String[] reservedFileNames;
+ private final boolean supportsDriveLetter;
/**
* Constructs a new instance.
*
- * @param caseSensitive
- * Whether this file system is case sensitive.
- * @param casePreserving
- * Whether this file system is case preserving.
- * @param maxFileLength
- * The maximum length for file names. The file name does not include folders.
- * @param maxPathLength
- * The maximum length of the path to a file. This can include folders.
- * @param illegalFileNameChars
- * Illegal characters for this file system.
- * @param reservedFileNames
- * The reserved file names.
+ * @param caseSensitive Whether this file system is case sensitive.
+ * @param casePreserving Whether this file system is case preserving.
+ * @param maxFileLength The maximum length for file names. The file name does not include folders.
+ * @param maxPathLength The maximum length of the path to a file. This can include folders.
+ * @param illegalFileNameChars Illegal characters for this file system.
+ * @param reservedFileNames The reserved file names.
+ * @param supportsDriveLetter Whether this file system support driver letters.
*/
FileSystem(final boolean caseSensitive, final boolean casePreserving, final int maxFileLength,
- final int maxPathLength, final char[] illegalFileNameChars, final String[] reservedFileNames) {
+ final int maxPathLength, final char[] illegalFileNameChars, final String[] reservedFileNames,
+ final boolean supportsDriveLetter) {
this.maxFileNameLength = maxFileLength;
this.maxPathLength = maxPathLength;
this.illegalFileNameChars = Objects.requireNonNull(illegalFileNameChars, "illegalFileNameChars");
this.reservedFileNames = Objects.requireNonNull(reservedFileNames, "reservedFileNames");
this.caseSensitive = caseSensitive;
this.casePreserving = casePreserving;
+ this.supportsDriveLetter = supportsDriveLetter;
}
/**
@@ -321,6 +319,22 @@ public boolean isReservedFileName(final CharSequence candidate) {
return Arrays.binarySearch(reservedFileNames, candidate) >= 0;
}
+ /**
+ * Tests whether this file system support driver letters.
+ * <p>
+ * Windows supports driver letters as do other operating systems. Whether these other OS's still support Java like
+ * OS/2, is a different matter.
+ * </p>
+ *
+ * @return whether this file system support driver letters.
+ * @since 2.9.0
+ * @see <a href="https://en.wikipedia.org/wiki/Drive_letter_assignment">Operating systems that use drive letter
+ * assignment</a>
+ */
+ public boolean supportsDriveLetter() {
+ return supportsDriveLetter;
+ }
+
/**
* Converts a candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"} to a legal file
* name. Illegal characters in the candidate name are replaced by the {@code replacement} character. If the file

MODIFY
src/test/java/org/apache/commons/io/FileSystemTestCase.java
src/test/java/org/apache/commons/io/FileSystemTestCase.java
@@ -40,38 +40,6 @@ public void testGetCurrent() {
}
}
- @Test
- public void testSorted() {
- for (final FileSystem fs : FileSystem.values()) {
- final char[] chars = fs.getIllegalFileNameChars();
- for (int i = 0; i < chars.length - 1; i++) {
- assertTrue(chars[i] < chars[i + 1], fs.name());
- }
- }
- }
-
- @Test
- public void testToLegalFileNameWindows() {
- final FileSystem fs = FileSystem.WINDOWS;
- final char replacement = '-';
- for (char i = 0; i < 32; i++) {
- assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
- }
- final char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
- for (char i = 0; i < illegal.length; i++) {
- assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
- }
- for (char i = 'a'; i < 'z'; i++) {
- assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
- }
- for (char i = 'A'; i < 'Z'; i++) {
- assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
- }
- for (char i = '0'; i < '9'; i++) {
- assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
- }
- }
-
@Test
public void testIsLegalName() {
for (final FileSystem fs : FileSystem.values()) {
@@ -105,4 +73,44 @@ public void testReplacementWithNUL() {
}
}
}
+
+ @Test
+ public void testSorted() {
+ for (final FileSystem fs : FileSystem.values()) {
+ final char[] chars = fs.getIllegalFileNameChars();
+ for (int i = 0; i < chars.length - 1; i++) {
+ assertTrue(chars[i] < chars[i + 1], fs.name());
+ }
+ }
+ }
+
+ @Test
+ public void testSupportsDriveLetter() {
+ assertTrue(FileSystem.WINDOWS.supportsDriveLetter());
+ assertFalse(FileSystem.GENERIC.supportsDriveLetter());
+ assertFalse(FileSystem.LINUX.supportsDriveLetter());
+ assertFalse(FileSystem.MAC_OSX.supportsDriveLetter());
+ }
+
+ @Test
+ public void testToLegalFileNameWindows() {
+ final FileSystem fs = FileSystem.WINDOWS;
+ final char replacement = '-';
+ for (char i = 0; i < 32; i++) {
+ assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
+ }
+ final char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
+ for (char i = 0; i < illegal.length; i++) {
+ assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
+ }
+ for (char i = 'a'; i < 'z'; i++) {
+ assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
+ }
+ for (char i = 'A'; i < 'Z'; i++) {
+ assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
+ }
+ for (char i = '0'; i < '9'; i++) {
+ assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
+ }
+ }
}

Add FileSystem#supportsDriveLetter().