Skip to content

Commit 0603303

Browse files
committed
[IO-553] Add
org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
1 parent d8e8908 commit 0603303

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.7" date="tba" description="tba">
50-
<action issue="IO-553" dev="ggregory" type="update">
51-
Add org.apache.commons.io.FilenameUtils.WINDOWS_ILLEGAL_FILE_NAME_CHARS.
50+
<action issue="IO-553" dev="ggregory" type="add">
51+
Add org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
5252
</action>
5353
</release>
5454

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.util.ArrayList;
22+
import java.util.Arrays;
2223
import java.util.Collection;
2324
import java.util.Stack;
2425

@@ -121,14 +122,15 @@ public class FilenameUtils {
121122
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">Naming Files,
122123
* Paths, and Namespaces</a>
123124
*/
124-
public static final char[] WINDOWS_ILLEGAL_FILE_NAME_CHARS = {
125+
private static final char[] WINDOWS_ILLEGAL_FILE_NAME_CHARS = {
126+
// KEEP THIS ARRAY SORTED!
125127
// @formatter:off
126128
// ASCII NULL
127129
0,
128130
// 1-31 may be allowed in file streams
129131
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,
130132
29, 30, 31,
131-
'<', '>', ':', '"', '/', '\\', '|', '?', '*'
133+
'"', '*', '/', ':', '<', '>', '?', '\\', '|'
132134
// @formatter:on
133135
};
134136

@@ -1290,6 +1292,35 @@ public static boolean isExtension(final String filename, final Collection<String
12901292
return false;
12911293
}
12921294

1295+
/**
1296+
* Checks whether the given character is illegal in a Windows file names.
1297+
* <p>
1298+
* The illegal character are:
1299+
* </p>
1300+
* <ul>
1301+
* <li>< (less than</li>
1302+
* <li>> (greater than</li>
1303+
* <li>: (colon</li>
1304+
* <li>" (double quote</li>
1305+
* <li>/ (forward slash</li>
1306+
* <li>\ (backslash</li>
1307+
* <li>| (vertical bar or pipe</li>
1308+
* <li>? (question mark</li>
1309+
* <li>* (asterisk</li>
1310+
* <li>ASCII NUL (0)</li>
1311+
* <li>Integer characters 1 through 31</li>
1312+
* </ul>
1313+
*
1314+
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">Naming Files,
1315+
* Paths, and Namespaces</a>
1316+
* @param c the character to check
1317+
* @return whether the give character is legal
1318+
* @since 2.7
1319+
*/
1320+
public static boolean isIllegalWindowsFileName(final char c) {
1321+
return Arrays.binarySearch(WINDOWS_ILLEGAL_FILE_NAME_CHARS, c) >= 0;
1322+
}
1323+
12931324
//-----------------------------------------------------------------------
12941325
/**
12951326
* Checks a filename to see if it matches the specified wildcard matcher,

src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,28 @@ public void testNormalize() throws Exception {
246246
assertEquals(SEP + SEP + "server" + SEP + "", FilenameUtils.normalize("//server/"));
247247
}
248248

249+
@Test
250+
public void testIsIllegalWindowsFileName() {
251+
for (char i = 0; i < 32; i++) {
252+
assertTrue(FilenameUtils.isIllegalWindowsFileName(i));
253+
}
254+
char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
255+
Arrays.sort(illegal);
256+
System.out.println(Arrays.toString(illegal));
257+
for (char i = 0; i < illegal.length; i++) {
258+
assertTrue(FilenameUtils.isIllegalWindowsFileName(illegal[i]));
259+
}
260+
for (char i = 'a'; i < 'z'; i++) {
261+
assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
262+
}
263+
for (char i = 'A'; i < 'Z'; i++) {
264+
assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
265+
}
266+
for (char i = '0'; i < '9'; i++) {
267+
assertFalse("i = " + (int) i, FilenameUtils.isIllegalWindowsFileName(i));
268+
}
269+
}
270+
249271
@Test
250272
public void testNormalize_with_nullbytes() throws Exception {
251273
try {

0 commit comments

Comments
 (0)