一、前言
关于apache的commons-io源码包org.apache.commons.io.FileUtils文件工具类,通过File路径/名称字符串获取文件getFile、获取用户目录getUserDirectoryPath、获取临时路径getTempDirectoryPath、通过文件File打开openOutputStream文件输出流openOutputStream、通过源文件和目标文件进行目录/文件拷贝copyFileToDirectory/copyFile、通过URL源/输入字符流文件拷贝到文件copyURLToFile/copyInputStreamToFile、读取文件为字符串/二进制数组readFileToString/readFileToByteArray等。
二、源码说明
package org.apache.commons.io;@b@@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileFilter;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.Reader;@b@import java.math.BigInteger;@b@import java.net.URI;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import java.nio.channels.FileChannel;@b@import java.nio.charset.Charset;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Date;@b@import java.util.Iterator;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.zip.CRC32;@b@import java.util.zip.CheckedInputStream;@b@import java.util.zip.Checksum;@b@import org.apache.commons.io.filefilter.DirectoryFileFilter;@b@import org.apache.commons.io.filefilter.FalseFileFilter;@b@import org.apache.commons.io.filefilter.FileFilterUtils;@b@import org.apache.commons.io.filefilter.IOFileFilter;@b@import org.apache.commons.io.filefilter.SuffixFileFilter;@b@import org.apache.commons.io.filefilter.TrueFileFilter;@b@import org.apache.commons.io.output.NullOutputStream;@b@@b@public class FileUtils@b@{@b@ public static final long ONE_KB = 1024L;@b@ public static final BigInteger ONE_KB_BI = BigInteger.valueOf(1024L);@b@ public static final long ONE_MB = 1048576L;@b@ public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);@b@ private static final long FILE_COPY_BUFFER_SIZE = 31457280L;@b@ public static final long ONE_GB = 1073741824L;@b@ public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);@b@ public static final long ONE_TB = 1099511627776L;@b@ public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);@b@ public static final long ONE_PB = 1125899906842624L;@b@ public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);@b@ public static final long ONE_EB = 1152921504606846976L;@b@ public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);@b@ public static final BigInteger ONE_ZB = BigInteger.valueOf(1024L).multiply(BigInteger.valueOf(1152921504606846976L));@b@ public static final BigInteger ONE_YB = ONE_KB_BI.multiply(ONE_ZB);@b@ public static final File[] EMPTY_FILE_ARRAY = new File[0];@b@ private static final Charset UTF8 = Charset.forName("UTF-8");@b@@b@ public static File getFile(File directory, String[] names)@b@ {@b@ if (directory == null)@b@ throw new NullPointerException("directorydirectory must not be null");@b@@b@ if (names == null)@b@ throw new NullPointerException("names must not be null");@b@@b@ File file = directory;@b@ String[] arr$ = names; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String name = arr$[i$];@b@ file = new File(file, name);@b@ }@b@ return file;@b@ }@b@@b@ public static File getFile(String[] names)@b@ {@b@ if (names == null)@b@ throw new NullPointerException("names must not be null");@b@@b@ File file = null;@b@ String[] arr$ = names; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String name = arr$[i$];@b@ if (file == null)@b@ file = new File(name);@b@ else@b@ file = new File(file, name);@b@ }@b@@b@ return file;@b@ }@b@@b@ public static String getTempDirectoryPath()@b@ {@b@ return System.getProperty("java.io.tmpdir");@b@ }@b@@b@ public static File getTempDirectory()@b@ {@b@ return new File(getTempDirectoryPath());@b@ }@b@@b@ public static String getUserDirectoryPath()@b@ {@b@ return System.getProperty("user.home");@b@ }@b@@b@ public static File getUserDirectory()@b@ {@b@ return new File(getUserDirectoryPath());@b@ }@b@@b@ public static FileInputStream openInputStream(File file)@b@ throws IOException@b@ {@b@ if (file.exists()) {@b@ if (file.isDirectory())@b@ throw new IOException("File '" + file + "' exists but is a directory");@b@@b@ if (file.canRead()) break label117;@b@ throw new IOException("File '" + file + "' cannot be read");@b@ }@b@@b@ throw new FileNotFoundException("File '" + file + "' does not exist");@b@@b@ label117: return new FileInputStream(file);@b@ }@b@@b@ public static FileOutputStream openOutputStream(File file)@b@ throws IOException@b@ {@b@ return openOutputStream(file, false);@b@ }@b@@b@ public static FileOutputStream openOutputStream(File file, boolean append)@b@ throws IOException@b@ {@b@ if (file.exists()) {@b@ if (file.isDirectory())@b@ throw new IOException("File '" + file + "' exists but is a directory");@b@@b@ if (file.canWrite()) break label140;@b@ throw new IOException("File '" + file + "' cannot be written to");@b@ }@b@@b@ File parent = file.getParentFile();@b@ if ((parent != null) && @b@ (!(parent.mkdirs())) && (!(parent.isDirectory()))) {@b@ throw new IOException("Directory '" + parent + "' could not be created");@b@ }@b@@b@ label140: return new FileOutputStream(file, append);@b@ }@b@@b@ public static String byteCountToDisplaySize(BigInteger size)@b@ {@b@ String displaySize;@b@ if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";@b@ else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";@b@ else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";@b@ else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";@b@ else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";@b@ else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0)@b@ displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";@b@ else@b@ displaySize = String.valueOf(size) + " bytes";@b@@b@ return displaySize;@b@ }@b@@b@ public static String byteCountToDisplaySize(long size)@b@ {@b@ return byteCountToDisplaySize(BigInteger.valueOf(size));@b@ }@b@@b@ public static void touch(File file)@b@ throws IOException@b@ {@b@ if (!(file.exists())) {@b@ OutputStream out = openOutputStream(file);@b@ IOUtils.closeQuietly(out);@b@ }@b@ boolean success = file.setLastModified(System.currentTimeMillis());@b@ if (!(success))@b@ throw new IOException("Unable to set the last modification time for " + file);@b@ }@b@@b@ public static File[] convertFileCollectionToFileArray(Collection<File> files)@b@ {@b@ return ((File[])files.toArray(new File[files.size()]));@b@ }@b@@b@ private static void innerListFiles(Collection<File> files, File directory, IOFileFilter filter, boolean includeSubDirectories)@b@ {@b@ File[] arr$;@b@ int i$;@b@ File[] found = directory.listFiles(filter);@b@@b@ if (found != null) {@b@ arr$ = found; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ if (file.isDirectory()) {@b@ if (includeSubDirectories)@b@ files.add(file);@b@@b@ innerListFiles(files, file, filter, includeSubDirectories);@b@ } else {@b@ files.add(file);@b@ }@b@ }@b@ }@b@ }@b@@b@ public static Collection<File> listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)@b@ {@b@ validateListFilesParameters(directory, fileFilter);@b@@b@ IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);@b@ IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);@b@@b@ Collection files = new LinkedList();@b@ innerListFiles(files, directory, FileFilterUtils.or(new IOFileFilter[] { effFileFilter, effDirFilter }), false);@b@@b@ return files;@b@ }@b@@b@ private static void validateListFilesParameters(File directory, IOFileFilter fileFilter)@b@ {@b@ if (!(directory.isDirectory()))@b@ throw new IllegalArgumentException("Parameter 'directory' is not a directory");@b@@b@ if (fileFilter == null)@b@ throw new NullPointerException("Parameter 'fileFilter' is null");@b@ }@b@@b@ private static IOFileFilter setUpEffectiveFileFilter(IOFileFilter fileFilter)@b@ {@b@ return FileFilterUtils.and(new IOFileFilter[] { fileFilter, FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE) });@b@ }@b@@b@ private static IOFileFilter setUpEffectiveDirFilter(IOFileFilter dirFilter)@b@ {@b@ return ((dirFilter == null) ? FalseFileFilter.INSTANCE : FileFilterUtils.and(new IOFileFilter[] { dirFilter, DirectoryFileFilter.INSTANCE }));@b@ }@b@@b@ public static Collection<File> listFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)@b@ {@b@ validateListFilesParameters(directory, fileFilter);@b@@b@ IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);@b@ IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);@b@@b@ Collection files = new LinkedList();@b@ if (directory.isDirectory())@b@ files.add(directory);@b@@b@ innerListFiles(files, directory, FileFilterUtils.or(new IOFileFilter[] { effFileFilter, effDirFilter }), true);@b@@b@ return files;@b@ }@b@@b@ public static Iterator<File> iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)@b@ {@b@ return listFiles(directory, fileFilter, dirFilter).iterator();@b@ }@b@@b@ public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)@b@ {@b@ return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();@b@ }@b@@b@ private static String[] toSuffixes(String[] extensions)@b@ {@b@ String[] suffixes = new String[extensions.length];@b@ for (int i = 0; i < extensions.length; ++i)@b@ suffixes[i] = "." + extensions[i];@b@@b@ return suffixes;@b@ }@b@@b@ public static Collection<File> listFiles(File directory, String[] extensions, boolean recursive)@b@ {@b@ IOFileFilter filter;@b@ if (extensions == null) {@b@ filter = TrueFileFilter.INSTANCE;@b@ } else {@b@ String[] suffixes = toSuffixes(extensions);@b@ filter = new SuffixFileFilter(suffixes);@b@ }@b@ return listFiles(directory, filter, (recursive) ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE);@b@ }@b@@b@ public static Iterator<File> iterateFiles(File directory, String[] extensions, boolean recursive)@b@ {@b@ return listFiles(directory, extensions, recursive).iterator();@b@ }@b@@b@ public static boolean contentEquals(File file1, File file2)@b@ throws IOException@b@ {@b@ boolean file1Exists = file1.exists();@b@ if (file1Exists != file2.exists()) {@b@ return false;@b@ }@b@@b@ if (!(file1Exists))@b@ {@b@ return true;@b@ }@b@@b@ if ((file1.isDirectory()) || (file2.isDirectory()))@b@ {@b@ throw new IOException("Can't compare directories, only files");@b@ }@b@@b@ if (file1.length() != file2.length())@b@ {@b@ return false;@b@ }@b@@b@ if (file1.getCanonicalFile().equals(file2.getCanonicalFile()))@b@ {@b@ return true;@b@ }@b@@b@ InputStream input1 = null;@b@ InputStream input2 = null;@b@ try {@b@ input1 = new FileInputStream(file1);@b@ input2 = new FileInputStream(file2);@b@ boolean bool1 = IOUtils.contentEquals(input1, input2);@b@@b@ return bool1;@b@ }@b@ finally@b@ {@b@ IOUtils.closeQuietly(input1);@b@ IOUtils.closeQuietly(input2);@b@ }@b@ }@b@@b@ public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName)@b@ throws IOException@b@ {@b@ boolean file1Exists = file1.exists();@b@ if (file1Exists != file2.exists()) {@b@ return false;@b@ }@b@@b@ if (!(file1Exists))@b@ {@b@ return true;@b@ }@b@@b@ if ((file1.isDirectory()) || (file2.isDirectory()))@b@ {@b@ throw new IOException("Can't compare directories, only files");@b@ }@b@@b@ if (file1.getCanonicalFile().equals(file2.getCanonicalFile()))@b@ {@b@ return true;@b@ }@b@@b@ Reader input1 = null;@b@ Reader input2 = null;@b@ try {@b@ if (charsetName == null) {@b@ input1 = new InputStreamReader(new FileInputStream(file1));@b@ input2 = new InputStreamReader(new FileInputStream(file2));@b@ } else {@b@ input1 = new InputStreamReader(new FileInputStream(file1), charsetName);@b@ input2 = new InputStreamReader(new FileInputStream(file2), charsetName);@b@ }@b@ boolean bool1 = IOUtils.contentEqualsIgnoreEOL(input1, input2);@b@@b@ return bool1;@b@ }@b@ finally@b@ {@b@ IOUtils.closeQuietly(input1);@b@ IOUtils.closeQuietly(input2);@b@ }@b@ }@b@@b@ public static File toFile(URL url)@b@ {@b@ if ((url == null) || (!("file".equalsIgnoreCase(url.getProtocol()))))@b@ return null;@b@@b@ String filename = url.getFile().replace('/', File.separatorChar);@b@ filename = decodeUrl(filename);@b@ return new File(filename); } @b@ // ERROR //@b@ static String decodeUrl(String url) { // Byte code:@b@ @b@ public static File[] toFiles(URL[] urls) { if ((urls == null) || (urls.length == 0))@b@ return EMPTY_FILE_ARRAY;@b@@b@ File[] files = new File[urls.length];@b@ for (int i = 0; i < urls.length; ++i) {@b@ URL url = urls[i];@b@ if (url != null) {@b@ if (!(url.getProtocol().equals("file"))) {@b@ throw new IllegalArgumentException("URL could not be converted to a File: " + url);@b@ }@b@@b@ files[i] = toFile(url);@b@ }@b@ }@b@ return files;@b@ }@b@@b@ public static URL[] toURLs(File[] files)@b@ throws IOException@b@ {@b@ URL[] urls = new URL[files.length];@b@@b@ for (int i = 0; i < urls.length; ++i) {@b@ urls[i] = files[i].toURI().toURL();@b@ }@b@@b@ return urls;@b@ }@b@@b@ public static void copyFileToDirectory(File srcFile, File destDir)@b@ throws IOException@b@ {@b@ copyFileToDirectory(srcFile, destDir, true);@b@ }@b@@b@ public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate)@b@ throws IOException@b@ {@b@ if (destDir == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if ((destDir.exists()) && (!(destDir.isDirectory())))@b@ throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");@b@@b@ File destFile = new File(destDir, srcFile.getName());@b@ copyFile(srcFile, destFile, preserveFileDate);@b@ }@b@@b@ public static void copyFile(File srcFile, File destFile)@b@ throws IOException@b@ {@b@ copyFile(srcFile, destFile, true);@b@ }@b@@b@ public static void copyFile(File srcFile, File destFile, boolean preserveFileDate)@b@ throws IOException@b@ {@b@ if (srcFile == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destFile == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if (!(srcFile.exists()))@b@ throw new FileNotFoundException("Source '" + srcFile + "' does not exist");@b@@b@ if (srcFile.isDirectory())@b@ throw new IOException("Source '" + srcFile + "' exists but is a directory");@b@@b@ if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath()))@b@ throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");@b@@b@ File parentFile = destFile.getParentFile();@b@ if ((parentFile != null) && @b@ (!(parentFile.mkdirs())) && (!(parentFile.isDirectory()))) {@b@ throw new IOException("Destination '" + parentFile + "' directory cannot be created");@b@ }@b@@b@ if ((destFile.exists()) && (!(destFile.canWrite())))@b@ throw new IOException("Destination '" + destFile + "' exists but is read-only");@b@@b@ doCopyFile(srcFile, destFile, preserveFileDate);@b@ }@b@@b@ public static long copyFile(File input, OutputStream output)@b@ throws IOException@b@ {@b@ FileInputStream fis = new FileInputStream(input);@b@ try {@b@ long l = IOUtils.copyLarge(fis, output);@b@@b@ return l; } finally { fis.close();@b@ }@b@ }@b@@b@ private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate)@b@ throws IOException@b@ {@b@ if ((destFile.exists()) && (destFile.isDirectory())) {@b@ throw new IOException("Destination '" + destFile + "' exists but is a directory");@b@ }@b@@b@ FileInputStream fis = null;@b@ FileOutputStream fos = null;@b@ FileChannel input = null;@b@ FileChannel output = null;@b@ try {@b@ fis = new FileInputStream(srcFile);@b@ fos = new FileOutputStream(destFile);@b@ input = fis.getChannel();@b@ output = fos.getChannel();@b@ long size = input.size();@b@ long pos = 0L;@b@ long count = 0L;@b@ while (pos < size) {@b@ count = (size - pos > 31457280L) ? 31457280L : size - pos;@b@ pos += output.transferFrom(input, pos, count);@b@ }@b@ } finally {@b@ IOUtils.closeQuietly(output);@b@ IOUtils.closeQuietly(fos);@b@ IOUtils.closeQuietly(input);@b@ IOUtils.closeQuietly(fis);@b@ }@b@@b@ if (srcFile.length() != destFile.length()) {@b@ throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");@b@ }@b@@b@ if (preserveFileDate)@b@ destFile.setLastModified(srcFile.lastModified());@b@ }@b@@b@ public static void copyDirectoryToDirectory(File srcDir, File destDir)@b@ throws IOException@b@ {@b@ if (srcDir == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if ((srcDir.exists()) && (!(srcDir.isDirectory())))@b@ throw new IllegalArgumentException("Source '" + destDir + "' is not a directory");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if ((destDir.exists()) && (!(destDir.isDirectory())))@b@ throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");@b@@b@ copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);@b@ }@b@@b@ public static void copyDirectory(File srcDir, File destDir)@b@ throws IOException@b@ {@b@ copyDirectory(srcDir, destDir, true);@b@ }@b@@b@ public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate)@b@ throws IOException@b@ {@b@ copyDirectory(srcDir, destDir, null, preserveFileDate);@b@ }@b@@b@ public static void copyDirectory(File srcDir, File destDir, FileFilter filter)@b@ throws IOException@b@ {@b@ copyDirectory(srcDir, destDir, filter, true);@b@ }@b@@b@ public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate)@b@ throws IOException@b@ {@b@ File[] arr$;@b@ int i$;@b@ if (srcDir == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if (!(srcDir.exists()))@b@ throw new FileNotFoundException("Source '" + srcDir + "' does not exist");@b@@b@ if (!(srcDir.isDirectory()))@b@ throw new IOException("Source '" + srcDir + "' exists but is not a directory");@b@@b@ if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {@b@ throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");@b@ }@b@@b@ List exclusionList = null;@b@ if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {@b@ File[] srcFiles = (filter == null) ? srcDir.listFiles() : srcDir.listFiles(filter);@b@ if ((srcFiles != null) && (srcFiles.length > 0)) {@b@ exclusionList = new ArrayList(srcFiles.length);@b@ arr$ = srcFiles; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { File srcFile = arr$[i$];@b@ File copiedFile = new File(destDir, srcFile.getName());@b@ exclusionList.add(copiedFile.getCanonicalPath());@b@ }@b@ }@b@ }@b@ doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);@b@ }@b@@b@ private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate, List<String> exclusionList)@b@ throws IOException@b@ {@b@ File[] srcFiles = (filter == null) ? srcDir.listFiles() : srcDir.listFiles(filter);@b@ if (srcFiles == null)@b@ throw new IOException("Failed to list contents of " + srcDir);@b@@b@ if (destDir.exists()) {@b@ if (destDir.isDirectory()) break label142;@b@ throw new IOException("Destination '" + destDir + "' exists but is not a directory");@b@ }@b@@b@ if ((!(destDir.mkdirs())) && (!(destDir.isDirectory()))) {@b@ throw new IOException("Destination '" + destDir + "' directory cannot be created");@b@ }@b@@b@ if (!(destDir.canWrite()))@b@ label142: throw new IOException("Destination '" + destDir + "' cannot be written to");@b@@b@ File[] arr$ = srcFiles; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File srcFile = arr$[i$];@b@ File dstFile = new File(destDir, srcFile.getName());@b@ if ((exclusionList == null) || (!(exclusionList.contains(srcFile.getCanonicalPath())))) {@b@ if (srcFile.isDirectory())@b@ doCopyDirectory(srcFile, dstFile, filter, preserveFileDate, exclusionList);@b@ else@b@ doCopyFile(srcFile, dstFile, preserveFileDate);@b@@b@ }@b@@b@ }@b@@b@ if (preserveFileDate)@b@ destDir.setLastModified(srcDir.lastModified());@b@ }@b@@b@ public static void copyURLToFile(URL source, File destination)@b@ throws IOException@b@ {@b@ InputStream input = source.openStream();@b@ copyInputStreamToFile(input, destination);@b@ }@b@@b@ public static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout)@b@ throws IOException@b@ {@b@ URLConnection connection = source.openConnection();@b@ connection.setConnectTimeout(connectionTimeout);@b@ connection.setReadTimeout(readTimeout);@b@ InputStream input = connection.getInputStream();@b@ copyInputStreamToFile(input, destination);@b@ }@b@@b@ public static void copyInputStreamToFile(InputStream source, File destination)@b@ throws IOException@b@ {@b@ FileOutputStream output;@b@ try@b@ {@b@ output = openOutputStream(destination);@b@ try {@b@ IOUtils.copy(source, output);@b@ output.close();@b@ } finally {@b@ IOUtils.closeQuietly(output);@b@ }@b@ } finally {@b@ IOUtils.closeQuietly(source);@b@ }@b@ }@b@@b@ public static void deleteDirectory(File directory)@b@ throws IOException@b@ {@b@ if (!(directory.exists())) {@b@ return;@b@ }@b@@b@ if (!(isSymlink(directory))) {@b@ cleanDirectory(directory);@b@ }@b@@b@ if (!(directory.delete())) {@b@ String message = "Unable to delete directory " + directory + ".";@b@@b@ throw new IOException(message);@b@ }@b@ }@b@@b@ public static boolean deleteQuietly(File file)@b@ {@b@ if (file == null)@b@ return false;@b@ try@b@ {@b@ if (file.isDirectory())@b@ cleanDirectory(file);@b@ }@b@ catch (Exception ignored)@b@ {@b@ }@b@ try {@b@ return file.delete(); } catch (Exception ignored) {@b@ }@b@ return false;@b@ }@b@@b@ public static boolean directoryContains(File directory, File child)@b@ throws IOException@b@ {@b@ if (directory == null) {@b@ throw new IllegalArgumentException("Directory must not be null");@b@ }@b@@b@ if (!(directory.isDirectory())) {@b@ throw new IllegalArgumentException("Not a directory: " + directory);@b@ }@b@@b@ if (child == null) {@b@ return false;@b@ }@b@@b@ if ((!(directory.exists())) || (!(child.exists()))) {@b@ return false;@b@ }@b@@b@ String canonicalParent = directory.getCanonicalPath();@b@ String canonicalChild = child.getCanonicalPath();@b@@b@ return FilenameUtils.directoryContains(canonicalParent, canonicalChild);@b@ }@b@@b@ public static void cleanDirectory(File directory)@b@ throws IOException@b@ {@b@ String message;@b@ if (!(directory.exists())) {@b@ message = directory + " does not exist";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ if (!(directory.isDirectory())) {@b@ message = directory + " is not a directory";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ File[] files = directory.listFiles();@b@ if (files == null) {@b@ throw new IOException("Failed to list contents of " + directory);@b@ }@b@@b@ IOException exception = null;@b@ File[] arr$ = files; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ try {@b@ forceDelete(file);@b@ } catch (IOException ioe) {@b@ exception = ioe;@b@ }@b@ }@b@@b@ if (null != exception)@b@ throw exception;@b@ }@b@@b@ public static boolean waitFor(File file, int seconds)@b@ {@b@ int timeout = 0;@b@ int tick = 0;@b@ if (!(file.exists())) {@b@ if (tick++ >= 10) {@b@ tick = 0;@b@ if (timeout++ > seconds)@b@ return false;@b@ }@b@ try@b@ {@b@ Thread.sleep(100L);@b@ } catch (InterruptedException ignore) {@b@ }@b@ catch (Exception ex) {@b@ break label51:@b@ }@b@ }@b@ label51: return true;@b@ }@b@@b@ public static String readFileToString(File file, Charset encoding)@b@ throws IOException@b@ {@b@ InputStream in = null;@b@ try {@b@ in = openInputStream(file);@b@ String str = IOUtils.toString(in, Charsets.toCharset(encoding));@b@@b@ return str; } finally { IOUtils.closeQuietly(in);@b@ }@b@ }@b@@b@ public static String readFileToString(File file, String encoding)@b@ throws IOException@b@ {@b@ return readFileToString(file, Charsets.toCharset(encoding));@b@ }@b@@b@ public static String readFileToString(File file)@b@ throws IOException@b@ {@b@ return readFileToString(file, Charset.defaultCharset());@b@ }@b@@b@ public static byte[] readFileToByteArray(File file)@b@ throws IOException@b@ {@b@ InputStream in = null;@b@ try {@b@ in = openInputStream(file);@b@ byte[] arrayOfByte = IOUtils.toByteArray(in, file.length());@b@@b@ return arrayOfByte; } finally { IOUtils.closeQuietly(in);@b@ }@b@ }@b@@b@ public static List<String> readLines(File file, Charset encoding)@b@ throws IOException@b@ {@b@ InputStream in = null;@b@ try {@b@ in = openInputStream(file);@b@ List localList = IOUtils.readLines(in, Charsets.toCharset(encoding));@b@@b@ return localList; } finally { IOUtils.closeQuietly(in);@b@ }@b@ }@b@@b@ public static List<String> readLines(File file, String encoding)@b@ throws IOException@b@ {@b@ return readLines(file, Charsets.toCharset(encoding));@b@ }@b@@b@ public static List<String> readLines(File file)@b@ throws IOException@b@ {@b@ return readLines(file, Charset.defaultCharset());@b@ }@b@@b@ public static LineIterator lineIterator(File file, String encoding)@b@ throws IOException@b@ {@b@ InputStream in = null;@b@ try {@b@ in = openInputStream(file);@b@ return IOUtils.lineIterator(in, encoding);@b@ } catch (IOException ex) {@b@ IOUtils.closeQuietly(in);@b@ throw ex;@b@ } catch (RuntimeException ex) {@b@ IOUtils.closeQuietly(in);@b@ throw ex;@b@ }@b@ }@b@@b@ public static LineIterator lineIterator(File file)@b@ throws IOException@b@ {@b@ return lineIterator(file, null);@b@ }@b@@b@ public static void writeStringToFile(File file, String data, Charset encoding)@b@ throws IOException@b@ {@b@ writeStringToFile(file, data, encoding, false);@b@ }@b@@b@ public static void writeStringToFile(File file, String data, String encoding)@b@ throws IOException@b@ {@b@ writeStringToFile(file, data, encoding, false);@b@ }@b@@b@ public static void writeStringToFile(File file, String data, Charset encoding, boolean append)@b@ throws IOException@b@ {@b@ OutputStream out = null;@b@ try {@b@ out = openOutputStream(file, append);@b@ IOUtils.write(data, out, encoding);@b@ out.close();@b@ } finally {@b@ IOUtils.closeQuietly(out);@b@ }@b@ }@b@@b@ public static void writeStringToFile(File file, String data, String encoding, boolean append)@b@ throws IOException@b@ {@b@ writeStringToFile(file, data, Charsets.toCharset(encoding), append);@b@ }@b@@b@ public static void writeStringToFile(File file, String data)@b@ throws IOException@b@ {@b@ writeStringToFile(file, data, Charset.defaultCharset(), false);@b@ }@b@@b@ public static void writeStringToFile(File file, String data, boolean append)@b@ throws IOException@b@ {@b@ writeStringToFile(file, data, Charset.defaultCharset(), append);@b@ }@b@@b@ public static void write(File file, CharSequence data)@b@ throws IOException@b@ {@b@ write(file, data, Charset.defaultCharset(), false);@b@ }@b@@b@ public static void write(File file, CharSequence data, boolean append)@b@ throws IOException@b@ {@b@ write(file, data, Charset.defaultCharset(), append);@b@ }@b@@b@ public static void write(File file, CharSequence data, Charset encoding)@b@ throws IOException@b@ {@b@ write(file, data, encoding, false);@b@ }@b@@b@ public static void write(File file, CharSequence data, String encoding)@b@ throws IOException@b@ {@b@ write(file, data, encoding, false);@b@ }@b@@b@ public static void write(File file, CharSequence data, Charset encoding, boolean append)@b@ throws IOException@b@ {@b@ String str = (data == null) ? null : data.toString();@b@ writeStringToFile(file, str, encoding, append);@b@ }@b@@b@ public static void write(File file, CharSequence data, String encoding, boolean append)@b@ throws IOException@b@ {@b@ write(file, data, Charsets.toCharset(encoding), append);@b@ }@b@@b@ public static void writeByteArrayToFile(File file, byte[] data)@b@ throws IOException@b@ {@b@ writeByteArrayToFile(file, data, false);@b@ }@b@@b@ public static void writeByteArrayToFile(File file, byte[] data, boolean append)@b@ throws IOException@b@ {@b@ OutputStream out = null;@b@ try {@b@ out = openOutputStream(file, append);@b@ out.write(data);@b@ out.close();@b@ } finally {@b@ IOUtils.closeQuietly(out);@b@ }@b@ }@b@@b@ public static void writeLines(File file, String encoding, Collection<?> lines)@b@ throws IOException@b@ {@b@ writeLines(file, encoding, lines, null, false);@b@ }@b@@b@ public static void writeLines(File file, String encoding, Collection<?> lines, boolean append)@b@ throws IOException@b@ {@b@ writeLines(file, encoding, lines, null, append);@b@ }@b@@b@ public static void writeLines(File file, Collection<?> lines)@b@ throws IOException@b@ {@b@ writeLines(file, null, lines, null, false);@b@ }@b@@b@ public static void writeLines(File file, Collection<?> lines, boolean append)@b@ throws IOException@b@ {@b@ writeLines(file, null, lines, null, append);@b@ }@b@@b@ public static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding)@b@ throws IOException@b@ {@b@ writeLines(file, encoding, lines, lineEnding, false);@b@ }@b@@b@ public static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding, boolean append)@b@ throws IOException@b@ {@b@ FileOutputStream out = null;@b@ try {@b@ out = openOutputStream(file, append);@b@ BufferedOutputStream buffer = new BufferedOutputStream(out);@b@ IOUtils.writeLines(lines, lineEnding, buffer, encoding);@b@ buffer.flush();@b@ out.close();@b@ } finally {@b@ IOUtils.closeQuietly(out);@b@ }@b@ }@b@@b@ public static void writeLines(File file, Collection<?> lines, String lineEnding)@b@ throws IOException@b@ {@b@ writeLines(file, null, lines, lineEnding, false);@b@ }@b@@b@ public static void writeLines(File file, Collection<?> lines, String lineEnding, boolean append)@b@ throws IOException@b@ {@b@ writeLines(file, null, lines, lineEnding, append);@b@ }@b@@b@ public static void forceDelete(File file)@b@ throws IOException@b@ {@b@ if (file.isDirectory()) {@b@ deleteDirectory(file);@b@ } else {@b@ boolean filePresent = file.exists();@b@ if (!(file.delete())) {@b@ if (!(filePresent))@b@ throw new FileNotFoundException("File does not exist: " + file);@b@@b@ String message = "Unable to delete file: " + file;@b@@b@ throw new IOException(message);@b@ }@b@ }@b@ }@b@@b@ public static void forceDeleteOnExit(File file)@b@ throws IOException@b@ {@b@ if (file.isDirectory())@b@ deleteDirectoryOnExit(file);@b@ else@b@ file.deleteOnExit();@b@ }@b@@b@ private static void deleteDirectoryOnExit(File directory)@b@ throws IOException@b@ {@b@ if (!(directory.exists())) {@b@ return;@b@ }@b@@b@ directory.deleteOnExit();@b@ if (!(isSymlink(directory)))@b@ cleanDirectoryOnExit(directory);@b@ }@b@@b@ private static void cleanDirectoryOnExit(File directory)@b@ throws IOException@b@ {@b@ String message;@b@ if (!(directory.exists())) {@b@ message = directory + " does not exist";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ if (!(directory.isDirectory())) {@b@ message = directory + " is not a directory";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ File[] files = directory.listFiles();@b@ if (files == null) {@b@ throw new IOException("Failed to list contents of " + directory);@b@ }@b@@b@ IOException exception = null;@b@ File[] arr$ = files; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ try {@b@ forceDeleteOnExit(file);@b@ } catch (IOException ioe) {@b@ exception = ioe;@b@ }@b@ }@b@@b@ if (null != exception)@b@ throw exception;@b@ }@b@@b@ public static void forceMkdir(File directory)@b@ throws IOException@b@ {@b@ String message;@b@ if (directory.exists()) {@b@ if (directory.isDirectory()) return;@b@ message = "File " + directory + " exists and is " + "not a directory. Unable to create directory.";@b@@b@ throw new IOException(message);@b@ }@b@@b@ if ((!(directory.mkdirs())) && @b@ (!(directory.isDirectory())))@b@ {@b@ message = "Unable to create directory " + directory;@b@@b@ throw new IOException(message);@b@ }@b@ }@b@@b@ public static long sizeOf(File file)@b@ {@b@ if (!(file.exists())) {@b@ String message = file + " does not exist";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ if (file.isDirectory())@b@ return sizeOfDirectory(file);@b@@b@ return file.length();@b@ }@b@@b@ public static BigInteger sizeOfAsBigInteger(File file)@b@ {@b@ if (!(file.exists())) {@b@ String message = file + " does not exist";@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ if (file.isDirectory())@b@ return sizeOfDirectoryAsBigInteger(file);@b@@b@ return BigInteger.valueOf(file.length());@b@ }@b@@b@ public static long sizeOfDirectory(File directory)@b@ {@b@ checkDirectory(directory);@b@@b@ File[] files = directory.listFiles();@b@ if (files == null)@b@ return 0L;@b@@b@ long size = 0L;@b@@b@ File[] arr$ = files; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ try {@b@ if (!(isSymlink(file))) {@b@ size += sizeOf(file);@b@ if (size < 0L)@b@ break label78:@b@ }@b@ }@b@ catch (IOException ioe)@b@ {@b@ }@b@ }@b@@b@ label78: return size;@b@ }@b@@b@ public static BigInteger sizeOfDirectoryAsBigInteger(File directory)@b@ {@b@ checkDirectory(directory);@b@@b@ File[] files = directory.listFiles();@b@ if (files == null)@b@ return BigInteger.ZERO;@b@@b@ BigInteger size = BigInteger.ZERO;@b@@b@ File[] arr$ = files; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ try {@b@ if (!(isSymlink(file)))@b@ size = size.add(BigInteger.valueOf(sizeOf(file)));@b@ }@b@ catch (IOException ioe)@b@ {@b@ }@b@ }@b@@b@ return size;@b@ }@b@@b@ private static void checkDirectory(File directory)@b@ {@b@ if (!(directory.exists()))@b@ throw new IllegalArgumentException(directory + " does not exist");@b@@b@ if (!(directory.isDirectory()))@b@ throw new IllegalArgumentException(directory + " is not a directory");@b@ }@b@@b@ public static boolean isFileNewer(File file, File reference)@b@ {@b@ if (reference == null)@b@ throw new IllegalArgumentException("No specified reference file");@b@@b@ if (!(reference.exists())) {@b@ throw new IllegalArgumentException("The reference file '" + reference + "' doesn't exist");@b@ }@b@@b@ return isFileNewer(file, reference.lastModified());@b@ }@b@@b@ public static boolean isFileNewer(File file, Date date)@b@ {@b@ if (date == null)@b@ throw new IllegalArgumentException("No specified date");@b@@b@ return isFileNewer(file, date.getTime());@b@ }@b@@b@ public static boolean isFileNewer(File file, long timeMillis)@b@ {@b@ if (file == null)@b@ throw new IllegalArgumentException("No specified file");@b@@b@ if (!(file.exists()))@b@ return false;@b@@b@ return (file.lastModified() > timeMillis);@b@ }@b@@b@ public static boolean isFileOlder(File file, File reference)@b@ {@b@ if (reference == null)@b@ throw new IllegalArgumentException("No specified reference file");@b@@b@ if (!(reference.exists())) {@b@ throw new IllegalArgumentException("The reference file '" + reference + "' doesn't exist");@b@ }@b@@b@ return isFileOlder(file, reference.lastModified());@b@ }@b@@b@ public static boolean isFileOlder(File file, Date date)@b@ {@b@ if (date == null)@b@ throw new IllegalArgumentException("No specified date");@b@@b@ return isFileOlder(file, date.getTime());@b@ }@b@@b@ public static boolean isFileOlder(File file, long timeMillis)@b@ {@b@ if (file == null)@b@ throw new IllegalArgumentException("No specified file");@b@@b@ if (!(file.exists()))@b@ return false;@b@@b@ return (file.lastModified() < timeMillis);@b@ }@b@@b@ public static long checksumCRC32(File file)@b@ throws IOException@b@ {@b@ CRC32 crc = new CRC32();@b@ checksum(file, crc);@b@ return crc.getValue();@b@ }@b@@b@ public static Checksum checksum(File file, Checksum checksum)@b@ throws IOException@b@ {@b@ if (file.isDirectory())@b@ throw new IllegalArgumentException("Checksums can't be computed on directories");@b@@b@ InputStream in = null;@b@ try {@b@ in = new CheckedInputStream(new FileInputStream(file), checksum);@b@ IOUtils.copy(in, new NullOutputStream());@b@ } finally {@b@ IOUtils.closeQuietly(in);@b@ }@b@ return checksum;@b@ }@b@@b@ public static void moveDirectory(File srcDir, File destDir)@b@ throws IOException@b@ {@b@ if (srcDir == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if (!(srcDir.exists()))@b@ throw new FileNotFoundException("Source '" + srcDir + "' does not exist");@b@@b@ if (!(srcDir.isDirectory()))@b@ throw new IOException("Source '" + srcDir + "' is not a directory");@b@@b@ if (destDir.exists())@b@ throw new FileExistsException("Destination '" + destDir + "' already exists");@b@@b@ boolean rename = srcDir.renameTo(destDir);@b@ if (!(rename)) {@b@ if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath()))@b@ throw new IOException("Cannot move directory: " + srcDir + " to a subdirectory of itself: " + destDir);@b@@b@ copyDirectory(srcDir, destDir);@b@ deleteDirectory(srcDir);@b@ if (srcDir.exists())@b@ throw new IOException("Failed to delete original directory '" + srcDir + "' after copy to '" + destDir + "'");@b@ }@b@ }@b@@b@ public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir)@b@ throws IOException@b@ {@b@ if (src == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination directory must not be null");@b@@b@ if ((!(destDir.exists())) && (createDestDir))@b@ destDir.mkdirs();@b@@b@ if (!(destDir.exists())) {@b@ throw new FileNotFoundException("Destination directory '" + destDir + "' does not exist [createDestDir=" + createDestDir + "]");@b@ }@b@@b@ if (!(destDir.isDirectory()))@b@ throw new IOException("Destination '" + destDir + "' is not a directory");@b@@b@ moveDirectory(src, new File(destDir, src.getName()));@b@ }@b@@b@ public static void moveFile(File srcFile, File destFile)@b@ throws IOException@b@ {@b@ if (srcFile == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destFile == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if (!(srcFile.exists()))@b@ throw new FileNotFoundException("Source '" + srcFile + "' does not exist");@b@@b@ if (srcFile.isDirectory())@b@ throw new IOException("Source '" + srcFile + "' is a directory");@b@@b@ if (destFile.exists())@b@ throw new FileExistsException("Destination '" + destFile + "' already exists");@b@@b@ if (destFile.isDirectory())@b@ throw new IOException("Destination '" + destFile + "' is a directory");@b@@b@ boolean rename = srcFile.renameTo(destFile);@b@ if (!(rename)) {@b@ copyFile(srcFile, destFile);@b@ if (!(srcFile.delete())) {@b@ deleteQuietly(destFile);@b@ throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'");@b@ }@b@ }@b@ }@b@@b@ public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)@b@ throws IOException@b@ {@b@ if (srcFile == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination directory must not be null");@b@@b@ if ((!(destDir.exists())) && (createDestDir))@b@ destDir.mkdirs();@b@@b@ if (!(destDir.exists())) {@b@ throw new FileNotFoundException("Destination directory '" + destDir + "' does not exist [createDestDir=" + createDestDir + "]");@b@ }@b@@b@ if (!(destDir.isDirectory()))@b@ throw new IOException("Destination '" + destDir + "' is not a directory");@b@@b@ moveFile(srcFile, new File(destDir, srcFile.getName()));@b@ }@b@@b@ public static void moveToDirectory(File src, File destDir, boolean createDestDir)@b@ throws IOException@b@ {@b@ if (src == null)@b@ throw new NullPointerException("Source must not be null");@b@@b@ if (destDir == null)@b@ throw new NullPointerException("Destination must not be null");@b@@b@ if (!(src.exists()))@b@ throw new FileNotFoundException("Source '" + src + "' does not exist");@b@@b@ if (src.isDirectory())@b@ moveDirectoryToDirectory(src, destDir, createDestDir);@b@ else@b@ moveFileToDirectory(src, destDir, createDestDir);@b@ }@b@@b@ public static boolean isSymlink(File file)@b@ throws IOException@b@ {@b@ if (file == null)@b@ throw new NullPointerException("File must not be null");@b@@b@ if (FilenameUtils.isSystemWindows())@b@ return false;@b@@b@ File fileInCanonicalDir = null;@b@ if (file.getParent() == null) {@b@ fileInCanonicalDir = file;@b@ } else {@b@ File canonicalDir = file.getParentFile().getCanonicalFile();@b@ fileInCanonicalDir = new File(canonicalDir, file.getName());@b@ }@b@@b@ return (!(fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())));@b@ }@b@}