You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/12/29 15:21:32 UTC

[commons-io] branch master updated: Add FileSystem#supportsDriveLetter().

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 17b6d78  Add FileSystem#supportsDriveLetter().
17b6d78 is described below

commit 17b6d78a9ce6fab68e00f605fbe647faeec300c3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Dec 29 10:21:27 2020 -0500

    Add FileSystem#supportsDriveLetter().
---
 src/changes/changes.xml                            |  3 +
 .../java/org/apache/commons/io/FileSystem.java     | 48 ++++++++++-----
 .../org/apache/commons/io/FileSystemTestCase.java  | 72 ++++++++++++----------
 3 files changed, 74 insertions(+), 49 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 46e42a4..6563c48 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/FileSystem.java b/src/main/java/org/apache/commons/io/FileSystem.java
index f3e07a1..7efbdd6 100644
--- a/src/main/java/org/apache/commons/io/FileSystem.java
+++ b/src/main/java/org/apache/commons/io/FileSystem.java
@@ -36,7 +36,7 @@ public enum FileSystem {
     /**
      * 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.
@@ -48,7 +48,7 @@ public enum FileSystem {
             0,
              '/'
             // @formatter:on
-    }, new String[] {}),
+    }, new String[] {}, false),
 
     /**
      * MacOS file system.
@@ -61,7 +61,7 @@ public enum FileSystem {
             '/',
              ':'
             // @formatter:on
-    }, new String[] {}),
+    }, new String[] {}, false),
 
     /**
      * Windows file system.
@@ -79,7 +79,7 @@ public enum FileSystem {
                     // @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 @@ public enum FileSystem {
     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;
     }
 
     /**
@@ -322,6 +320,22 @@ public enum FileSystem {
     }
 
     /**
+     * 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
      * name length exceeds {@link #getMaxFileNameLength()}, then the name is truncated to
diff --git a/src/test/java/org/apache/commons/io/FileSystemTestCase.java b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
index 4a7a7bb..c58166a 100644
--- a/src/test/java/org/apache/commons/io/FileSystemTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
@@ -41,38 +41,6 @@ public class FileSystemTestCase {
     }
 
     @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()) {
             assertFalse(fs.isLegalFileName(""), fs.name()); // Empty is always illegal
@@ -105,4 +73,44 @@ public class FileSystemTestCase {
             }
         }
     }
+
+    @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));
+        }
+    }
 }