@@ -39,7 +39,7 @@ public enum FileSystem {
3939 0 ,
4040 '/'
4141 // @formatter:on
42- }),
42+ }, new String [] {} ),
4343
4444 MAC_OSX (255 , 1024 , new char [] {
4545 // KEEP THIS ARRAY SORTED!
@@ -49,9 +49,9 @@ public enum FileSystem {
4949 '/' ,
5050 ':'
5151 // @formatter:on
52- }),
52+ }, new String [] {} ),
5353
54- GENERIC (Integer .MAX_VALUE , Integer .MAX_VALUE , new char [] { 0 }),
54+ GENERIC (Integer .MAX_VALUE , Integer .MAX_VALUE , new char [] { 0 }, new String [] {} ),
5555
5656 WINDOWS (255 , 32000 , new char [] {
5757 // KEEP THIS ARRAY SORTED!
@@ -63,7 +63,10 @@ public enum FileSystem {
6363 29 , 30 , 31 ,
6464 '"' , '*' , '/' , ':' , '<' , '>' , '?' , '\\' , '|'
6565 // @formatter:on
66- });
66+ },
67+ // KEEP THIS ARRAY SORTED!
68+ new String [] { "AUX" , "COM1" , "COM2" , "COM3" , "COM4" , "COM5" , "COM6" , "COM7" , "COM8" , "COM9" , "CON" , "LPT1" ,
69+ "LPT2" , "LPT3" , "LPT4" , "LPT5" , "LPT6" , "LPT7" , "LPT8" , "LPT9" , "NUL" , "PRN" });
6770
6871 /**
6972 * The prefix String for all Windows OS.
@@ -176,13 +179,16 @@ private static boolean isOsNameMatch(final String osName, final String osNamePre
176179
177180 private final char [] illegalFileNameChars ;
178181 private final int maxFileNameLength ;
179-
180182 private final int maxPathLength ;
183+ private final String [] reservedFileNames ;
181184
182- private FileSystem (final int maxFileLength , final int maxPathLength , final char [] illegalFileNameChars ) {
185+ private FileSystem (final int maxFileLength , final int maxPathLength , final char [] illegalFileNameChars ,
186+ final String [] reservedFileNames ) {
183187 this .maxFileNameLength = maxFileLength ;
184188 this .maxPathLength = maxPathLength ;
185189 this .illegalFileNameChars = Objects .requireNonNull (illegalFileNameChars , "illegalFileNameChars" );
190+ this .reservedFileNames = Objects .requireNonNull (reservedFileNames , "reservedFileNames" );
191+ ;
186192 }
187193
188194 /**
@@ -216,10 +222,22 @@ private boolean isIllegalFileNameChar(final char c) {
216222 return Arrays .binarySearch (illegalFileNameChars , c ) >= 0 ;
217223 }
218224
225+ /**
226+ * Returns whether the given string is a reserved file name.
227+ *
228+ * @param candidate
229+ * the string to test
230+ * @return {@code true} if the given string is a reserved file name.
231+ */
232+ public boolean isReservedFileName (final CharSequence candidate ) {
233+ return Arrays .binarySearch (reservedFileNames , candidate ) >= 0 ;
234+ }
235+
219236 /**
220237 * Converts a candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"} to a legal file
221238 * name. Illegal characters in the candidate name are replaced by the {@code replacement} character. If the file
222- * name length exceeds {@link #getMaxFileNameLength()}, then the name is truncated to {@link #getMaxFileNameLength()}.
239+ * name length exceeds {@link #getMaxFileNameLength()}, then the name is truncated to
240+ * {@link #getMaxFileNameLength()}.
223241 *
224242 * @param candidate
225243 * a candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"}
@@ -248,11 +266,9 @@ public String toLegalFileName(final String candidate, final char replacement) {
248266 }
249267
250268 /**
251- * Checks if a candidate file name (without a path)
252- * such as {@code "filename.ext"} or {@code "filename"}
253- * is a potentially legal file name.
254- * If the file name length exceeds {@link #getMaxFileNameLength()},
255- * or if it contains an illegal character then the check fails.
269+ * Checks if a candidate file name (without a path) such as {@code "filename.ext"} or {@code "filename"} is a
270+ * potentially legal file name. If the file name length exceeds {@link #getMaxFileNameLength()}, or if it contains
271+ * an illegal character then the check fails.
256272 *
257273 * @param candidate
258274 * a candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"}
@@ -262,11 +278,18 @@ public boolean isLegalFileName(final CharSequence candidate) {
262278 if (candidate == null || candidate .length () == 0 || candidate .length () > maxFileNameLength ) {
263279 return false ;
264280 }
281+ if (isReservedFileName (candidate )) {
282+ return false ;
283+ }
265284 for (int i = 0 ; i < candidate .length (); i ++) {
266285 if (isIllegalFileNameChar (candidate .charAt (i ))) {
267286 return false ;
268287 }
269288 }
270289 return true ;
271290 }
291+
292+ public String [] getReservedFileNames () {
293+ return reservedFileNames ;
294+ }
272295}
0 commit comments