newFile = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
}
//Unable to create temporary file, can happen in Vista if have Create Files/Write Data set to Deny
catch (IOException ioe) {
// //logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()), ioe);
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(),
af.getFile().getParentFile().getAbsolutePath()));
}
//Open temporary file and actual file for Editing
try {
rafTemp = new RandomAccessFile(newFile, WRITE_MODE);
raf = new RandomAccessFile(af.getFile(), WRITE_MODE);
}
//Unable to write to writable file, can happen in Vista if have Create Folders/Append Data set to Deny
catch (IOException ioe) {
// //logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath()), ioe);
//If we managed to open either file, delete it.
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe2) {
//Warn but assume has worked okay
// //logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile(), ioe.getMessage()), ioe2);
}
//Delete the temp file ( we cannot delet until closed correpsonding rafTemp)
if (!newFile.delete()) {
//Non critical failed deletion
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath()));
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath()));
}
//Write data to File
try {
raf.seek(0);
rafTemp.seek(0);
try {
if (this.modificationListener != null) {
this.modificationListener.fileWillBeModified(af, false);
}
writeTag(af.getTag(), raf, rafTemp);
if (this.modificationListener != null) {
this.modificationListener.fileModified(af, newFile);
}
} catch (ModifyVetoException veto) {
throw new CannotWriteException(veto);
}
} catch (Exception e) {
// //logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage()), e);
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe) {
//Warn but assume has worked okay
// //logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe);
}
//Delete the temporary file because either it was never used so lets just tidy up or we did start writing to it but
//the write failed and we havent renamed it back to the original file so we can just delete it.
if (!newFile.delete()) {
//Non critical failed deletion
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath()));
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage()));
} finally {
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe) {
//Warn but assume has worked okay
// //logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe);
}
}
//Result held in this file
result = af.getFile();
//If the temporary file was used
if (newFile.length() > 0) {
//Rename Original File
//Can fail on Vista if have Special Permission 'Delete' set Deny
File originalFileBackup = new File(af.getFile().getParentFile().getPath(), AudioFile.getBaseFilename(af.getFile()) + ".old");
boolean renameResult = af.getFile().renameTo(originalFileBackup);
if (renameResult == false) {
// //logger.log(Level.SEVERE,ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getPath(), originalFileBackup.getName()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getPath(), originalFileBackup.getName()));
}
//Rename Temp File to Original File
renameResult = newFile.renameTo(af.getFile());
if (!renameResult) {
//Renamed failed so lets do some checks rename the backup back to the original file
//New File doesnt exist
if (!newFile.exists()) {
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_NEW_FILE_DOESNT_EXIST.getMsg(newFile.getAbsolutePath()));
}
//Rename the backup back to the original
if (!originalFileBackup.renameTo(af.getFile())) {
//TODO now if this happens we are left with testfile.old instead of testfile.mp4
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_BACKUP_TO_ORIGINAL.getMsg(originalFileBackup.getAbsolutePath(), af.getFile().getName()));
}
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName()));
} else {
//Rename was okay so we can now delete the backup of the original
boolean deleteResult = originalFileBackup.delete();
if (!deleteResult) {
//Not a disaster but can't delete the backup so make a warning