diff --git a/Dorkbox-Util/src/dorkbox/util/FileUtil.java b/Dorkbox-Util/src/dorkbox/util/FileUtil.java index c39b986..2ef0903 100644 --- a/Dorkbox-Util/src/dorkbox/util/FileUtil.java +++ b/Dorkbox-Util/src/dorkbox/util/FileUtil.java @@ -15,16 +15,10 @@ */ package dorkbox.util; -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.RandomAccessFile; -import java.io.Reader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.LinkedList; @@ -32,21 +26,19 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * File related utilities. - * - * + *
+ * * Contains code from FilenameUtils.java (normalize + dependencies) - Apache 2.0 License * http://commons.apache.org/proper/commons-io/ * Copyright 2013 ASF * Authors: Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, - * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, - * Jeremias Maerki, Stephen Colebourne + * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, + * Jeremias Maerki, Stephen Colebourne */ -public class FileUtil { +public +class FileUtil { private static final Logger logger = LoggerFactory.getLogger(FileUtil.class); /** * The Unix separator character. @@ -67,10 +59,12 @@ public class FileUtil { * The separator character that is the opposite of the system separator. */ private static final char OTHER_SEPARATOR; + static { if (OS.isWindows()) { OTHER_SEPARATOR = UNIX_SEPARATOR; - } else { + } + else { OTHER_SEPARATOR = WINDOWS_SEPARATOR; } } @@ -78,14 +72,15 @@ public class FileUtil { public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); - public static byte[] ZIP_HEADER = { 'P', 'K', 0x3, 0x4 }; + public static byte[] ZIP_HEADER = {'P', 'K', 0x3, 0x4}; /** * Renames a file. Windows has all sorts of problems which are worked around. * * @return true if successful, false otherwise */ - public static boolean renameTo(File source, File dest) { + public static + boolean renameTo(File source, File dest) { // if we're on a civilized operating system we may be able to simple // rename it if (source.renameTo(dest)) { @@ -120,6 +115,8 @@ public class FileUtil { fin = new FileInputStream(source); fout = new FileOutputStream(dest); Sys.copyStream(fin, fout); + + Sys.close(fin); if (!source.delete()) { logger.warn("Failed to delete {} after brute force copy to {}.", source, dest); } @@ -139,11 +136,13 @@ public class FileUtil { * Reads the contents of the supplied input stream into a list of lines. * Closes the reader on successful or failed completion. */ - public static List+ *
* This is different, in that it returns ALL FILES, instead of ones that just match a specific extension. + * * @return the list of all files in the root+sub-dirs. */ - public static List+ *
* This is different, in that it returns ALL FILES, instead of ones that just match a specific extension. + * * @return the list of all files in the root+sub-dirs. */ - public static List+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format of the system. - *+ *
* A trailing slash will be retained. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows except * for the separator character. *@@ -1090,10 +1142,11 @@ public class FileUtil { ** (Note the file separator returned will be correct for Windows/Unix) * - * @param filename the filename to normalize, null returns null + * @param filename the filename to normalize, null returns null * @return the normalized filename, or null if invalid */ - public static String normalize(String filename) { + public static + String normalize(String filename) { return doNormalize(filename, SYSTEM_SEPARATOR, true); } @@ -1105,20 +1158,21 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Normalizes a path, removing double and single dot path steps. - *
+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format of the system. - *+ *
* A trailing slash will be retained. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows except * for the separator character. *@@ -1142,10 +1196,11 @@ public class FileUtil { ** (Note the file separator returned will be correct for Windows/Unix) * - * @param filename the filename to normalize, null returns null + * @param filename the filename to normalize, null returns null * @return the normalized filename, or null if invalid */ - public static String normalizeAsFile(String filename) { + public static + String normalizeAsFile(String filename) { return doNormalize(new File(filename).getAbsolutePath(), SYSTEM_SEPARATOR, true); } @@ -1157,20 +1212,21 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Normalizes a path, removing double and single dot path steps. - *
+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format of the system. - *+ *
* A trailing slash will be retained. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows except * for the separator character. *@@ -1194,10 +1250,11 @@ public class FileUtil { ** (Note the file separator returned will be correct for Windows/Unix) * - * @param file the file to normalize, null returns null + * @param file the file to normalize, null returns null * @return the normalized file, or null if invalid */ - public static File normalize(File file) { + public static + File normalize(File file) { if (file == null) { return null; } @@ -1218,20 +1275,21 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Normalizes a path, removing double and single dot path steps. - *
+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format specified. - *+ *
* A trailing slash will be retained. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows except * for the separator character. *@@ -1256,13 +1314,14 @@ public class FileUtil { * The output will be the same on both Unix and Windows including * the separator character. * - * @param filename the filename to normalize, null returns null + * @param filename the filename to normalize, null returns null * @param unixSeparator {@code true} if a unix separator should - * be used or {@code false} if a windows separator should be used. + * be used or {@code false} if a windows separator should be used. * @return the normalized filename, or null if invalid * @since 2.0 */ - public static String normalize(String filename, boolean unixSeparator) { + public static + String normalize(String filename, boolean unixSeparator) { char separator = unixSeparator ? UNIX_SEPARATOR : WINDOWS_SEPARATOR; return doNormalize(filename, separator, true); } @@ -1276,21 +1335,22 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Normalizes a path, removing double and single dot path steps, * and removing any final directory separator. - *- *+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format of the system. - *+ *
* A trailing slash will be removed. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows except * for the separator character. *@@ -1314,10 +1374,11 @@ public class FileUtil { ** (Note the file separator returned will be correct for Windows/Unix) * - * @param filename the filename to normalize, null returns null + * @param filename the filename to normalize, null returns null * @return the normalized filename, or null if invalid */ - public static String normalizeNoEndSeparator(String filename) { + public static + String normalizeNoEndSeparator(String filename) { return doNormalize(filename, SYSTEM_SEPARATOR, false); } @@ -1329,21 +1390,22 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Normalizes a path, removing double and single dot path steps, * and removing any final directory separator. - *+ *
* This method normalizes a path to a standard format. * The input may contain separators in either Unix or Windows format. * The output will contain separators in the format specified. - *+ *
* A trailing slash will be removed. * A double slash will be merged to a single slash (but UNC names are handled). * A single dot path segment will be removed. * A double dot will cause that path segment and the one before to be removed. * If the double dot has no parent path segment to work with, {@code null} * is returned. - *+ *
* The output will be the same on both Unix and Windows including * the separator character. *@@ -1366,14 +1428,15 @@ public class FileUtil { * ~/../bar --> null ** - * @param filename the filename to normalize, null returns null + * @param filename the filename to normalize, null returns null * @param unixSeparator {@code true} if a unix separator should - * be used or {@code false} if a windows separtor should be used. + * be used or {@code false} if a windows separtor should be used. * @return the normalized filename, or null if invalid * @since 2.0 */ - public static String normalizeNoEndSeparator(String filename, boolean unixSeparator) { - char separator = unixSeparator ? UNIX_SEPARATOR : WINDOWS_SEPARATOR; + public static + String normalizeNoEndSeparator(String filename, boolean unixSeparator) { + char separator = unixSeparator ? UNIX_SEPARATOR : WINDOWS_SEPARATOR; return doNormalize(filename, separator, false); } @@ -1385,15 +1448,17 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Internal method to perform the normalization. * - * @param filename the filename - * @param separator The separator character to use - * @param keepSeparator true to keep the final separator + * @param filename the filename + * @param separator The separator character to use + * @param keepSeparator true to keep the final separator * @return the normalized filename */ - private static String doNormalize(String filename, char separator, boolean keepSeparator) { + private static + String doNormalize(String filename, char separator, boolean keepSeparator) { if (filename == null) { return null; } @@ -1437,12 +1502,12 @@ public class FileUtil { // dot slash for (int i = prefix + 1; i < size; i++) { if (array[i] == separator && array[i - 1] == '.' && - (i == prefix + 1 || array[i - 2] == separator)) { + (i == prefix + 1 || array[i - 2] == separator)) { if (i == size - 1) { lastIsDirectory = true; } System.arraycopy(array, i + 1, array, i - 1, size - i); - size -=2; + size -= 2; i--; } } @@ -1451,7 +1516,7 @@ public class FileUtil { outer: for (int i = prefix + 2; i < size; i++) { if (array[i] == separator && array[i - 1] == '.' && array[i - 2] == '.' && - (i == prefix + 2 || array[i - 3] == separator)) { + (i == prefix + 2 || array[i - 3] == separator)) { if (i == prefix + 2) { return null; } @@ -1459,7 +1524,7 @@ public class FileUtil { lastIsDirectory = true; } int j; - for (j = i - 4 ; j >= prefix; j--) { + for (j = i - 4; j >= prefix; j--) { if (array[j] == separator) { // remove b/../ from a/b/../c System.arraycopy(array, i + 1, array, j + 1, size - i); @@ -1496,11 +1561,12 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Returns the length of the filename prefix, such asC:/
or~/
. - *+ *
* This method will handle a file in either Unix or Windows format. - *+ *
* The prefix length includes the first slash in the full filename * if applicable. Thus, it is possible that the length returned is greater * than the length of the input string. @@ -1520,14 +1586,15 @@ public class FileUtil { * ~user/a/b/c.txt --> "~user/" --> named user * ~user --> "~user/" --> named user (slash added) *
+ *
* The output will be the same irrespective of the machine that the code is running on. * ie. both Unix and Windows prefixes are matched regardless. * - * @param filename the filename to find the prefix in, null returns -1 + * @param filename the filename to find the prefix in, null returns -1 * @return the length of the prefix, -1 if invalid or null */ - public static int getPrefixLength(String filename) { + public static + int getPrefixLength(String filename) { if (filename == null) { return -1; } @@ -1544,7 +1611,8 @@ public class FileUtil { return 2; // return a length greater than the input } return isSeparator(ch0) ? 1 : 0; - } else { + } + else { if (ch0 == '~') { int posUnix = filename.indexOf(UNIX_SEPARATOR, 1); int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1); @@ -1566,7 +1634,8 @@ public class FileUtil { } return -1; - } else if (isSeparator(ch0) && isSeparator(ch1)) { + } + else if (isSeparator(ch0) && isSeparator(ch1)) { int posUnix = filename.indexOf(UNIX_SEPARATOR, 2); int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2); if (posUnix == -1 && posWin == -1 || posUnix == 2 || posWin == 2) { @@ -1575,7 +1644,8 @@ public class FileUtil { posUnix = posUnix == -1 ? posWin : posUnix; posWin = posWin == -1 ? posUnix : posWin; return Math.min(posUnix, posWin) + 1; - } else { + } + else { return isSeparator(ch0) ? 1 : 0; } } @@ -1590,13 +1660,15 @@ public class FileUtil { * Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, * Jeremias Maerki, Stephen Colebourne */ + /** * Checks if the character is a separator. * - * @param ch the character to check + * @param ch the character to check * @return true if it is a separator character */ - private static boolean isSeparator(char ch) { + private static + boolean isSeparator(char ch) { return ch == UNIX_SEPARATOR || ch == WINDOWS_SEPARATOR; } @@ -1604,11 +1676,13 @@ public class FileUtil { /** * Gets the extension of a file */ - public static String getExtension(String fileName) { + public static + String getExtension(String fileName) { int dot = fileName.lastIndexOf('.'); if (dot > -1) { return fileName.substring(dot + 1); - } else { + } + else { return null; } }