114114 */
115115public class FileUtils {
116116
117+ private static final String PROTOCOL_FILE = "file" ;
118+
117119 /**
118120 * The number of bytes in a kilobyte.
119121 */
@@ -329,7 +331,7 @@ private static File checkIsFile(final File file, final String name) {
329331 * @since 1.3
330332 */
331333 public static Checksum checksum (final File file , final Checksum checksum ) throws IOException {
332- checkFileExists (file , "file" );
334+ checkFileExists (file , PROTOCOL_FILE );
333335 Objects .requireNonNull (checksum , "checksum" );
334336 try (InputStream inputStream = new CheckedInputStream (Files .newInputStream (file .toPath ()), checksum )) {
335337 IOUtils .consume (inputStream );
@@ -1218,7 +1220,7 @@ static String decodeUrl(final String url) {
12181220 * @since 2.9.0
12191221 */
12201222 public static File delete (final File file ) throws IOException {
1221- Objects .requireNonNull (file , "file" );
1223+ Objects .requireNonNull (file , PROTOCOL_FILE );
12221224 Files .delete (file .toPath ());
12231225 return file ;
12241226 }
@@ -1379,7 +1381,7 @@ private static void doCopyDirectory(final File srcDir, final File destDir, final
13791381 * @throws IOException in case deletion is unsuccessful.
13801382 */
13811383 public static void forceDelete (final File file ) throws IOException {
1382- Objects .requireNonNull (file , "file" );
1384+ Objects .requireNonNull (file , PROTOCOL_FILE );
13831385
13841386 final Counters .PathCounters deleteCounters ;
13851387 try {
@@ -1404,7 +1406,7 @@ public static void forceDelete(final File file) throws IOException {
14041406 * @throws IOException in case deletion is unsuccessful.
14051407 */
14061408 public static void forceDeleteOnExit (final File file ) throws IOException {
1407- Objects .requireNonNull (file , "file" );
1409+ Objects .requireNonNull (file , PROTOCOL_FILE );
14081410 if (file .isDirectory ()) {
14091411 deleteDirectoryOnExit (file );
14101412 } else {
@@ -1444,7 +1446,7 @@ public static void forceMkdir(final File directory) throws IOException {
14441446 * @since 2.5
14451447 */
14461448 public static void forceMkdirParent (final File file ) throws IOException {
1447- forceMkdir (getParentFile (Objects .requireNonNull (file , "file" )));
1449+ forceMkdir (getParentFile (Objects .requireNonNull (file , PROTOCOL_FILE )));
14481450 }
14491451
14501452 /**
@@ -1689,7 +1691,7 @@ public static boolean isFileNewer(final File file, final ChronoLocalDateTime<?>
16891691 * @since 2.8.0
16901692 */
16911693 public static boolean isFileNewer (final File file , final ChronoZonedDateTime <?> chronoZonedDateTime ) {
1692- Objects .requireNonNull (file , "file" );
1694+ Objects .requireNonNull (file , PROTOCOL_FILE );
16931695 Objects .requireNonNull (chronoZonedDateTime , "chronoZonedDateTime" );
16941696 return Uncheck .get (() -> PathUtils .isNewer (file .toPath (), chronoZonedDateTime ));
16951697 }
@@ -1734,7 +1736,7 @@ public static boolean isFileNewer(final File file, final File reference) {
17341736 * @since 2.12.0
17351737 */
17361738 public static boolean isFileNewer (final File file , final FileTime fileTime ) throws IOException {
1737- Objects .requireNonNull (file , "file" );
1739+ Objects .requireNonNull (file , PROTOCOL_FILE );
17381740 return PathUtils .isNewer (file .toPath (), fileTime );
17391741 }
17401742
@@ -1764,7 +1766,7 @@ public static boolean isFileNewer(final File file, final Instant instant) {
17641766 * @throws NullPointerException if the file is {@code null}.
17651767 */
17661768 public static boolean isFileNewer (final File file , final long timeMillis ) {
1767- Objects .requireNonNull (file , "file" );
1769+ Objects .requireNonNull (file , PROTOCOL_FILE );
17681770 return Uncheck .get (() -> PathUtils .isNewer (file .toPath (), timeMillis ));
17691771 }
17701772
@@ -1947,7 +1949,7 @@ public static boolean isFileOlder(final File file, final File reference) throws
19471949 * @since 2.12.0
19481950 */
19491951 public static boolean isFileOlder (final File file , final FileTime fileTime ) throws IOException {
1950- Objects .requireNonNull (file , "file" );
1952+ Objects .requireNonNull (file , PROTOCOL_FILE );
19511953 return PathUtils .isOlder (file .toPath (), fileTime );
19521954 }
19531955
@@ -1976,7 +1978,7 @@ public static boolean isFileOlder(final File file, final Instant instant) {
19761978 * @throws UncheckedIOException if an I/O error occurs
19771979 */
19781980 public static boolean isFileOlder (final File file , final long timeMillis ) {
1979- Objects .requireNonNull (file , "file" );
1981+ Objects .requireNonNull (file , PROTOCOL_FILE );
19801982 return Uncheck .get (() -> PathUtils .isOlder (file .toPath (), timeMillis ));
19811983 }
19821984
@@ -1994,6 +1996,16 @@ public static boolean isFileOlder(final File file, final OffsetDateTime offsetDa
19941996 return isFileOlder (file , offsetDateTime .toInstant ());
19951997 }
19961998
1999+ /**
2000+ * Tests whether the given URL is a file URL.
2001+ *
2002+ * @param url The URL to test.
2003+ * @return Whether the given URL is a file URL.
2004+ */
2005+ private static boolean isFileProtocol (final URL url ) {
2006+ return PROTOCOL_FILE .equalsIgnoreCase (url .getProtocol ());
2007+ }
2008+
19972009 /**
19982010 * Tests whether the specified {@link File} is a regular file or not. Implemented as a
19992011 * null-safe delegate to {@link Files#isRegularFile(Path path, LinkOption... options)}.
@@ -2138,7 +2150,7 @@ public static FileTime lastModifiedFileTime(final File file) throws IOException
21382150 // https://bugs.openjdk.java.net/browse/JDK-8177809
21392151 // File.lastModified() is losing milliseconds (always ends in 000)
21402152 // This bug is in OpenJDK 8 and 9, and fixed in 10.
2141- return Files .getLastModifiedTime (Objects .requireNonNull (file , "file" ).toPath ());
2153+ return Files .getLastModifiedTime (Objects .requireNonNull (file , PROTOCOL_FILE ).toPath ());
21422154 }
21432155
21442156 /**
@@ -2549,7 +2561,7 @@ public static void moveToDirectory(final File src, final File destDir, final boo
25492561 * @since 2.12.0
25502562 */
25512563 public static OutputStream newOutputStream (final File file , final boolean append ) throws IOException {
2552- return PathUtils .newOutputStream (Objects .requireNonNull (file , "file" ).toPath (), append );
2564+ return PathUtils .newOutputStream (Objects .requireNonNull (file , PROTOCOL_FILE ).toPath (), append );
25532565 }
25542566
25552567 /**
@@ -2572,7 +2584,7 @@ public static OutputStream newOutputStream(final File file, final boolean append
25722584 * @since 1.3
25732585 */
25742586 public static FileInputStream openInputStream (final File file ) throws IOException {
2575- Objects .requireNonNull (file , "file" );
2587+ Objects .requireNonNull (file , PROTOCOL_FILE );
25762588 return new FileInputStream (file );
25772589 }
25782590
@@ -2628,9 +2640,9 @@ public static FileOutputStream openOutputStream(final File file) throws IOExcept
26282640 * @since 2.1
26292641 */
26302642 public static FileOutputStream openOutputStream (final File file , final boolean append ) throws IOException {
2631- Objects .requireNonNull (file , "file" );
2643+ Objects .requireNonNull (file , PROTOCOL_FILE );
26322644 if (file .exists ()) {
2633- checkIsFile (file , "file" );
2645+ checkIsFile (file , PROTOCOL_FILE );
26342646 } else {
26352647 createParentDirectories (file );
26362648 }
@@ -2649,7 +2661,7 @@ public static FileOutputStream openOutputStream(final File file, final boolean a
26492661 * @since 1.1
26502662 */
26512663 public static byte [] readFileToByteArray (final File file ) throws IOException {
2652- Objects .requireNonNull (file , "file" );
2664+ Objects .requireNonNull (file , PROTOCOL_FILE );
26532665 return Files .readAllBytes (file .toPath ());
26542666 }
26552667
@@ -2967,7 +2979,7 @@ public static Stream<File> streamFiles(final File directory, final boolean recur
29672979 * if the URL's protocol is not {@code file}
29682980 */
29692981 public static File toFile (final URL url ) {
2970- if (url == null || !"file" . equalsIgnoreCase (url . getProtocol () )) {
2982+ if (url == null || !isFileProtocol (url )) {
29712983 return null ;
29722984 }
29732985 final String fileName = url .getFile ().replace ('/' , File .separatorChar );
@@ -3003,7 +3015,7 @@ public static File[] toFiles(final URL... urls) {
30033015 for (int i = 0 ; i < urls .length ; i ++) {
30043016 final URL url = urls [i ];
30053017 if (url != null ) {
3006- if (!"file" . equalsIgnoreCase (url . getProtocol () )) {
3018+ if (!isFileProtocol (url )) {
30073019 throw new IllegalArgumentException ("Can only convert file URL to a File: " + url );
30083020 }
30093021 files [i ] = toFile (url );
@@ -3056,7 +3068,7 @@ private static String[] toSuffixes(final String... extensions) {
30563068 * @throws IOException if setting the last-modified time failed or an I/O problem occurs.
30573069 */
30583070 public static void touch (final File file ) throws IOException {
3059- PathUtils .touch (Objects .requireNonNull (file , "file" ).toPath ());
3071+ PathUtils .touch (Objects .requireNonNull (file , PROTOCOL_FILE ).toPath ());
30603072 }
30613073
30623074 /**
@@ -3113,7 +3125,7 @@ private static void validateMoveParameters(final File source, final File destina
31133125 * @throws NullPointerException if the file is {@code null}
31143126 */
31153127 public static boolean waitFor (final File file , final int seconds ) {
3116- Objects .requireNonNull (file , "file" );
3128+ Objects .requireNonNull (file , PROTOCOL_FILE );
31173129 return PathUtils .waitFor (file .toPath (), Duration .ofSeconds (seconds ), PathUtils .EMPTY_LINK_OPTION_ARRAY );
31183130 }
31193131
0 commit comments