commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject commons-io git commit: [IO-553] Add org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
Date Wed, 15 Nov 2017 15:44:47 GMT
Repository: commons-io
Updated Branches:
  refs/heads/master d8e8908db -> 06033035f


[IO-553] Add
org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).

Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/06033035
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/06033035
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/06033035

Branch: refs/heads/master
Commit: 06033035f09c662020ffe0300983697c78d3039a
Parents: d8e8908
Author: Gary Gregory <ggregory@apache.org>
Authored: Wed Nov 15 08:44:44 2017 -0700
Committer: Gary Gregory <ggregory@apache.org>
Committed: Wed Nov 15 08:44:44 2017 -0700

----------------------------------------------------------------------
 src/changes/changes.xml                         |  4 +--
 .../org/apache/commons/io/FilenameUtils.java    | 35 ++++++++++++++++++--
 .../commons/io/FilenameUtilsTestCase.java       | 22 ++++++++++++
 3 files changed, 57 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-io/blob/06033035/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7ad3fcc..b308d2f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,8 +47,8 @@ The <action> type attribute can be add,update,fix,remove.
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.7" date="tba" description="tba">
-      <action issue="IO-553" dev="ggregory" type="update">
-        Add org.apache.commons.io.FilenameUtils.WINDOWS_ILLEGAL_FILE_NAME_CHARS.
+      <action issue="IO-553" dev="ggregory" type="add">
+        Add org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
       </action>
     </release>
 

http://git-wip-us.apache.org/repos/asf/commons-io/blob/06033035/src/main/java/org/apache/commons/io/FilenameUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/io/FilenameUtils.java b/src/main/java/org/apache/commons/io/FilenameUtils.java
index b08ab91..bf91bcb 100644
--- a/src/main/java/org/apache/commons/io/FilenameUtils.java
+++ b/src/main/java/org/apache/commons/io/FilenameUtils.java
@@ -19,6 +19,7 @@ package org.apache.commons.io;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Stack;
 
@@ -121,14 +122,15 @@ public class FilenameUtils {
      * @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">Naming
Files,
      *      Paths, and Namespaces</a>
      */
-    public static final char[] WINDOWS_ILLEGAL_FILE_NAME_CHARS = {
+    private static final char[] WINDOWS_ILLEGAL_FILE_NAME_CHARS = {
+            // KEEP THIS ARRAY SORTED!
             // @formatter:off
             // ASCII NULL
             0,  
             // 1-31 may be allowed in file streams
             1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28,
             29, 30, 31, 
-            '<', '>', ':', '"', '/', '\\', '|', '?', '*'
+            '"', '*', '/', ':', '<', '>', '?', '\\', '|'
             // @formatter:on
     };
 
@@ -1290,6 +1292,35 @@ public class FilenameUtils {
         return false;
     }
 
+    /**
+     * Checks whether the given character is illegal in a Windows file names.
+     * <p>
+     * The illegal character are:
+     * </p>
+     * <ul>
+     * <li>< (less than</li>
+     * <li>> (greater than</li>
+     * <li>: (colon</li>
+     * <li>" (double quote</li>
+     * <li>/ (forward slash</li>
+     * <li>\ (backslash</li>
+     * <li>| (vertical bar or pipe</li>
+     * <li>? (question mark</li>
+     * <li>* (asterisk</li>
+     * <li>ASCII NUL (0)</li>
+     * <li>Integer characters 1 through 31</li>
+     * </ul>
+     * 
+     * @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">Naming
Files,
+     *      Paths, and Namespaces</a>
+     * @param c the character to check
+     * @return whether the give character is legal
+     * @since 2.7
+     */
+    public static boolean isIllegalWindowsFileName(final char c) {
+        return Arrays.binarySearch(WINDOWS_ILLEGAL_FILE_NAME_CHARS, c) >= 0;
+    }
+    
     //-----------------------------------------------------------------------
     /**
      * Checks a filename to see if it matches the specified wildcard matcher,

http://git-wip-us.apache.org/repos/asf/commons-io/blob/06033035/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java b/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
index 234c25e..dcc7e2b 100644
--- a/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
@@ -247,6 +247,28 @@ public class FilenameUtilsTestCase {
     }
 
     @Test
+    public void testIsIllegalWindowsFileName() {
+        for (char i = 0; i < 32; i++) {
+            assertTrue(FilenameUtils.isIllegalWindowsFileName(i));
+        }
+        char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*'
};
+        Arrays.sort(illegal);
+        System.out.println(Arrays.toString(illegal));
+        for (char i = 0; i < illegal.length; i++) {
+            assertTrue(FilenameUtils.isIllegalWindowsFileName(illegal[i]));
+        }
+        for (char i = 'a'; i < 'z'; i++) {
+            assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
+        }
+        for (char i = 'A'; i < 'Z'; i++) {
+            assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
+        }
+        for (char i = '0'; i < '9'; i++) {
+            assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
+        }
+    }
+    
+    @Test
     public void testNormalize_with_nullbytes() throws Exception {
         try {
             assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c\u0000.txt"));


Mime
View raw message