| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.generic; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.AudioFile; |
| 22 | |
import org.jaudiotagger.audio.exceptions.CannotReadException; |
| 23 | |
import org.jaudiotagger.audio.exceptions.CannotWriteException; |
| 24 | |
import org.jaudiotagger.audio.exceptions.ModifyVetoException; |
| 25 | |
import org.jaudiotagger.audio.exceptions.UnableToRenameFileException; |
| 26 | |
import org.jaudiotagger.logging.ErrorMessage; |
| 27 | |
import org.jaudiotagger.tag.Tag; |
| 28 | |
|
| 29 | |
import java.io.File; |
| 30 | |
import java.io.IOException; |
| 31 | |
import java.io.RandomAccessFile; |
| 32 | |
import java.util.logging.Level; |
| 33 | |
import java.util.logging.Logger; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 336 | public abstract class AudioFileWriter |
| 47 | |
{ |
| 48 | |
private static final String TEMP_FILENAME_SUFFIX = ".tmp"; |
| 49 | |
private static final String WRITE_MODE = "rw"; |
| 50 | |
private static final int MINIMUM_FILESIZE = 150; |
| 51 | |
|
| 52 | |
|
| 53 | 42 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.generic"); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 336 | private AudioFileModificationListener modificationListener = null; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public synchronized void delete(AudioFile af) throws CannotReadException, CannotWriteException |
| 69 | |
{ |
| 70 | 17 | if (!af.getFile().canWrite()) |
| 71 | |
{ |
| 72 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath())); |
| 73 | |
} |
| 74 | |
|
| 75 | 17 | if (af.getFile().length() <= MINIMUM_FILESIZE) |
| 76 | |
{ |
| 77 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath())); |
| 78 | |
} |
| 79 | |
|
| 80 | 17 | RandomAccessFile raf = null; |
| 81 | 17 | RandomAccessFile rafTemp = null; |
| 82 | 17 | File tempF = null; |
| 83 | |
|
| 84 | |
|
| 85 | 17 | boolean revert = false; |
| 86 | |
|
| 87 | |
try |
| 88 | |
{ |
| 89 | |
|
| 90 | 17 | tempF = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile()); |
| 91 | 17 | rafTemp = new RandomAccessFile(tempF, WRITE_MODE); |
| 92 | 17 | raf = new RandomAccessFile(af.getFile(), WRITE_MODE); |
| 93 | 17 | raf.seek(0); |
| 94 | 17 | rafTemp.seek(0); |
| 95 | |
|
| 96 | |
try |
| 97 | |
{ |
| 98 | 17 | if (this.modificationListener != null) |
| 99 | |
{ |
| 100 | 17 | this.modificationListener.fileWillBeModified(af, true); |
| 101 | |
} |
| 102 | 17 | deleteTag(raf, rafTemp); |
| 103 | 17 | if (this.modificationListener != null) |
| 104 | |
{ |
| 105 | 17 | this.modificationListener.fileModified(af, tempF); |
| 106 | |
} |
| 107 | |
} |
| 108 | 0 | catch (ModifyVetoException veto) |
| 109 | |
{ |
| 110 | 0 | throw new CannotWriteException(veto); |
| 111 | 17 | } |
| 112 | |
|
| 113 | 17 | } |
| 114 | 0 | catch (Exception e) |
| 115 | |
{ |
| 116 | 0 | revert = true; |
| 117 | 0 | throw new CannotWriteException("\"" + af.getFile().getAbsolutePath() + "\" :" + e, e); |
| 118 | |
} |
| 119 | |
finally |
| 120 | |
{ |
| 121 | |
|
| 122 | 0 | File result = af.getFile(); |
| 123 | |
try |
| 124 | |
{ |
| 125 | 17 | if (raf != null) |
| 126 | |
{ |
| 127 | 17 | raf.close(); |
| 128 | |
} |
| 129 | 17 | if (rafTemp != null) |
| 130 | |
{ |
| 131 | 17 | rafTemp.close(); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | 17 | if (tempF.length() > 0 && !revert) |
| 136 | |
{ |
| 137 | 15 | boolean deleteResult = af.getFile().delete(); |
| 138 | 15 | if (deleteResult == false) |
| 139 | |
{ |
| 140 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath())); |
| 141 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath())); |
| 142 | |
} |
| 143 | 15 | boolean renameResult = tempF.renameTo(af.getFile()); |
| 144 | 15 | if (renameResult == false) |
| 145 | |
{ |
| 146 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath())); |
| 147 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath())); |
| 148 | |
} |
| 149 | 15 | result = tempF; |
| 150 | |
|
| 151 | |
|
| 152 | 15 | if(tempF.exists()) |
| 153 | |
{ |
| 154 | 0 | if(!tempF.delete()) |
| 155 | |
{ |
| 156 | |
|
| 157 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath())); |
| 158 | |
} |
| 159 | |
} |
| 160 | 15 | } |
| 161 | |
else |
| 162 | |
{ |
| 163 | |
|
| 164 | 2 | if(!tempF.delete()) |
| 165 | |
{ |
| 166 | |
|
| 167 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath())); |
| 168 | |
} |
| 169 | |
} |
| 170 | |
} |
| 171 | 0 | catch (Exception ex) |
| 172 | |
{ |
| 173 | 0 | logger.severe("AudioFileWriter exception cleaning up delete:" + af.getFile().getPath() + " or" + tempF.getAbsolutePath() + ":" + ex); |
| 174 | 17 | } |
| 175 | |
|
| 176 | 17 | if (this.modificationListener != null) |
| 177 | |
{ |
| 178 | 17 | this.modificationListener.fileOperationFinished(result); |
| 179 | |
} |
| 180 | 34 | } |
| 181 | 17 | } |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public synchronized void delete(RandomAccessFile raf, RandomAccessFile tempRaf) throws CannotReadException, CannotWriteException, IOException |
| 192 | |
{ |
| 193 | 0 | raf.seek(0); |
| 194 | 0 | tempRaf.seek(0); |
| 195 | 0 | deleteTag(raf, tempRaf); |
| 196 | 0 | } |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
protected abstract void deleteTag(RandomAccessFile raf, RandomAccessFile tempRaf) throws CannotReadException, CannotWriteException, IOException; |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public synchronized void setAudioFileModificationListener(AudioFileModificationListener listener) |
| 215 | |
{ |
| 216 | 336 | this.modificationListener = listener; |
| 217 | 336 | } |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
private void precheckWrite(AudioFile af) throws CannotWriteException |
| 232 | |
{ |
| 233 | |
|
| 234 | |
try |
| 235 | |
{ |
| 236 | 92 | if (af.getTag().isEmpty()) |
| 237 | |
{ |
| 238 | 1 | delete(af); |
| 239 | 1 | return; |
| 240 | |
} |
| 241 | |
} |
| 242 | 0 | catch (CannotReadException re) |
| 243 | |
{ |
| 244 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(af.getFile().getPath())); |
| 245 | 91 | } |
| 246 | |
|
| 247 | 91 | if (!af.getFile().canWrite()) |
| 248 | |
{ |
| 249 | 0 | logger.severe(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(af.getFile().getPath())); |
| 250 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(af.getFile().getPath())); |
| 251 | |
} |
| 252 | |
|
| 253 | 91 | if (af.getFile().length() <= MINIMUM_FILESIZE) |
| 254 | |
{ |
| 255 | 0 | logger.severe(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(af.getFile().getPath())); |
| 256 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(af.getFile().getPath())); |
| 257 | |
} |
| 258 | 91 | } |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public synchronized void write(AudioFile af) throws CannotWriteException |
| 270 | |
{ |
| 271 | 92 | logger.info("Started writing tag data for file:" + af.getFile().getName()); |
| 272 | |
|
| 273 | |
|
| 274 | 92 | precheckWrite(af); |
| 275 | |
|
| 276 | 92 | RandomAccessFile raf = null; |
| 277 | 92 | RandomAccessFile rafTemp = null; |
| 278 | 92 | File newFile = null; |
| 279 | 92 | File result = null; |
| 280 | |
|
| 281 | |
|
| 282 | |
try |
| 283 | |
{ |
| 284 | 92 | newFile = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile()); |
| 285 | |
} |
| 286 | |
|
| 287 | 0 | catch(IOException ioe) |
| 288 | |
{ |
| 289 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()), ioe); |
| 290 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), |
| 291 | |
af.getFile().getParentFile().getAbsolutePath())); |
| 292 | 92 | } |
| 293 | |
|
| 294 | |
|
| 295 | |
try |
| 296 | |
{ |
| 297 | 92 | rafTemp = new RandomAccessFile(newFile, WRITE_MODE); |
| 298 | 92 | raf = new RandomAccessFile(af.getFile(), WRITE_MODE); |
| 299 | |
|
| 300 | |
} |
| 301 | |
|
| 302 | 0 | catch(IOException ioe) |
| 303 | |
{ |
| 304 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath()), ioe); |
| 305 | |
|
| 306 | |
|
| 307 | |
try |
| 308 | |
{ |
| 309 | 0 | if (raf != null) |
| 310 | |
{ |
| 311 | 0 | raf.close(); |
| 312 | |
} |
| 313 | 0 | if (rafTemp != null) |
| 314 | |
{ |
| 315 | 0 | rafTemp.close(); |
| 316 | |
} |
| 317 | |
} |
| 318 | 0 | catch (IOException ioe2) |
| 319 | |
{ |
| 320 | |
|
| 321 | 0 | logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile(), ioe.getMessage()), ioe2); |
| 322 | 0 | } |
| 323 | |
|
| 324 | |
|
| 325 | 0 | if(!newFile.delete()) |
| 326 | |
{ |
| 327 | |
|
| 328 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath())); |
| 329 | |
} |
| 330 | |
|
| 331 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath())); |
| 332 | 92 | } |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
try |
| 337 | |
{ |
| 338 | |
|
| 339 | 92 | raf.seek(0); |
| 340 | 92 | rafTemp.seek(0); |
| 341 | |
try |
| 342 | |
{ |
| 343 | 92 | if (this.modificationListener != null) |
| 344 | |
{ |
| 345 | 92 | this.modificationListener.fileWillBeModified(af, false); |
| 346 | |
} |
| 347 | 92 | writeTag(af.getTag(), raf, rafTemp); |
| 348 | 92 | if (this.modificationListener != null) |
| 349 | |
{ |
| 350 | 92 | this.modificationListener.fileModified(af, newFile); |
| 351 | |
} |
| 352 | |
} |
| 353 | 0 | catch (ModifyVetoException veto) |
| 354 | |
{ |
| 355 | 0 | throw new CannotWriteException(veto); |
| 356 | 92 | } |
| 357 | 92 | } |
| 358 | 0 | catch (Exception e) |
| 359 | |
{ |
| 360 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage()), e); |
| 361 | |
|
| 362 | |
try |
| 363 | |
{ |
| 364 | 0 | if (raf != null) |
| 365 | |
{ |
| 366 | 0 | raf.close(); |
| 367 | |
} |
| 368 | 0 | if (rafTemp != null) |
| 369 | |
{ |
| 370 | 0 | rafTemp.close(); |
| 371 | |
} |
| 372 | |
} |
| 373 | 0 | catch (IOException ioe) |
| 374 | |
{ |
| 375 | |
|
| 376 | 0 | logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe); |
| 377 | 0 | } |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | 0 | if(!newFile.delete()) |
| 382 | |
{ |
| 383 | |
|
| 384 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath())); |
| 385 | |
} |
| 386 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage())); |
| 387 | |
} |
| 388 | |
finally |
| 389 | |
{ |
| 390 | 0 | try |
| 391 | |
{ |
| 392 | 92 | if (raf != null) |
| 393 | |
{ |
| 394 | 92 | raf.close(); |
| 395 | |
} |
| 396 | 92 | if (rafTemp != null) |
| 397 | |
{ |
| 398 | 92 | rafTemp.close(); |
| 399 | |
} |
| 400 | |
} |
| 401 | 0 | catch (IOException ioe) |
| 402 | |
{ |
| 403 | |
|
| 404 | 0 | logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe); |
| 405 | 184 | } |
| 406 | 92 | } |
| 407 | |
|
| 408 | |
|
| 409 | 92 | result = af.getFile(); |
| 410 | |
|
| 411 | |
|
| 412 | 92 | if (newFile.length() > 0) |
| 413 | |
{ |
| 414 | |
|
| 415 | |
|
| 416 | 87 | File originalFileBackup = new File(af.getFile().getParentFile().getPath(), AudioFile.getBaseFilename(af.getFile())+ ".old"); |
| 417 | 87 | boolean renameResult = af.getFile().renameTo(originalFileBackup); |
| 418 | 87 | if (renameResult == false) |
| 419 | |
{ |
| 420 | 0 | logger.log(Level.SEVERE,ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getPath(), originalFileBackup.getName())); |
| 421 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getPath(), originalFileBackup.getName())); |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | 87 | renameResult = newFile.renameTo(af.getFile()); |
| 426 | 87 | if (!renameResult) |
| 427 | |
{ |
| 428 | |
|
| 429 | |
|
| 430 | 0 | if(!newFile.exists()) |
| 431 | |
{ |
| 432 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_NEW_FILE_DOESNT_EXIST.getMsg(newFile.getAbsolutePath())); |
| 433 | |
} |
| 434 | |
|
| 435 | |
|
| 436 | 0 | if(!originalFileBackup.renameTo(af.getFile())) |
| 437 | |
{ |
| 438 | |
|
| 439 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_BACKUP_TO_ORIGINAL.getMsg(originalFileBackup.getAbsolutePath(),af.getFile().getName())); |
| 440 | |
} |
| 441 | |
|
| 442 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName())); |
| 443 | 0 | throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName())); |
| 444 | |
} |
| 445 | |
else |
| 446 | |
{ |
| 447 | |
|
| 448 | 87 | boolean deleteResult=originalFileBackup.delete(); |
| 449 | 87 | if(!deleteResult) |
| 450 | |
{ |
| 451 | |
|
| 452 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_WARNING_UNABLE_TO_DELETE_BACKUP_FILE.getMsg(originalFileBackup.getAbsolutePath())); |
| 453 | |
} |
| 454 | |
} |
| 455 | |
|
| 456 | |
|
| 457 | 87 | if(newFile.exists()) |
| 458 | |
{ |
| 459 | 0 | if(!newFile.delete()) |
| 460 | |
{ |
| 461 | |
|
| 462 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getPath())); |
| 463 | |
} |
| 464 | |
} |
| 465 | 87 | } |
| 466 | |
else |
| 467 | |
{ |
| 468 | |
|
| 469 | 5 | if(!newFile.delete()) |
| 470 | |
{ |
| 471 | |
|
| 472 | 0 | logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getPath())); |
| 473 | |
} |
| 474 | |
} |
| 475 | |
|
| 476 | 92 | if (this.modificationListener != null) |
| 477 | |
{ |
| 478 | 92 | this.modificationListener.fileOperationFinished(result); |
| 479 | |
} |
| 480 | 92 | } |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
protected abstract void writeTag(Tag tag, RandomAccessFile raf, RandomAccessFile rafTemp) throws CannotReadException, CannotWriteException, IOException; |
| 504 | |
|
| 505 | |
} |