View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.codehaus.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact codehaus@codehaus.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.codehaus.org/>.
55   *
56   */
57  
58  import java.io.BufferedReader;
59  import java.io.File;
60  import java.io.IOException;
61  import java.io.InputStream;
62  import java.io.OutputStream;
63  import java.io.OutputStreamWriter;
64  import java.io.Reader;
65  import java.io.Writer;
66  import java.net.URL;
67  import java.nio.charset.Charset;
68  import java.nio.file.Files;
69  import java.nio.file.LinkOption;
70  import java.nio.file.Path;
71  import java.nio.file.Paths;
72  import java.nio.file.StandardOpenOption;
73  import java.security.SecureRandom;
74  import java.text.DecimalFormat;
75  import java.util.ArrayList;
76  import java.util.Arrays;
77  import java.util.List;
78  import java.util.Random;
79  
80  import org.codehaus.plexus.util.io.InputStreamFacade;
81  import org.codehaus.plexus.util.io.URLInputStreamFacade;
82  
83  /**
84   * <p>This class provides basic facilities for manipulating files and file paths.</p>
85   *
86   * <b>Path-related methods</b>
87   *
88   * <p>Methods exist to retrieve the components of a typical file path. For example
89   * <code>/www/hosted/mysite/index.html</code>, can be broken into:
90   * <ul>
91   * <li><code>/www/hosted/mysite/</code> -- retrievable through {@link #getPath}</li>
92   * <li><code>index.html</code> -- retrievable through {@link #removePath}</li>
93   * <li><code>/www/hosted/mysite/index</code> -- retrievable through {@link #removeExtension}</li>
94   * <li><code>html</code> -- retrievable through {@link #getExtension}</li>
95   * </ul>
96   * <p>There are also methods to {@link #catPath concatenate two paths}, {@link #resolveFile resolve a path relative to a
97   * File} and {@link #normalize} a path.</p>
98   *
99   * <b>File-related methods</b>
100  *
101  * <p>There are methods to create a {@link #toFile File from a URL}, copy a {@link #copyFileToDirectory File to a
102  * directory}, copy a {@link #copyFile File to another File}, copy a {@link #copyURLToFile URL's contents to a File}, as
103  * well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File) clean} a directory.</p>
104  *
105  * <p>Common {@link java.io.File} manipulation routines.</p>
106  *
107  * <p>Taken from the commons-utils repo. Also code from Alexandria's FileUtils. And from Avalon Excalibur's IO. And from
108  * Ant.</p>
109  *
110  * @author <a href="mailto:burton@relativity.yi.org">Kevin A. Burton</A>
111  * @author <a href="mailto:sanders@codehaus.org">Scott Sanders</a>
112  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
113  * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
114  * @author <a href="mailto:peter@codehaus.org">Peter Donald</a>
115  * @author <a href="mailto:jefft@codehaus.org">Jeff Turner</a>
116  *
117  */
118 public class FileUtils extends BaseFileUtils {
119     /**
120      * The number of bytes in a kilobyte.
121      */
122     public static final int ONE_KB = 1024;
123 
124     /**
125      * The number of bytes in a megabyte.
126      */
127     public static final int ONE_MB = ONE_KB * ONE_KB;
128 
129     /**
130      * The number of bytes in a gigabyte.
131      */
132     public static final int ONE_GB = ONE_KB * ONE_MB;
133 
134     /**
135      * The vm file separator
136      */
137     public static String FS = File.separator;
138 
139     /**
140      * Non-valid Characters for naming files, folders under Windows: <code>":", "*", "?", "\"", "<", ">", "|"</code>
141      *
142      * @see <a href="http://support.microsoft.com/?scid=kb%3Ben-us%3B177506&x=12&y=13">
143      *      http://support.microsoft.com/?scid=kb%3Ben-us%3B177506&x=12&y=13</a>
144      */
145     private static final String[] INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME = {":", "*", "?", "\"", "<", ">", "|"};
146 
147     /**
148      * @return the default excludes pattern
149      * @see DirectoryScanner#DEFAULTEXCLUDES
150      */
151     public static String[] getDefaultExcludes() {
152         return DirectoryScanner.DEFAULTEXCLUDES;
153     }
154 
155     /**
156      * @return the default excludes pattern as list.
157      * @see #getDefaultExcludes()
158      */
159     public static List<String> getDefaultExcludesAsList() {
160         return Arrays.asList(getDefaultExcludes());
161     }
162 
163     /**
164      * @return the default excludes pattern as comma separated string.
165      * @see DirectoryScanner#DEFAULTEXCLUDES
166      * @see StringUtils#join(Object[], String)
167      */
168     public static String getDefaultExcludesAsString() {
169         return StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, ",");
170     }
171 
172     /**
173      * Returns a human-readable version of the file size (original is in bytes).
174      *
175      * @param size The number of bytes.
176      * @return A human-readable display value (includes units).
177      */
178     public static String byteCountToDisplaySize(int size) {
179         String displaySize;
180 
181         if (size / ONE_GB > 0) {
182             displaySize = String.valueOf(size / ONE_GB) + " GB";
183         } else if (size / ONE_MB > 0) {
184             displaySize = String.valueOf(size / ONE_MB) + " MB";
185         } else if (size / ONE_KB > 0) {
186             displaySize = String.valueOf(size / ONE_KB) + " KB";
187         } else {
188             displaySize = String.valueOf(size) + " bytes";
189         }
190 
191         return displaySize;
192     }
193 
194     /**
195      * Returns the directory path portion of a file specification string. Matches the equally named unix command.
196      *
197      * @param filename the file path
198      * @return The directory portion excluding the ending file separator.
199      */
200     public static String dirname(String filename) {
201         int i = filename.lastIndexOf(File.separator);
202         return (i >= 0 ? filename.substring(0, i) : "");
203     }
204 
205     /**
206      * Returns the filename portion of a file specification string.
207      *
208      * @param filename the file path
209      * @return The filename string with extension.
210      */
211     public static String filename(String filename) {
212         int i = filename.lastIndexOf(File.separator);
213         return (i >= 0 ? filename.substring(i + 1) : filename);
214     }
215 
216     /**
217      * Returns the filename portion of a file specification string. Matches the equally named unix command.
218      *
219      * @param filename the file path
220      * @return The filename string without extension.
221      */
222     public static String basename(String filename) {
223         return basename(filename, extension(filename));
224     }
225 
226     /**
227      * Returns the filename portion of a file specification string. Matches the equally named unix command.
228      *
229      * @param filename the file path
230      * @param suffix the file suffix
231      * @return the basename of the file
232      */
233     public static String basename(String filename, String suffix) {
234         int i = filename.lastIndexOf(File.separator) + 1;
235         int lastDot = ((suffix != null) && (suffix.length() > 0)) ? filename.lastIndexOf(suffix) : -1;
236 
237         if (lastDot >= 0) {
238             return filename.substring(i, lastDot);
239         } else if (i > 0) {
240             return filename.substring(i);
241         } else {
242             return filename; // else returns all (no path and no extension)
243         }
244     }
245 
246     /**
247      * Returns the extension portion of a file specification string. This everything after the last dot '.' in the
248      * filename (NOT including the dot).
249      *
250      * @param filename the file path
251      * @return the extension of the file
252      */
253     public static String extension(String filename) {
254         // Ensure the last dot is after the last file separator
255         int lastSep = filename.lastIndexOf(File.separatorChar);
256         int lastDot;
257         if (lastSep < 0) {
258             lastDot = filename.lastIndexOf('.');
259         } else {
260             lastDot = filename.substring(lastSep + 1).lastIndexOf('.');
261             if (lastDot >= 0) {
262                 lastDot += lastSep + 1;
263             }
264         }
265 
266         if (lastDot >= 0 && lastDot > lastSep) {
267             return filename.substring(lastDot + 1);
268         }
269 
270         return "";
271     }
272 
273     /**
274      * Check if a file exits.
275      *
276      * @param fileName the file path.
277      * @return true if file exists.
278      */
279     public static boolean fileExists(String fileName) {
280         File file = new File(fileName);
281         return file.exists();
282     }
283 
284     /**
285      * Note: the file content is read with platform encoding.
286      *
287      * @param file the file path
288      * @return the file content using the platform encoding.
289      * @throws IOException if any
290      */
291     public static String fileRead(String file) throws IOException {
292         return fileRead(file, null);
293     }
294 
295     /**
296      * @param file the file path
297      * @param encoding the wanted encoding
298      * @return the file content using the specified encoding.
299      * @throws IOException if any
300      */
301     public static String fileRead(String file, String encoding) throws IOException {
302         return fileRead(Paths.get(file), encoding);
303     }
304 
305     /**
306      * Note: the file content is read with platform encoding
307      *
308      * @param file the file path
309      * @return the file content using the platform encoding.
310      * @throws IOException if any
311      */
312     public static String fileRead(File file) throws IOException {
313         return fileRead(file, null);
314     }
315 
316     /**
317      * @param file the file path
318      * @param encoding the wanted encoding
319      * @return the file content using the specified encoding.
320      * @throws IOException if any
321      */
322     public static String fileRead(File file, String encoding) throws IOException {
323         return fileRead(file.toPath(), encoding);
324     }
325 
326     /**
327      * Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform
328      * encoding
329      *
330      * @param fileName The path of the file to write.
331      * @param data The content to write to the file.
332      * @throws IOException if any
333      * @deprecated use {@code java.nio.files.Files.write(filename, data.getBytes(encoding),
334      *     StandardOpenOption.APPEND, StandardOpenOption.CREATE)}
335      */
336     public static void fileAppend(String fileName, String data) throws IOException {
337         fileAppend(fileName, null, data);
338     }
339 
340     /**
341      * Appends data to a file. The file will be created if it does not exist.
342      *
343      * @param fileName The path of the file to write.
344      * @param encoding The encoding of the file.
345      * @param data The content to write to the file.
346      * @throws IOException if any
347      * @deprecated use {@code java.nio.files.Files.write(filename, data.getBytes(encoding),
348      *     StandardOpenOption.APPEND, StandardOpenOption.CREATE)}
349      */
350     public static void fileAppend(String fileName, String encoding, String data) throws IOException {
351         fileAppend(Paths.get(fileName), encoding, data);
352     }
353 
354     private static void fileAppend(Path path, String encoding, String data) throws IOException {
355         fileWrite(path, encoding, data, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
356     }
357 
358     /**
359      * Writes data to a file. The file will be created if it does not exist. Note: the data is written with platform
360      * encoding
361      *
362      * @param fileName The path of the file to write.
363      * @param data The content to write to the file.
364      * @throws IOException if any
365      */
366     public static void fileWrite(String fileName, String data) throws IOException {
367         fileWrite(fileName, null, data);
368     }
369 
370     /**
371      * Writes data to a file. The file will be created if it does not exist.
372      *
373      * @param fileName The path of the file to write.
374      * @param encoding The encoding of the file.
375      * @param data The content to write to the file.
376      * @throws IOException if any
377      */
378     public static void fileWrite(String fileName, String encoding, String data) throws IOException {
379         Path file = (fileName == null) ? null : Paths.get(fileName);
380         fileWrite(file, encoding, data);
381     }
382 
383     /**
384      * Writes data to a file. The file will be created if it does not exist. Note: the data is written with platform
385      * encoding
386      *
387      * @param file The file to write.
388      * @param data The content to write to the file.
389      * @throws IOException if any
390      * @since 2.0.6
391      */
392     public static void fileWrite(File file, String data) throws IOException {
393         fileWrite(file, null, data);
394     }
395 
396     /**
397      * Writes data to a file. The file will be created if it does not exist.
398      *
399      * @param file The file to write.
400      * @param encoding The encoding of the file.
401      * @param data The content to write to the file.
402      * @throws IOException if any
403      * @since 2.0.6
404      */
405     public static void fileWrite(File file, String encoding, String data) throws IOException {
406         fileWrite(file.toPath(), encoding, data);
407     }
408 
409     /**
410      * Deletes a file.
411      *
412      * @param fileName The path of the file to delete.
413      */
414     public static void fileDelete(String fileName) {
415         File file = new File(fileName);
416         try {
417             NioFiles.deleteIfExists(file);
418         } catch (IOException e) {
419             throw new RuntimeException(e);
420         }
421     }
422 
423     /**
424      * Waits for NFS to propagate a file creation, imposing a timeout.
425      *
426      * @param fileName The path of the file.
427      * @param seconds The maximum time in seconds to wait.
428      * @return True if file exists.
429      */
430     public static boolean waitFor(String fileName, int seconds) {
431         return waitFor(new File(fileName), seconds);
432     }
433 
434     /**
435      * Waits for NFS to propagate a file creation, imposing a timeout.
436      *
437      * @param file The file.
438      * @param seconds The maximum time in seconds to wait.
439      * @return True if file exists.
440      */
441     public static boolean waitFor(File file, int seconds) {
442         int timeout = 0;
443         int tick = 0;
444         while (!file.exists()) {
445             if (tick++ >= 10) {
446                 tick = 0;
447                 if (timeout++ > seconds) {
448                     return false;
449                 }
450             }
451             try {
452                 Thread.sleep(100);
453             } catch (InterruptedException ignore) {
454                 // nop
455             }
456         }
457         return true;
458     }
459 
460     /**
461      * Creates a file handle.
462      *
463      * @param fileName The path of the file.
464      * @return A <code>File</code> manager.
465      */
466     public static File getFile(String fileName) {
467         return new File(fileName);
468     }
469 
470     /**
471      * <p>Given a directory and an array of extensions return an array of compliant files.</p>
472      *
473      * <p>TODO Should an ignore list be passed in? TODO Should a recurse flag be passed in?</p>
474      *
475      * <p>The given extensions should be like "java" and not like ".java"</p>
476      *
477      * @param directory The path of the directory.
478      * @param extensions an array of expected extensions.
479      * @return An array of files for the wanted extensions.
480      */
481     public static String[] getFilesFromExtension(String directory, String[] extensions) {
482         List<String> files = new ArrayList<String>();
483 
484         File currentDir = new File(directory);
485 
486         String[] unknownFiles = currentDir.list();
487 
488         if (unknownFiles == null) {
489             return new String[0];
490         }
491 
492         for (String unknownFile : unknownFiles) {
493             String currentFileName = directory + System.getProperty("file.separator") + unknownFile;
494             File currentFile = new File(currentFileName);
495 
496             if (currentFile.isDirectory()) {
497                 // ignore all CVS directories...
498                 if (currentFile.getName().equals("CVS")) {
499                     continue;
500                 }
501 
502                 // ok... transverse into this directory and get all the files... then combine
503                 // them with the current list.
504 
505                 String[] fetchFiles = getFilesFromExtension(currentFileName, extensions);
506                 files = blendFilesToVector(files, fetchFiles);
507             } else {
508                 // ok... add the file
509 
510                 String add = currentFile.getAbsolutePath();
511                 if (isValidFile(add, extensions)) {
512                     files.add(add);
513                 }
514             }
515         }
516 
517         // ok... move the Vector into the files list...
518         return files.toArray(new String[0]);
519     }
520 
521     /**
522      * Private helper method for getFilesFromExtension()
523      */
524     private static List<String> blendFilesToVector(List<String> v, String[] files) {
525         for (String file : files) {
526             v.add(file);
527         }
528 
529         return v;
530     }
531 
532     /**
533      * Checks to see if a file is of a particular type(s). Note that if the file does not have an extension, an empty
534      * string (&quot;&quot;) is matched for.
535      */
536     private static boolean isValidFile(String file, String[] extensions) {
537         String extension = extension(file);
538         if (extension == null) {
539             extension = "";
540         }
541 
542         // ok.. now that we have the "extension" go through the current know
543         // excepted extensions and determine if this one is OK.
544 
545         for (String extension1 : extensions) {
546             if (extension1.equals(extension)) {
547                 return true;
548             }
549         }
550 
551         return false;
552     }
553 
554     /**
555      * Simple way to make a directory
556      *
557      * @param dir the directory to create
558      * @throws IllegalArgumentException if the dir contains illegal Windows characters under Windows OS.
559      * @see #INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
560      */
561     public static void mkdir(String dir) {
562         File file = new File(dir);
563 
564         if (Os.isFamily(Os.FAMILY_WINDOWS) && !isValidWindowsFileName(file)) {
565             throw new IllegalArgumentException("The file (" + dir
566                     + ") cannot contain any of the following characters: \n"
567                     + StringUtils.join(INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME, " "));
568         }
569 
570         if (!file.exists()) {
571             file.mkdirs();
572         }
573     }
574 
575     /**
576      * Compare the contents of two files to determine if they are equal or not.
577      *
578      * @param file1 the first file
579      * @param file2 the second file
580      * @return true if the content of the files are equal or they both don't exist, false otherwise
581      * @throws IOException if any
582      */
583     public static boolean contentEquals(final File file1, final File file2) throws IOException {
584         final boolean file1Exists = file1.exists();
585         if (file1Exists != file2.exists()) {
586             return false;
587         }
588 
589         if (!file1Exists) {
590             // two not existing files are equal
591             return true;
592         }
593 
594         if (file1.isDirectory() || file2.isDirectory()) {
595             // don't want to compare directory contents
596             return false;
597         }
598 
599         try (InputStream input1 = Files.newInputStream(file1.toPath());
600                 InputStream input2 = Files.newInputStream(file2.toPath())) {
601             return IOUtil.contentEquals(input1, input2);
602         }
603     }
604 
605     /**
606      * Convert from a <code>URL</code> to a <code>File</code>.
607      *
608      * @param url File URL.
609      * @return The equivalent <code>File</code> object, or <code>null</code> if the URL's protocol is not
610      *         <code>file</code>
611      */
612     public static File toFile(final URL url) {
613         if (url == null || !url.getProtocol().equalsIgnoreCase("file")) {
614             return null;
615         }
616 
617         String filename = url.getFile().replace('/', File.separatorChar);
618         int pos = -1;
619         while ((pos = filename.indexOf('%', pos + 1)) >= 0) {
620             if (pos + 2 < filename.length()) {
621                 String hexStr = filename.substring(pos + 1, pos + 3);
622                 char ch = (char) Integer.parseInt(hexStr, 16);
623                 filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
624             }
625         }
626         return new File(filename);
627     }
628 
629     /**
630      * Convert the array of Files into a list of URLs.
631      *
632      * @param files the array of files
633      * @return the array of URLs
634      * @throws IOException if an error occurs
635      */
636     public static URL[] toURLs(final File[] files) throws IOException {
637         final URL[] urls = new URL[files.length];
638 
639         for (int i = 0; i < urls.length; i++) {
640             urls[i] = files[i].toURI().toURL();
641         }
642 
643         return urls;
644     }
645 
646     /**
647      * Remove extension from filename. ie
648      *
649      * <pre>
650      * foo.txt    --&gt; foo
651      * a\b\c.jpg  --&gt; a\b\c
652      * a\b\c      --&gt; a\b\c
653      * </pre>
654      *
655      * @param filename the path of the file
656      * @return the filename minus extension
657      */
658     public static String removeExtension(final String filename) {
659         String ext = extension(filename);
660 
661         if ("".equals(ext)) {
662             return filename;
663         }
664 
665         final int index = filename.lastIndexOf(ext) - 1;
666         return filename.substring(0, index);
667     }
668 
669     /**
670      * Get extension from filename. ie
671      *
672      * <pre>
673      * foo.txt    --&gt; "txt"
674      * a\b\c.jpg  --&gt; "jpg"
675      * a\b\c      --&gt; ""
676      * </pre>
677      *
678      * @param filename the path of the file
679      * @return the extension of filename or "" if none
680      */
681     public static String getExtension(final String filename) {
682         return extension(filename);
683     }
684 
685     /**
686      * Remove path from filename. Equivalent to the unix command <code>basename</code> ie.
687      *
688      * <pre>
689      * a/b/c.txt --&gt; c.txt
690      * a.txt     --&gt; a.txt
691      * </pre>
692      *
693      * @param filepath the path of the file
694      * @return the filename minus path
695      */
696     public static String removePath(final String filepath) {
697         return removePath(filepath, File.separatorChar);
698     }
699 
700     /**
701      * Remove path from filename. ie.
702      *
703      * <pre>
704      * a/b/c.txt --&gt; c.txt
705      * a.txt     --&gt; a.txt
706      * </pre>
707      *
708      * @param filepath the path of the file
709      * @param fileSeparatorChar the file separator character like <b>/</b> on Unix platforms.
710      * @return the filename minus path
711      */
712     public static String removePath(final String filepath, final char fileSeparatorChar) {
713         final int index = filepath.lastIndexOf(fileSeparatorChar);
714 
715         if (-1 == index) {
716             return filepath;
717         }
718 
719         return filepath.substring(index + 1);
720     }
721 
722     /**
723      * Get path from filename. Roughly equivalent to the unix command <code>dirname</code>. ie.
724      *
725      * <pre>
726      * a/b/c.txt --&gt; a/b
727      * a.txt     --&gt; ""
728      * </pre>
729      *
730      * @param filepath the filepath
731      * @return the filename minus path
732      */
733     public static String getPath(final String filepath) {
734         return getPath(filepath, File.separatorChar);
735     }
736 
737     /**
738      * Get path from filename. ie.
739      *
740      * <pre>
741      * a/b/c.txt --&gt; a/b
742      * a.txt     --&gt; ""
743      * </pre>
744      *
745      * @param filepath the filepath
746      * @param fileSeparatorChar the file separator character like <b>/</b> on Unix platforms.
747      * @return the filename minus path
748      */
749     public static String getPath(final String filepath, final char fileSeparatorChar) {
750         final int index = filepath.lastIndexOf(fileSeparatorChar);
751         if (-1 == index) {
752             return "";
753         }
754 
755         return filepath.substring(0, index);
756     }
757 
758     /**
759      * Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it (and any parent
760      * directories) will be created. If a file <code>source</code> in <code>destinationDirectory</code> exists, it will
761      * be overwritten.
762      *
763      * @param source An existing <code>File</code> to copy.
764      * @param destinationDirectory A directory to copy <code>source</code> into.
765      * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
766      * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
767      * @throws IOException if <code>source</code> does not exist, the file in <code>destinationDirectory</code> cannot
768      *             be written to, or an IO error occurs during copying.
769      */
770     public static void copyFileToDirectory(final String source, final String destinationDirectory) throws IOException {
771         copyFileToDirectory(new File(source), new File(destinationDirectory));
772     }
773 
774     /**
775      * Copy file from source to destination only if source is newer than the target file. If
776      * <code>destinationDirectory</code> does not exist, it (and any parent directories) will be created. If a file
777      * <code>source</code> in <code>destinationDirectory</code> exists, it will be overwritten.
778      *
779      * @param source An existing <code>File</code> to copy.
780      * @param destinationDirectory A directory to copy <code>source</code> into.
781      * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
782      * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
783      * @throws IOException if <code>source</code> does not exist, the file in <code>destinationDirectory</code> cannot
784      *             be written to, or an IO error occurs during copying.
785      */
786     public static void copyFileToDirectoryIfModified(final String source, final String destinationDirectory)
787             throws IOException {
788         copyFileToDirectoryIfModified(new File(source), new File(destinationDirectory));
789     }
790 
791     /**
792      * Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it (and any parent
793      * directories) will be created. If a file <code>source</code> in <code>destinationDirectory</code> exists, it will
794      * be overwritten.
795      *
796      * @param source An existing <code>File</code> to copy.
797      * @param destinationDirectory A directory to copy <code>source</code> into.
798      * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
799      * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
800      * @throws IOException if <code>source</code> does not exist, the file in <code>destinationDirectory</code> cannot
801      *             be written to, or an IO error occurs during copying.
802      */
803     public static void copyFileToDirectory(final File source, final File destinationDirectory) throws IOException {
804         if (destinationDirectory.exists() && !destinationDirectory.isDirectory()) {
805             throw new IllegalArgumentException("Destination is not a directory");
806         }
807 
808         copyFile(source, new File(destinationDirectory, source.getName()));
809     }
810 
811     /**
812      * Copy file from source to destination only if source is newer than the target file. If
813      * <code>destinationDirectory</code> does not exist, it (and any parent directories) will be created. If a file
814      * <code>source</code> in <code>destinationDirectory</code> exists, it will be overwritten.
815      *
816      * @param source An existing <code>File</code> to copy.
817      * @param destinationDirectory A directory to copy <code>source</code> into.
818      * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
819      * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
820      * @throws IOException if <code>source</code> does not exist, the file in <code>destinationDirectory</code> cannot
821      *             be written to, or an IO error occurs during copying.
822      */
823     public static void copyFileToDirectoryIfModified(final File source, final File destinationDirectory)
824             throws IOException {
825         if (destinationDirectory.exists() && !destinationDirectory.isDirectory()) {
826             throw new IllegalArgumentException("Destination is not a directory");
827         }
828 
829         copyFileIfModified(source, new File(destinationDirectory, source.getName()));
830     }
831 
832     /**
833      * Creates a number of directories, as delivered from DirectoryScanner
834      *
835      * @param sourceBase The basedir used for the directory scan
836      * @param dirs The getIncludedDirs from the dirscanner
837      * @param destination The base dir of the output structure
838      * @throws IOException io issue
839      */
840     public static void mkDirs(final File sourceBase, String[] dirs, final File destination) throws IOException {
841         for (String dir : dirs) {
842             File src = new File(sourceBase, dir);
843             File dst = new File(destination, dir);
844             if (NioFiles.isSymbolicLink(src)) {
845                 File target = NioFiles.readSymbolicLink(src);
846                 NioFiles.createSymbolicLink(dst, target);
847             } else {
848                 dst.mkdirs();
849             }
850         }
851     }
852 
853     /**
854      * Copy file from source to destination. The directories up to <code>destination</code> will be created if they
855      * don't already exist. <code>destination</code> will be overwritten if it already exists.
856      *
857      * @param source An existing non-directory <code>File</code> to copy bytes from.
858      * @param destination A non-directory <code>File</code> to write bytes to (possibly overwriting).
859      * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be written to, or an
860      *             IO error occurs during copying.
861      * @throws java.io.FileNotFoundException if <code>destination</code> is a directory (use
862      *             {@link #copyFileToDirectory}).
863      */
864     public static void copyFile(final File source, final File destination) throws IOException {
865         // check source exists
866         if (!source.exists()) {
867             final String message = "File " + source + " does not exist";
868             throw new IOException(message);
869         }
870 
871         // check source != destination, see PLXUTILS-10
872         if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
873             // if they are equal, we can exit the method without doing any work
874             return;
875         }
876         mkdirsFor(destination);
877 
878         doCopyFile(source, destination);
879 
880         if (source.length() != destination.length()) {
881             String message = "Failed to copy full contents from " + source + " to " + destination;
882             throw new IOException(message);
883         }
884     }
885 
886     private static void doCopyFile(File source, File destination) throws IOException {
887         doCopyFileUsingNewIO(source, destination);
888     }
889 
890     private static void doCopyFileUsingNewIO(File source, File destination) throws IOException {
891         NioFiles.copy(source, destination);
892     }
893 
894     /**
895      * Link file from destination to source. The directories up to <code>destination</code> will be created if they
896      * don't already exist. <code>destination</code> will be overwritten if it already exists.
897      *
898      * @param source An existing non-directory <code>File</code> to link to.
899      * @param destination A non-directory <code>File</code> becoming the link (possibly overwriting).
900      * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be created, or an
901      *             IO error occurs during linking.
902      * @throws java.io.FileNotFoundException if <code>destination</code> is a directory (use
903      *             {@link #copyFileToDirectory}).
904      */
905     public static void linkFile(final File source, final File destination) throws IOException {
906         // check source exists
907         if (!source.exists()) {
908             final String message = "File " + source + " does not exist";
909             throw new IOException(message);
910         }
911 
912         // check source != destination, see PLXUTILS-10
913         if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
914             // if they are equal, we can exit the method without doing any work
915             return;
916         }
917         mkdirsFor(destination);
918 
919         NioFiles.createSymbolicLink(destination, source);
920     }
921 
922     /**
923      * Copy file from source to destination only if source timestamp is later than the destination timestamp. The
924      * directories up to <code>destination</code> will be created if they don't already exist. <code>destination</code>
925      * will be overwritten if it already exists.
926      *
927      * @param source An existing non-directory <code>File</code> to copy bytes from.
928      * @param destination A non-directory <code>File</code> to write bytes to (possibly overwriting).
929      * @return true if no problem occured
930      * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be written to, or an
931      *             IO error occurs during copying.
932      */
933     public static boolean copyFileIfModified(final File source, final File destination) throws IOException {
934         if (isSourceNewerThanDestination(source, destination)) {
935             copyFile(source, destination);
936 
937             return true;
938         }
939 
940         return false;
941     }
942 
943     /**
944      * Copies bytes from the URL <code>source</code> to a file <code>destination</code>. The directories up to
945      * <code>destination</code> will be created if they don't already exist. <code>destination</code> will be
946      * overwritten if it already exists.
947      *
948      * @param source A <code>URL</code> to copy bytes from.
949      * @param destination A non-directory <code>File</code> to write bytes to (possibly overwriting).
950      * @throws IOException if
951      *             <ul>
952      *             <li><code>source</code> URL cannot be opened</li>
953      *             <li><code>destination</code> cannot be written to</li>
954      *             <li>an IO error occurs during copying</li>
955      *             </ul>
956      */
957     public static void copyURLToFile(final URL source, final File destination) throws IOException {
958         copyStreamToFile(new URLInputStreamFacade(source), destination);
959     }
960 
961     /**
962      * Copies bytes from the {@link InputStream} <code>source</code> to a file <code>destination</code>. The directories
963      * up to <code>destination</code> will be created if they don't already exist. <code>destination</code> will be
964      * overwritten if it already exists.
965      *
966      * @param source An {@link InputStream} to copy bytes from. This stream is guaranteed to be closed.
967      * @param destination A non-directory <code>File</code> to write bytes to (possibly overwriting).
968      * @throws IOException if
969      *             <ul>
970      *             <li><code>source</code> URL cannot be opened</li>
971      *             <li><code>destination</code> cannot be written to</li>
972      *             <li>an IO error occurs during copying</li>
973      *             </ul>
974      */
975     public static void copyStreamToFile(final InputStreamFacade source, final File destination) throws IOException {
976         mkdirsFor(destination);
977         checkCanWrite(destination);
978 
979         try (InputStream input = source.getInputStream();
980                 OutputStream output = Files.newOutputStream(destination.toPath())) {
981             IOUtil.copy(input, output);
982         }
983     }
984 
985     private static void checkCanWrite(File destination) throws IOException {
986         // make sure we can write to destination
987         if (destination.exists() && !destination.canWrite()) {
988             final String message = "Unable to open file " + destination + " for writing.";
989             throw new IOException(message);
990         }
991     }
992 
993     private static void mkdirsFor(File destination) {
994         // does destination directory exist ?
995         File parentFile = destination.getParentFile();
996         if (parentFile != null && !parentFile.exists()) {
997             parentFile.mkdirs();
998         }
999     }
1000 
1001     /**
1002      * Normalize a path. Eliminates "/../" and "/./" in a string. Returns <code>null</code> if the ..'s went past the
1003      * root. Eg:
1004      *
1005      * <pre>
1006      * /foo//               --&gt;     /foo/
1007      * /foo/./              --&gt;     /foo/
1008      * /foo/../bar          --&gt;     /bar
1009      * /foo/../bar/         --&gt;     /bar/
1010      * /foo/../bar/../baz   --&gt;     /baz
1011      * //foo//./bar         --&gt;     /foo/bar
1012      * /../                 --&gt;     null
1013      * </pre>
1014      *
1015      * @param path the path to normalize
1016      * @return the normalized String, or <code>null</code> if too many ..'s.
1017      */
1018     public static String normalize(final String path) {
1019         String normalized = path;
1020         // Resolve occurrences of "//" in the normalized path
1021         while (true) {
1022             int index = normalized.indexOf("//");
1023             if (index < 0) {
1024                 break;
1025             }
1026             normalized = normalized.substring(0, index) + normalized.substring(index + 1);
1027         }
1028 
1029         // Resolve occurrences of "/./" in the normalized path
1030         while (true) {
1031             int index = normalized.indexOf("/./");
1032             if (index < 0) {
1033                 break;
1034             }
1035             normalized = normalized.substring(0, index) + normalized.substring(index + 2);
1036         }
1037 
1038         // Resolve occurrences of "/../" in the normalized path
1039         while (true) {
1040             int index = normalized.indexOf("/../");
1041             if (index < 0) {
1042                 break;
1043             }
1044             if (index == 0) {
1045                 return null; // Trying to go outside our context
1046             }
1047             int index2 = normalized.lastIndexOf('/', index - 1);
1048             normalized = normalized.substring(0, index2) + normalized.substring(index + 3);
1049         }
1050 
1051         // Return the normalized path that we have completed
1052         return normalized;
1053     }
1054 
1055     /**
1056      * <p>Will concatenate 2 paths. Paths with <code>..</code> will be properly handled.</p>
1057      *
1058      * Eg.,
1059      * <pre>
1060      * /a/b/c + d = /a/b/d
1061      * /a/b/c + ../d = /a/d
1062      * </pre>
1063      *
1064      * <p>Thieved from Tomcat sources...</p>
1065      *
1066      * @param lookupPath a path
1067      * @param path the path to concatenate
1068      * @return The concatenated paths, or null if error occurs
1069      */
1070     public static String catPath(final String lookupPath, final String path) {
1071         // Cut off the last slash and everything beyond
1072         int index = lookupPath.lastIndexOf("/");
1073         String lookup = lookupPath.substring(0, index);
1074         String pth = path;
1075 
1076         // Deal with .. by chopping dirs off the lookup path
1077         while (pth.startsWith("../")) {
1078             if (lookup.length() > 0) {
1079                 index = lookup.lastIndexOf("/");
1080                 lookup = lookup.substring(0, index);
1081             } else {
1082                 // More ..'s than dirs, return null
1083                 return null;
1084             }
1085 
1086             index = pth.indexOf("../") + 3;
1087             pth = pth.substring(index);
1088         }
1089 
1090         return new StringBuffer(lookup).append("/").append(pth).toString();
1091     }
1092 
1093     /**
1094      * Resolve a file <code>filename</code> to it's canonical form. If <code>filename</code> is relative (doesn't start
1095      * with <code>/</code>), it will be resolved relative to <code>baseFile</code>, otherwise it is treated as a normal
1096      * root-relative path.
1097      *
1098      * @param baseFile Where to resolve <code>filename</code> from, if <code>filename</code> is relative.
1099      * @param filename Absolute or relative file path to resolve.
1100      * @return The canonical <code>File</code> of <code>filename</code>.
1101      */
1102     public static File resolveFile(final File baseFile, String filename) {
1103         String filenm = filename;
1104         if ('/' != File.separatorChar) {
1105             filenm = filename.replace('/', File.separatorChar);
1106         }
1107 
1108         if ('\\' != File.separatorChar) {
1109             filenm = filename.replace('\\', File.separatorChar);
1110         }
1111 
1112         // deal with absolute files
1113         if (filenm.startsWith(File.separator) || (Os.isFamily(Os.FAMILY_WINDOWS) && filenm.indexOf(":") > 0)) {
1114             File file = new File(filenm);
1115 
1116             try {
1117                 file = file.getCanonicalFile();
1118             } catch (final IOException ioe) {
1119                 // nop
1120             }
1121 
1122             return file;
1123         }
1124         // FIXME: I'm almost certain this // removal is unnecessary, as getAbsoluteFile() strips
1125         // them. However, I'm not sure about this UNC stuff. (JT)
1126         final char[] chars = filename.toCharArray();
1127         final StringBuilder sb = new StringBuilder();
1128 
1129         // remove duplicate file separators in succession - except
1130         // on win32 at start of filename as UNC filenames can
1131         // be \\AComputer\AShare\myfile.txt
1132         int start = 0;
1133         if ('\\' == File.separatorChar) {
1134             sb.append(filenm.charAt(0));
1135             start++;
1136         }
1137 
1138         for (int i = start; i < chars.length; i++) {
1139             final boolean doubleSeparator = File.separatorChar == chars[i] && File.separatorChar == chars[i - 1];
1140 
1141             if (!doubleSeparator) {
1142                 sb.append(chars[i]);
1143             }
1144         }
1145 
1146         filenm = sb.toString();
1147 
1148         // must be relative
1149         File file = (new File(baseFile, filenm)).getAbsoluteFile();
1150 
1151         try {
1152             file = file.getCanonicalFile();
1153         } catch (final IOException ioe) {
1154             // nop
1155         }
1156 
1157         return file;
1158     }
1159 
1160     /**
1161      * Delete a file. If file is directory delete it and all sub-directories.
1162      *
1163      * @param file the file path
1164      * @throws IOException if any
1165      */
1166     public static void forceDelete(final String file) throws IOException {
1167         forceDelete(new File(file));
1168     }
1169 
1170     /**
1171      * Delete a file. If file is directory delete it and all sub-directories.
1172      *
1173      * @param file a file
1174      * @throws IOException if any
1175      */
1176     public static void forceDelete(final File file) throws IOException {
1177         if (file.isDirectory()) {
1178             deleteDirectory(file);
1179         } else {
1180             if (!deleteFile(file)) {
1181                 throw new IOException("File " + file + " unable to be deleted.");
1182             }
1183         }
1184     }
1185 
1186     /**
1187      * Deletes a file, or a symlink without following it. A file that does not exist (including a dangling symlink,
1188      * which {@link Files#deleteIfExists} removes) counts as deleted and returns immediately. Only if the file still
1189      * exists after a failed attempt, accommodate the Windows bug encountered in both Sun and IBM JDKs: call
1190      * System.gc(), wait a little and try again.
1191      *
1192      * @param file a file
1193      * @return {@code true} if the file no longer exists
1194      * @throws IOException if any
1195      */
1196     private static boolean deleteFile(File file) throws IOException {
1197         if (file.isDirectory()) {
1198             throw new IOException("File " + file + " isn't a file.");
1199         }
1200 
1201         final Path path = file.toPath();
1202         try {
1203             Files.deleteIfExists(path);
1204             return true;
1205         } catch (IOException e) {
1206             if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
1207                 return true;
1208             }
1209         }
1210 
1211         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
1212             System.gc();
1213         }
1214 
1215         try {
1216             Thread.sleep(10);
1217         } catch (InterruptedException ignore) {
1218             Thread.currentThread().interrupt();
1219         }
1220 
1221         try {
1222             Files.deleteIfExists(path);
1223             return true;
1224         } catch (IOException e) {
1225             return !Files.exists(path, LinkOption.NOFOLLOW_LINKS);
1226         }
1227     }
1228 
1229     /**
1230      * Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.
1231      *
1232      * @param file a file
1233      * @throws IOException if any
1234      */
1235     public static void forceDeleteOnExit(final File file) throws IOException {
1236         if (!file.exists()) {
1237             return;
1238         }
1239 
1240         if (file.isDirectory()) {
1241             deleteDirectoryOnExit(file);
1242         } else {
1243             file.deleteOnExit();
1244         }
1245     }
1246 
1247     /**
1248      * Recursively schedule directory for deletion on JVM exit.
1249      *
1250      * @param directory a directory
1251      * @throws IOException if any
1252      */
1253     private static void deleteDirectoryOnExit(final File directory) throws IOException {
1254         if (!directory.exists()) {
1255             return;
1256         }
1257         directory.deleteOnExit(); // The hook reverses the list
1258 
1259         if (!NioFiles.isSymbolicLink(directory)) {
1260             cleanDirectoryOnExit(directory);
1261         }
1262     }
1263 
1264     /**
1265      * Clean a directory without deleting it.
1266      *
1267      * @param directory a directory
1268      * @throws IOException if any
1269      */
1270     private static void cleanDirectoryOnExit(final File directory) throws IOException {
1271         if (!directory.exists()) {
1272             final String message = directory + " does not exist";
1273             throw new IllegalArgumentException(message);
1274         }
1275 
1276         if (!directory.isDirectory()) {
1277             final String message = directory + " is not a directory";
1278             throw new IllegalArgumentException(message);
1279         }
1280 
1281         IOException exception = null;
1282 
1283         final File[] files = directory.listFiles();
1284         if (files == null) {
1285             throw new IOException("Could not list directory " + directory.getAbsolutePath());
1286         }
1287         for (final File file : files) {
1288             try {
1289                 forceDeleteOnExit(file);
1290             } catch (final IOException ioe) {
1291                 exception = ioe;
1292             }
1293         }
1294 
1295         if (null != exception) {
1296             throw exception;
1297         }
1298     }
1299 
1300     /**
1301      * Make a directory.
1302      *
1303      * @param file not null
1304      * @throws IOException If there already exists a file with specified name or the directory is unable to be created
1305      * @throws IllegalArgumentException if the file contains illegal Windows characters under Windows OS.
1306      * @see #INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
1307      */
1308     public static void forceMkdir(final File file) throws IOException {
1309         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
1310             if (!isValidWindowsFileName(file)) {
1311                 throw new IllegalArgumentException("The file (" + file.getAbsolutePath()
1312                         + ") cannot contain any of the following characters: \n"
1313                         + StringUtils.join(INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME, " "));
1314             }
1315         }
1316 
1317         if (file.exists()) {
1318             if (file.isFile()) {
1319                 final String message =
1320                         "File " + file + " exists and is " + "not a directory. Unable to create directory.";
1321                 throw new IOException(message);
1322             }
1323         } else {
1324             if (false == file.mkdirs()) {
1325                 final String message = "Unable to create directory " + file;
1326                 throw new IOException(message);
1327             }
1328         }
1329     }
1330 
1331     /**
1332      * Recursively delete a directory.
1333      *
1334      * @param directory a directory
1335      * @throws IOException if any
1336      */
1337     public static void deleteDirectory(final String directory) throws IOException {
1338         deleteDirectory(new File(directory));
1339     }
1340 
1341     /**
1342      * Recursively delete a directory.
1343      *
1344      * @param directory a directory
1345      * @throws IOException if any
1346      */
1347     public static void deleteDirectory(final File directory) throws IOException {
1348         if (!directory.exists()) {
1349             return;
1350         }
1351 
1352         /*
1353          * try delete the directory before its contents, which will take care of any directories that are really
1354          * symbolic links.
1355          */
1356         if (directory.delete()) {
1357             return;
1358         }
1359 
1360         if (!NioFiles.isSymbolicLink(directory)) {
1361             cleanDirectory(directory);
1362         }
1363         if (!directory.delete()) {
1364             final String message = "Directory " + directory + " unable to be deleted.";
1365             throw new IOException(message);
1366         }
1367     }
1368 
1369     /**
1370      * Clean a directory without deleting it.
1371      *
1372      * @param directory a directory
1373      * @throws IOException if any
1374      */
1375     public static void cleanDirectory(final String directory) throws IOException {
1376         cleanDirectory(new File(directory));
1377     }
1378 
1379     /**
1380      * Clean a directory without deleting it.
1381      *
1382      * @param directory a directory
1383      * @throws IOException if any
1384      */
1385     public static void cleanDirectory(final File directory) throws IOException {
1386         if (!directory.exists()) {
1387             final String message = directory + " does not exist";
1388             throw new IllegalArgumentException(message);
1389         }
1390 
1391         if (!directory.isDirectory()) {
1392             final String message = directory + " is not a directory";
1393             throw new IllegalArgumentException(message);
1394         }
1395 
1396         IOException exception = null;
1397 
1398         final File[] files = directory.listFiles();
1399 
1400         if (files == null) {
1401             return;
1402         }
1403 
1404         for (final File file : files) {
1405             try {
1406                 forceDelete(file);
1407             } catch (final IOException ioe) {
1408                 exception = ioe;
1409             }
1410         }
1411 
1412         if (null != exception) {
1413             throw exception;
1414         }
1415     }
1416 
1417     /**
1418      * Recursively count size of a directory.
1419      *
1420      * @param directory a directory
1421      * @return size of directory in bytes.
1422      */
1423     public static long sizeOfDirectory(final String directory) {
1424         return sizeOfDirectory(new File(directory));
1425     }
1426 
1427     /**
1428      * Recursively count size of a directory.
1429      *
1430      * @param directory a directory
1431      * @return size of directory in bytes.
1432      */
1433     public static long sizeOfDirectory(final File directory) {
1434         if (!directory.exists()) {
1435             final String message = directory + " does not exist";
1436             throw new IllegalArgumentException(message);
1437         }
1438 
1439         if (!directory.isDirectory()) {
1440             final String message = directory + " is not a directory";
1441             throw new IllegalArgumentException(message);
1442         }
1443 
1444         long size = 0;
1445 
1446         final File[] files = directory.listFiles();
1447         for (final File file : files) {
1448             if (file.isDirectory()) {
1449                 size += sizeOfDirectory(file);
1450             } else {
1451                 size += file.length();
1452             }
1453         }
1454 
1455         return size;
1456     }
1457 
1458     /**
1459      * Return the files contained in the directory, using inclusion and exclusion Ant patterns, including the directory
1460      * name in each of the files
1461      *
1462      * @param directory the directory to scan
1463      * @param includes the includes pattern, comma separated
1464      * @param excludes the excludes pattern, comma separated
1465      * @return a list of File objects
1466      * @throws IOException io issue
1467      * @see #getFileNames(File, String, String, boolean)
1468      */
1469     public static List<File> getFiles(File directory, String includes, String excludes) throws IOException {
1470         return getFiles(directory, includes, excludes, true);
1471     }
1472 
1473     /**
1474      * Return the files contained in the directory, using inclusion and exclusion Ant patterns
1475      *
1476      * @param directory the directory to scan
1477      * @param includes the includes pattern, comma separated
1478      * @param excludes the excludes pattern, comma separated
1479      * @param includeBasedir true to include the base dir in each file
1480      * @return a list of File objects
1481      * @throws IOException io issue
1482      * @see #getFileNames(File, String, String, boolean)
1483      */
1484     public static List<File> getFiles(File directory, String includes, String excludes, boolean includeBasedir)
1485             throws IOException {
1486         List<String> fileNames = getFileNames(directory, includes, excludes, includeBasedir);
1487 
1488         List<File> files = new ArrayList<File>();
1489 
1490         for (String filename : fileNames) {
1491             files.add(new File(filename));
1492         }
1493 
1494         return files;
1495     }
1496 
1497     /**
1498      * Return a list of files as String depending options. This method use case sensitive file name.
1499      *
1500      * @param directory the directory to scan
1501      * @param includes the includes pattern, comma separated
1502      * @param excludes the excludes pattern, comma separated
1503      * @param includeBasedir true to include the base dir in each String of file
1504      * @return a list of files as String
1505      * @throws IOException io issue
1506      */
1507     public static List<String> getFileNames(File directory, String includes, String excludes, boolean includeBasedir)
1508             throws IOException {
1509         return getFileNames(directory, includes, excludes, includeBasedir, true);
1510     }
1511 
1512     /**
1513      * Return a list of files as String depending options.
1514      *
1515      * @param directory the directory to scan
1516      * @param includes the includes pattern, comma separated
1517      * @param excludes the excludes pattern, comma separated
1518      * @param includeBasedir true to include the base dir in each String of file
1519      * @param isCaseSensitive true if case sensitive
1520      * @return a list of files as String
1521      * @throws IOException io issue
1522      */
1523     public static List<String> getFileNames(
1524             File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive)
1525             throws IOException {
1526         return getFileAndDirectoryNames(directory, includes, excludes, includeBasedir, isCaseSensitive, true, false);
1527     }
1528 
1529     /**
1530      * Return a list of directories as String depending options. This method use case sensitive file name.
1531      *
1532      * @param directory the directory to scan
1533      * @param includes the includes pattern, comma separated
1534      * @param excludes the excludes pattern, comma separated
1535      * @param includeBasedir true to include the base dir in each String of file
1536      * @return a list of directories as String
1537      * @throws IOException io issue
1538      */
1539     public static List<String> getDirectoryNames(
1540             File directory, String includes, String excludes, boolean includeBasedir) throws IOException {
1541         return getDirectoryNames(directory, includes, excludes, includeBasedir, true);
1542     }
1543 
1544     /**
1545      * Return a list of directories as String depending options.
1546      *
1547      * @param directory the directory to scan
1548      * @param includes the includes pattern, comma separated
1549      * @param excludes the excludes pattern, comma separated
1550      * @param includeBasedir true to include the base dir in each String of file
1551      * @param isCaseSensitive true if case sensitive
1552      * @return a list of directories as String
1553      * @throws IOException io issue
1554      */
1555     public static List<String> getDirectoryNames(
1556             File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive)
1557             throws IOException {
1558         return getFileAndDirectoryNames(directory, includes, excludes, includeBasedir, isCaseSensitive, false, true);
1559     }
1560 
1561     /**
1562      * Return a list of files as String depending options.
1563      *
1564      * @param directory the directory to scan
1565      * @param includes the includes pattern, comma separated
1566      * @param excludes the excludes pattern, comma separated
1567      * @param includeBasedir true to include the base dir in each String of file
1568      * @param isCaseSensitive true if case sensitive
1569      * @param getFiles true if get files
1570      * @param getDirectories true if get directories
1571      * @return a list of files as String
1572      * @throws IOException io issue
1573      */
1574     public static List<String> getFileAndDirectoryNames(
1575             File directory,
1576             String includes,
1577             String excludes,
1578             boolean includeBasedir,
1579             boolean isCaseSensitive,
1580             boolean getFiles,
1581             boolean getDirectories)
1582             throws IOException {
1583         DirectoryScanner scanner = new DirectoryScanner();
1584 
1585         scanner.setBasedir(directory);
1586 
1587         if (includes != null) {
1588             scanner.setIncludes(StringUtils.split(includes, ","));
1589         }
1590 
1591         if (excludes != null) {
1592             scanner.setExcludes(StringUtils.split(excludes, ","));
1593         }
1594 
1595         scanner.setCaseSensitive(isCaseSensitive);
1596 
1597         scanner.scan();
1598 
1599         List<String> list = new ArrayList<String>();
1600 
1601         if (getFiles) {
1602             String[] files = scanner.getIncludedFiles();
1603 
1604             for (String file : files) {
1605                 if (includeBasedir) {
1606                     list.add(directory + FileUtils.FS + file);
1607                 } else {
1608                     list.add(file);
1609                 }
1610             }
1611         }
1612 
1613         if (getDirectories) {
1614             String[] directories = scanner.getIncludedDirectories();
1615 
1616             for (String directory1 : directories) {
1617                 if (includeBasedir) {
1618                     list.add(directory + FileUtils.FS + directory1);
1619                 } else {
1620                     list.add(directory1);
1621                 }
1622             }
1623         }
1624 
1625         return list;
1626     }
1627 
1628     /**
1629      * Copy a directory to an other one.
1630      *
1631      * @param sourceDirectory the source dir
1632      * @param destinationDirectory the target dir
1633      * @throws IOException if any
1634      */
1635     public static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
1636         copyDirectory(sourceDirectory, destinationDirectory, "**", null);
1637     }
1638 
1639     /**
1640      * Copy a directory to an other one.
1641      *
1642      * @param sourceDirectory the source dir
1643      * @param destinationDirectory the target dir
1644      * @param includes include pattern
1645      * @param excludes exclude pattern
1646      * @throws IOException if any
1647      * @see #getFiles(File, String, String)
1648      */
1649     public static void copyDirectory(File sourceDirectory, File destinationDirectory, String includes, String excludes)
1650             throws IOException {
1651         if (!sourceDirectory.exists()) {
1652             return;
1653         }
1654 
1655         List<File> files = getFiles(sourceDirectory, includes, excludes);
1656 
1657         for (File file : files) {
1658             copyFileToDirectory(file, destinationDirectory);
1659         }
1660     }
1661 
1662     /**
1663      * <p>Copies a entire directory layout : no files will be copied only directories</p>
1664      *
1665      * Note:
1666      * <ul>
1667      * <li>It will include empty directories.
1668      * <li>The <code>sourceDirectory</code> must exists.
1669      * </ul>
1670      *
1671      * @param sourceDirectory the source dir
1672      * @param destinationDirectory the target dir
1673      * @param includes include pattern
1674      * @param excludes exclude pattern
1675      * @throws IOException if any
1676      * @since 1.5.7
1677      */
1678     public static void copyDirectoryLayout(
1679             File sourceDirectory, File destinationDirectory, String[] includes, String[] excludes) throws IOException {
1680         if (sourceDirectory == null) {
1681             throw new IOException("source directory can't be null.");
1682         }
1683 
1684         if (destinationDirectory == null) {
1685             throw new IOException("destination directory can't be null.");
1686         }
1687 
1688         if (sourceDirectory.equals(destinationDirectory)) {
1689             throw new IOException("source and destination are the same directory.");
1690         }
1691 
1692         if (!sourceDirectory.exists()) {
1693             throw new IOException("Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ").");
1694         }
1695 
1696         DirectoryScanner scanner = new DirectoryScanner();
1697 
1698         scanner.setBasedir(sourceDirectory);
1699 
1700         if (includes != null && includes.length >= 1) {
1701             scanner.setIncludes(includes);
1702         } else {
1703             scanner.setIncludes(new String[] {"**"});
1704         }
1705 
1706         if (excludes != null && excludes.length >= 1) {
1707             scanner.setExcludes(excludes);
1708         }
1709 
1710         scanner.addDefaultExcludes();
1711         scanner.scan();
1712         List<String> includedDirectories = Arrays.asList(scanner.getIncludedDirectories());
1713 
1714         for (String name : includedDirectories) {
1715             File source = new File(sourceDirectory, name);
1716 
1717             if (source.equals(sourceDirectory)) {
1718                 continue;
1719             }
1720 
1721             File destination = new File(destinationDirectory, name);
1722             destination.mkdirs();
1723         }
1724     }
1725 
1726     /**
1727      * <p>Copies a entire directory structure.</p>
1728      *
1729      * Note:
1730      * <ul>
1731      * <li>It will include empty directories.
1732      * <li>The <code>sourceDirectory</code> must exists.
1733      * </ul>
1734      *
1735      * @param sourceDirectory the source dir
1736      * @param destinationDirectory the target dir
1737      * @throws IOException if any
1738      */
1739     public static void copyDirectoryStructure(File sourceDirectory, File destinationDirectory) throws IOException {
1740         copyDirectoryStructure(sourceDirectory, destinationDirectory, destinationDirectory, false);
1741     }
1742 
1743     /**
1744      * <p>Copies an entire directory structure but only source files with timestamp later than the destinations'.</p>
1745      *
1746      * Note:
1747      * <ul>
1748      * <li>It will include empty directories.
1749      * <li>The <code>sourceDirectory</code> must exists.
1750      * </ul>
1751      *
1752      * @param sourceDirectory the source dir
1753      * @param destinationDirectory the target dir
1754      * @throws IOException if any
1755      */
1756     public static void copyDirectoryStructureIfModified(File sourceDirectory, File destinationDirectory)
1757             throws IOException {
1758         copyDirectoryStructure(sourceDirectory, destinationDirectory, destinationDirectory, true);
1759     }
1760 
1761     private static void copyDirectoryStructure(
1762             File sourceDirectory, File destinationDirectory, File rootDestinationDirectory, boolean onlyModifiedFiles)
1763             throws IOException {
1764         if (sourceDirectory == null) {
1765             throw new IOException("source directory can't be null.");
1766         }
1767 
1768         if (destinationDirectory == null) {
1769             throw new IOException("destination directory can't be null.");
1770         }
1771 
1772         if (sourceDirectory.equals(destinationDirectory)) {
1773             throw new IOException("source and destination are the same directory.");
1774         }
1775 
1776         if (!sourceDirectory.exists()) {
1777             throw new IOException("Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ").");
1778         }
1779 
1780         File[] files = sourceDirectory.listFiles();
1781         if (files == null) {
1782             throw new IOException("Could not list directory " + sourceDirectory.getAbsolutePath());
1783         }
1784 
1785         String sourcePath = sourceDirectory.getAbsolutePath();
1786 
1787         for (File file : files) {
1788             if (file.equals(rootDestinationDirectory)) {
1789                 // We don't copy the destination directory in itself
1790                 continue;
1791             }
1792 
1793             String dest = file.getAbsolutePath();
1794 
1795             dest = dest.substring(sourcePath.length() + 1);
1796 
1797             File destination = new File(destinationDirectory, dest);
1798 
1799             if (file.isFile()) {
1800                 destination = destination.getParentFile();
1801 
1802                 if (onlyModifiedFiles) {
1803                     copyFileToDirectoryIfModified(file, destination);
1804                 } else {
1805                     copyFileToDirectory(file, destination);
1806                 }
1807             } else if (file.isDirectory()) {
1808                 if (!destination.exists() && !destination.mkdirs()) {
1809                     throw new IOException(
1810                             "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
1811                 }
1812 
1813                 copyDirectoryStructure(file, destination, rootDestinationDirectory, onlyModifiedFiles);
1814             } else {
1815                 throw new IOException("Unknown file type: " + file.getAbsolutePath());
1816             }
1817         }
1818     }
1819 
1820     /**
1821      * <p>Renames a file, even if that involves crossing file system boundaries.</p>
1822      *
1823      * <p>This will remove <code>to</code> (if it exists), ensure that <code>to</code>'s parent directory exists and move
1824      * <code>from</code>, which involves deleting <code>from</code> as well.</p>
1825      *
1826      * @param from the file to move
1827      * @param to the new file name
1828      * @throws IOException if anything bad happens during this process. Note that <code>to</code> may have been deleted
1829      *             already when this happens.
1830      */
1831     public static void rename(File from, File to) throws IOException {
1832         if (to.exists() && !to.delete()) {
1833             throw new IOException("Failed to delete " + to + " while trying to rename " + from);
1834         }
1835 
1836         File parent = to.getParentFile();
1837         if (parent != null && !parent.exists() && !parent.mkdirs()) {
1838             throw new IOException("Failed to create directory " + parent + " while trying to rename " + from);
1839         }
1840 
1841         if (!from.renameTo(to)) {
1842             copyFile(from, to);
1843             if (!from.delete()) {
1844                 throw new IOException("Failed to delete " + from + " while trying to rename it.");
1845             }
1846         }
1847     }
1848 
1849     /**
1850      * <p>Create a temporary file in a given directory.</p>
1851      *
1852      * <p>The file denoted by the returned abstract pathname did not exist before this method was invoked, any subsequent
1853      * invocation of this method will yield a different file name.</p>
1854      *
1855      * <p>The filename is prefixNNNNNsuffix where NNNN is a random number</p>
1856      *
1857      * <p>This method is different to {@link File#createTempFile(String, String, File)} of JDK 1.2 as it doesn't create the
1858      * file itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is null.</p>
1859      *
1860      * <p>To delete automatically the file created by this method, use the {@link File#deleteOnExit()} method.</p>
1861      *
1862      * @param prefix prefix before the random number
1863      * @param suffix file extension; include the '.'
1864      * @param parentDir Directory to create the temporary file in <code>-java.io.tmpdir</code> used if not specificed
1865      * @return a File reference to the new temporary file.
1866      */
1867     public static File createTempFile(String prefix, String suffix, File parentDir) {
1868         File result = null;
1869         String parent = System.getProperty("java.io.tmpdir");
1870         if (parentDir != null) {
1871             parent = parentDir.getPath();
1872         }
1873         DecimalFormat fmt = new DecimalFormat("#####");
1874         SecureRandom secureRandom = new SecureRandom();
1875         long secureInitializer = secureRandom.nextLong();
1876         Random rand = new Random(secureInitializer + Runtime.getRuntime().freeMemory());
1877         synchronized (rand) {
1878             do {
1879                 result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix);
1880             } while (result.exists());
1881         }
1882 
1883         return result;
1884     }
1885 
1886     /**
1887      * <b>If wrappers is null or empty, the file will be copy only if {@code to.lastModified() < from.lastModified()}</b>
1888      *
1889      * @param from the file to copy
1890      * @param to the destination file
1891      * @param encoding the file output encoding (only if wrappers is not empty)
1892      * @param wrappers array of {@link FilterWrapper}
1893      * @throws IOException if an IO error occurs during copying or filtering
1894      */
1895     public static void copyFile(File from, File to, String encoding, FilterWrapper[] wrappers) throws IOException {
1896         copyFile(from, to, encoding, wrappers, false);
1897     }
1898 
1899     public abstract static class FilterWrapper {
1900         public abstract Reader getReader(Reader fileReader);
1901     }
1902 
1903     /**
1904      * <b>If wrappers is null or empty, the file will be copy only if {@code to.lastModified() < from.lastModified()}, if overwrite is true</b>
1905      *
1906      * @param from the file to copy
1907      * @param to the destination file
1908      * @param encoding the file output encoding (only if wrappers is not empty)
1909      * @param wrappers array of {@link FilterWrapper}
1910      * @param overwrite if true and wrappers is null or empty, the file will be copied even if {@code to.lastModified() < from.lastModified()}
1911      * @throws IOException if an IO error occurs during copying or filtering
1912      * @since 1.5.2
1913      */
1914     public static void copyFile(File from, File to, String encoding, FilterWrapper[] wrappers, boolean overwrite)
1915             throws IOException {
1916         if (wrappers != null && wrappers.length > 0) {
1917             // buffer so it isn't reading a byte at a time!
1918             Reader fileReader = null;
1919             Writer fileWriter = null;
1920             try {
1921                 if (encoding == null || encoding.length() < 1) {
1922                     fileReader = Files.newBufferedReader(from.toPath());
1923                     fileWriter = Files.newBufferedWriter(to.toPath());
1924                 } else {
1925                     OutputStream outstream = Files.newOutputStream(to.toPath());
1926 
1927                     fileReader = Files.newBufferedReader(from.toPath(), Charset.forName(encoding));
1928 
1929                     fileWriter = new OutputStreamWriter(outstream, encoding);
1930                 }
1931 
1932                 Reader reader = fileReader;
1933                 for (FilterWrapper wrapper : wrappers) {
1934                     reader = wrapper.getReader(reader);
1935                 }
1936 
1937                 IOUtil.copy(reader, fileWriter);
1938                 fileWriter.close();
1939                 fileWriter = null;
1940                 fileReader.close();
1941                 fileReader = null;
1942             } finally {
1943                 //noinspection deprecation
1944                 IOUtil.close(fileReader);
1945                 //noinspection deprecation
1946                 IOUtil.close(fileWriter);
1947             }
1948         } else {
1949             if (isSourceNewerThanDestination(from, to) || overwrite) {
1950                 copyFile(from, to);
1951             }
1952         }
1953     }
1954 
1955     private static boolean isSourceNewerThanDestination(File source, File destination) {
1956         return (destination.lastModified() == 0L && source.lastModified() == 0L)
1957                 || destination.lastModified() < source.lastModified();
1958     }
1959 
1960     /**
1961      * Note: the file content is read with platform encoding
1962      *
1963      * @param file the file
1964      * @return a List containing every every line not starting with # and not empty
1965      * @throws IOException if any
1966      */
1967     public static List<String> loadFile(File file) throws IOException {
1968         final List<String> lines = new ArrayList<String>();
1969 
1970         if (file.exists()) {
1971             try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
1972                 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
1973                     line = line.trim();
1974 
1975                     if (!line.startsWith("#") && line.length() != 0) {
1976                         lines.add(line);
1977                     }
1978                 }
1979             }
1980         }
1981 
1982         return lines;
1983     }
1984 
1985     /**
1986      * For Windows OS, check if the file name contains any of the following characters:
1987      * <code>":", "*", "?", "\"", "&lt;", "&gt;", "|"</code>
1988      *
1989      * @param f not null file
1990      * @return <code>false</code> if the file path contains any of forbidden Windows characters, <code>true</code> if
1991      *         the Os is not Windows or if the file path respect the Windows constraints.
1992      * @see #INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
1993      * @since 1.5.2
1994      */
1995     public static boolean isValidWindowsFileName(File f) {
1996         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
1997             if (StringUtils.indexOfAny(f.getName(), INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME) != -1) {
1998                 return false;
1999             }
2000 
2001             File parentFile = f.getParentFile();
2002             if (parentFile != null) {
2003                 return isValidWindowsFileName(parentFile);
2004             }
2005         }
2006 
2007         return true;
2008     }
2009 }