1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| /**
* Check the accessibility of the given file by trying to create a copy of it.
*
* @param file The file to be checked.
* @return true if the file is accessible, false if not
*/
public static boolean isFileAccessible(File file) {
if (!file.canWrite()) {
return false;
}
File newFile = null;
int cpy = 1;
try {
newFile = new File(System.getProperty("java.io.tmpdir"), file.getName() + ".cpy");
while (newFile.exists()) {
newFile = new File(System.getProperty("java.io.tmpdir"), file.getName() + ".cpy." + cpy++);
}
newFile.deleteOnExit();
copy(file, newFile);
Thread.sleep(1000);
byte[] b1 = loadFileToBytes(file);
byte[] b2 = loadFileToBytes(newFile);
delete(newFile);
if (b1.length != b2.length) {
return false;
}
if (!md5sum(b1).equals(md5sum(b2))) {
return false;
}
} catch (IOException e) {
return false;
} catch (OverlappingFileLockException e) {
s_logger.warn(e.getMessage() + ": " + file + " => " + newFile);
} catch (InterruptedException e) {
s_logger.warn(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
s_logger.error(e.getMessage(), e);
} catch (Throwable t) {
s_logger.fatal(t.getMessage(), t);
}
return true;
}
/**
* Copy the file or directory <code>input</code> to the file or directory <code>output</code>.
*
* @param input File or Directory to be copied
* @param output Copy
* @throws IOException
*/
public static void copy(File input, File output) throws IOException {
copy(input, output, false);
}
/**
* Copy the file or directory <code>input</code> to the file or directory <code>output</code>.
*
* @param input File or Directory to be copied
* @param output Copy
* @param force Force the writting if outputFile already exists
* @throws IOException
*/
public static void copy(File input, File output, boolean force) throws IOException {
if (input.isDirectory() && output.isDirectory()) {
copyDir(input, output, force);
} else {
copyFile(input, output, force);
}
}
/**
* Copy the file <code>inputFile</code> to the file <code>outputFile</code>.
*
* @param inputFile File to be copied
* @param outputFile Copy
* @param force Force the writting if outputFile already exists
* @throws IOException
*/
public static void copyFile(File inputFile, File outputFile, boolean force) throws IOException {
if (!force && outputFile.exists()) {
throw new IOException("File " + outputFile + " already exists.");
} else if (outputFile.exists()) {
outputFile.delete();
}
FileChannel in = null;
FileChannel out = null;
try {
in = new FileInputStream(inputFile).getChannel();
out = new FileOutputStream(outputFile).getChannel();
in.transferTo(0, in.size(), out);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) { }
}
if (out != null) {
try {
out.close();
} catch (IOException e) { }
}
}
} |
Partager