* @param af The file to process
* @throws CannotWriteException if anything went wrong
*/
public synchronized void delete(AudioFile af) throws CannotReadException, CannotWriteException {
if (!af.getFile().canWrite()) {
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath()));
}
if (af.getFile().length() <= MINIMUM_FILESIZE) {
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath()));
}
RandomAccessFile raf = null;
RandomAccessFile rafTemp = null;
File tempF = null;
//Will be set to true on VetoException, causing the finally block to discard the tempfile.
boolean revert = false;
try {
tempF = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
rafTemp = new RandomAccessFile(tempF, WRITE_MODE);
raf = new RandomAccessFile(af.getFile(), WRITE_MODE);
raf.seek(0);
rafTemp.seek(0);
try {
if (this.modificationListener != null) {
this.modificationListener.fileWillBeModified(af, true);
}
deleteTag(raf, rafTemp);
if (this.modificationListener != null) {
this.modificationListener.fileModified(af, tempF);
}
} catch (ModifyVetoException veto) {
throw new CannotWriteException(veto);
}
} catch (Exception e) {
revert = true;
throw new CannotWriteException("\"" + af.getFile().getAbsolutePath() + "\" :" + e, e);
} finally {
// will be set to the remaining file.
File result = af.getFile();
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
if (tempF.length() > 0 && !revert) {
boolean deleteResult = af.getFile().delete();
if (deleteResult == false) {
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
boolean renameResult = tempF.renameTo(af.getFile());
if (renameResult == false) {
//logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
result = tempF;
//If still exists we can now delete
if (tempF.exists()) {