package bs.util.tool.commongui.utils;
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.OutputStream;
/**
* 复制文件,方法直接拷贝自commons-io-1.4的org.apache.commons.io.FileUtils.copyFile(srcFile, destFile).
*/
public class CopyFileFromCommonsIo {
public static void copyFile(File srcFile, File destFile) throws IOException {
copyFile(srcFile, destFile, true);
}
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destFile == null) {
throw new NullPointerException("Destination must not be null");
}
if (!srcFile.exists()) {
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
}
if ((destFile.getParentFile() != null) && (!destFile.getParentFile().exists())
&& (!destFile.getParentFile().mkdirs())) {
throw new IOException("Destination '" + destFile + "' directory cannot be created");
}
if ((destFile.exists()) && (!destFile.canWrite())) {
throw new IOException("Destination '" + destFile + "' exists but is read-only");
}
doCopyFile(srcFile, destFile, preserveFileDate);
}
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if ((destFile.exists()) && (destFile.isDirectory())) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}
FileInputStream input = new FileInputStream(srcFile);
try {
FileOutputStream output = new FileOutputStream(destFile);
try {
copy(input, output);
} finally {
closeQuietly(output);
}
} finally {
closeQuietly(input);
}
if (srcFile.length() != destFile.length()) {
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
}
if (preserveFileDate)
destFile.setLastModified(srcFile.lastModified());
}
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > 2147483647L) {
return -1;
}
return (int) count;
}
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void closeQuietly(InputStream input) {
try {
if (input != null)
input.close();
} catch (IOException ioe) {
}
}
public static void closeQuietly(OutputStream output) {
try {
if (output != null)
output.close();
} catch (IOException ioe) {
}
}
}