[NIO] ClosedByInterruptException soudain et aléatoire
Salut,
Je me retrouve face à un problème qui me laisse perplexe.
J'ai un service Windows en java qui traite des fichiers, j'écoute un répertoire et quand un fichier y est créé, je teste son accessibilité puis je le traite s'il l'est.
Hors, depuis la mise à jour en SP2 de nos serveurs Windows Server 2003, j'ai une erreur :
Code:
1 2 3 4
| java.nio.channels.ClosedByInterruptException
at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:184)
at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:317)
at ch.idinfo.basis.common.util.FileUtils.copyFile(FileUtils.java:324) |
Voici mon code de test d'accessibilité du fichier (un peu maniclette mais j'ai rien trouvé de mieux) :
Code:
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) { }
}
}
} |
Ca plante sur le in.size() de
Code:
in.transferTo(0, in.size(), out);
de la méthode #copyFile(File, File, boolean).
Ce code de copie de fichier est celui de la FAQ. J'en avais un autre avant mais il plantait encore pire.
Si vous avez des idées, je suis preneur !
(s'il y a plus simple pour vérifier qu'un fichier est accessible, c'est-à-dire qu'il n'y a aucun programme qui n'y accède, je suis à fond preneur également).
Merci
PS: Je suis en Java 6 (b01)