| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package org.jaudiotagger.audio.mp3; |
| 23 | |
|
| 24 | |
import org.jaudiotagger.audio.AudioFile; |
| 25 | |
import org.jaudiotagger.audio.exceptions.CannotWriteException; |
| 26 | |
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; |
| 27 | |
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; |
| 28 | |
import org.jaudiotagger.logging.*; |
| 29 | |
import org.jaudiotagger.tag.Tag; |
| 30 | |
import org.jaudiotagger.tag.TagException; |
| 31 | |
import org.jaudiotagger.tag.TagNotFoundException; |
| 32 | |
import org.jaudiotagger.tag.TagOptionSingleton; |
| 33 | |
import org.jaudiotagger.tag.id3.*; |
| 34 | |
import org.jaudiotagger.tag.lyrics3.AbstractLyrics3; |
| 35 | |
|
| 36 | |
import java.io.*; |
| 37 | |
import java.nio.ByteBuffer; |
| 38 | |
import java.nio.channels.FileChannel; |
| 39 | |
import java.util.logging.Level; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class MP3File extends AudioFile |
| 45 | |
{ |
| 46 | |
private static final int MINIMUM_FILESIZE = 150; |
| 47 | |
|
| 48 | |
protected static AbstractTagDisplayFormatter tagFormatter; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 2061 | private AbstractID3v2Tag id3v2tag = null; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 2061 | private ID3v24Tag id3v2Asv24tag = null; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | 2061 | private AbstractLyrics3 lyrics3tag = null; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 2061 | private ID3v1Tag id3v1tag = null; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public MP3File() |
| 76 | 0 | { |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public MP3File(String filename) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException |
| 89 | |
{ |
| 90 | 0 | this(new File(filename)); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public static final int LOAD_IDV1TAG = 2; |
| 96 | |
|
| 97 | |
|
| 98 | |
public static final int LOAD_IDV2TAG = 4; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public static final int LOAD_LYRICS3 = 8; |
| 104 | |
|
| 105 | |
public static final int LOAD_ALL = LOAD_IDV1TAG | LOAD_IDV2TAG | LOAD_LYRICS3; |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public MP3File(File file, int loadOptions) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException |
| 119 | |
{ |
| 120 | 1416 | this(file, loadOptions, false); |
| 121 | 1400 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
private void readV1Tag(File file, RandomAccessFile newFile, int loadOptions) throws IOException |
| 132 | |
{ |
| 133 | 2045 | if ((loadOptions & LOAD_IDV1TAG) != 0) |
| 134 | |
{ |
| 135 | 2045 | logger.finer("Attempting to read id3v1tags"); |
| 136 | |
try |
| 137 | |
{ |
| 138 | 2045 | id3v1tag = new ID3v11Tag(newFile, file.getName()); |
| 139 | |
} |
| 140 | 2012 | catch (TagNotFoundException ex) |
| 141 | |
{ |
| 142 | 2012 | logger.info("No ids3v11 tag found"); |
| 143 | 33 | } |
| 144 | |
|
| 145 | |
try |
| 146 | |
{ |
| 147 | 2045 | if (id3v1tag == null) |
| 148 | |
{ |
| 149 | 2012 | id3v1tag = new ID3v1Tag(newFile, file.getName()); |
| 150 | |
} |
| 151 | |
} |
| 152 | 1730 | catch (TagNotFoundException ex) |
| 153 | |
{ |
| 154 | 1730 | logger.info("No id3v1 tag found"); |
| 155 | 315 | } |
| 156 | |
} |
| 157 | 2045 | } |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
private void readV2Tag(File file, int loadOptions) throws IOException, TagException |
| 170 | |
{ |
| 171 | |
|
| 172 | |
|
| 173 | 2045 | int startByte = (int) ((MP3AudioHeader) audioHeader).getMp3StartByte(); |
| 174 | 2045 | if (startByte >= AbstractID3v2Tag.TAG_HEADER_LENGTH) |
| 175 | |
{ |
| 176 | 1404 | logger.finer("Attempting to read id3v2tags"); |
| 177 | 1404 | FileInputStream fis = null; |
| 178 | 1404 | FileChannel fc = null; |
| 179 | 1404 | ByteBuffer bb = null; |
| 180 | |
try |
| 181 | |
{ |
| 182 | 1404 | fis = new FileInputStream(file); |
| 183 | 1404 | fc = fis.getChannel(); |
| 184 | |
|
| 185 | 1404 | bb = ByteBuffer.allocate(startByte); |
| 186 | 1404 | fc.read(bb); |
| 187 | 1404 | } |
| 188 | |
finally |
| 189 | |
{ |
| 190 | 0 | if (fc != null) |
| 191 | |
{ |
| 192 | 1404 | fc.close(); |
| 193 | |
} |
| 194 | |
|
| 195 | 1404 | if (fis != null) |
| 196 | |
{ |
| 197 | 1404 | fis.close(); |
| 198 | |
} |
| 199 | 1404 | } |
| 200 | |
|
| 201 | 1404 | bb.rewind(); |
| 202 | |
|
| 203 | 1404 | if ((loadOptions & LOAD_IDV2TAG) != 0) |
| 204 | |
{ |
| 205 | 1404 | logger.info("Attempting to read id3v2tags"); |
| 206 | |
try |
| 207 | |
{ |
| 208 | 1404 | this.setID3v2Tag(new ID3v24Tag(bb, file.getName())); |
| 209 | |
} |
| 210 | 819 | catch (TagNotFoundException ex) |
| 211 | |
{ |
| 212 | 819 | logger.info("No id3v24 tag found"); |
| 213 | 585 | } |
| 214 | |
|
| 215 | |
try |
| 216 | |
{ |
| 217 | 1404 | if (id3v2tag == null) |
| 218 | |
{ |
| 219 | 819 | this.setID3v2Tag(new ID3v23Tag(bb, file.getName())); |
| 220 | |
} |
| 221 | |
} |
| 222 | 278 | catch (TagNotFoundException ex) |
| 223 | |
{ |
| 224 | 278 | logger.info("No id3v23 tag found"); |
| 225 | 1126 | } |
| 226 | |
|
| 227 | |
try |
| 228 | |
{ |
| 229 | 1404 | if (id3v2tag == null) |
| 230 | |
{ |
| 231 | 278 | this.setID3v2Tag(new ID3v22Tag(bb, file.getName())); |
| 232 | |
} |
| 233 | |
} |
| 234 | 12 | catch (TagNotFoundException ex) |
| 235 | |
{ |
| 236 | 12 | logger.info("No id3v22 tag found"); |
| 237 | 1392 | } |
| 238 | |
} |
| 239 | 1404 | } |
| 240 | |
else |
| 241 | |
{ |
| 242 | 641 | logger.info("Not enough room for valid id3v2 tag:" + startByte); |
| 243 | |
} |
| 244 | 2045 | } |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
private void readLyrics3Tag(File file, RandomAccessFile newFile, int loadOptions) throws IOException |
| 257 | |
{ |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | 0 | } |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
private MP3AudioHeader checkAudioStart(long startByte, MP3AudioHeader currentHeader) throws IOException, InvalidAudioFrameException |
| 292 | |
{ |
| 293 | |
MP3AudioHeader newAudioHeader; |
| 294 | |
MP3AudioHeader nextAudioHeader; |
| 295 | |
|
| 296 | 47 | logger.warning(ErrorMessage.MP3_ID3TAG_LENGTH_INCORRECT.getMsg(file.getPath(), Hex.asHex(startByte), Hex.asHex(currentHeader.getMp3StartByte()))); |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | 47 | newAudioHeader = new MP3AudioHeader(file, 0); |
| 302 | 47 | logger.info("Checking from start:" + newAudioHeader); |
| 303 | |
|
| 304 | 47 | if (currentHeader.getMp3StartByte() == newAudioHeader.getMp3StartByte()) |
| 305 | |
{ |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | 46 | logger.info(ErrorMessage.MP3_START_OF_AUDIO_CONFIRMED.getMsg(file.getPath(), Hex.asHex(newAudioHeader.getMp3StartByte()))); |
| 310 | 46 | return currentHeader; |
| 311 | |
} |
| 312 | |
else |
| 313 | |
{ |
| 314 | |
|
| 315 | 1 | logger.info((ErrorMessage.MP3_RECALCULATED_POSSIBLE_START_OF_MP3_AUDIO.getMsg(file.getPath(), Hex.asHex(newAudioHeader.getMp3StartByte())))); |
| 316 | |
|
| 317 | |
|
| 318 | 1 | if (currentHeader.getNumberOfFrames() != newAudioHeader.getNumberOfFrames()) |
| 319 | |
{ |
| 320 | |
|
| 321 | 1 | nextAudioHeader = new MP3AudioHeader(file, newAudioHeader.getMp3StartByte() + newAudioHeader.mp3FrameHeader.getFrameLength()); |
| 322 | 1 | logger.info("Checking next:" + nextAudioHeader); |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | 1 | if (nextAudioHeader.getMp3StartByte() == currentHeader.getMp3StartByte()) |
| 327 | |
{ |
| 328 | 1 | logger.warning((ErrorMessage.MP3_START_OF_AUDIO_CONFIRMED.getMsg(file.getPath(), Hex.asHex(currentHeader.getMp3StartByte())))); |
| 329 | 1 | return currentHeader; |
| 330 | |
} |
| 331 | |
|
| 332 | 0 | else if (nextAudioHeader.getNumberOfFrames() == newAudioHeader.getNumberOfFrames()) |
| 333 | |
{ |
| 334 | 0 | logger.warning((ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(file.getPath(), Hex.asHex(newAudioHeader.getMp3StartByte())))); |
| 335 | 0 | return newAudioHeader; |
| 336 | |
} |
| 337 | |
|
| 338 | |
else |
| 339 | |
{ |
| 340 | 0 | logger.warning((ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(file.getPath(), Hex.asHex(newAudioHeader.getMp3StartByte())))); |
| 341 | 0 | return newAudioHeader; |
| 342 | |
} |
| 343 | |
} |
| 344 | |
|
| 345 | |
else |
| 346 | |
{ |
| 347 | 0 | logger.warning((ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(file.getPath(), Hex.asHex(newAudioHeader.getMp3StartByte())))); |
| 348 | 0 | return newAudioHeader; |
| 349 | |
} |
| 350 | |
} |
| 351 | |
} |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
public MP3File(File file, int loadOptions, boolean readOnly) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException |
| 366 | 2061 | { |
| 367 | 2061 | RandomAccessFile newFile = null; |
| 368 | |
try |
| 369 | |
{ |
| 370 | 2061 | this.file = file; |
| 371 | |
|
| 372 | |
|
| 373 | 2061 | newFile = checkFilePermissions(file, readOnly); |
| 374 | |
|
| 375 | |
|
| 376 | 2057 | long startByte = AbstractID3v2Tag.getV2TagSizeIfExists(file); |
| 377 | |
|
| 378 | |
|
| 379 | 2057 | audioHeader = new MP3AudioHeader(file, startByte); |
| 380 | |
|
| 381 | 2045 | if (startByte != ((MP3AudioHeader) audioHeader).getMp3StartByte()) |
| 382 | |
{ |
| 383 | 47 | logger.info("First header found after tag:" + audioHeader); |
| 384 | 47 | audioHeader = checkAudioStart(startByte, (MP3AudioHeader) audioHeader); |
| 385 | |
} |
| 386 | |
|
| 387 | |
|
| 388 | 2045 | readV1Tag(file, newFile, loadOptions); |
| 389 | |
|
| 390 | |
|
| 391 | 2045 | readV2Tag(file, loadOptions); |
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | 2045 | if (this.getID3v2Tag() != null) |
| 398 | |
{ |
| 399 | 1392 | tag = this.getID3v2Tag(); |
| 400 | |
} |
| 401 | 653 | else if (id3v1tag != null) |
| 402 | |
{ |
| 403 | 24 | tag = id3v1tag; |
| 404 | |
} |
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | 2045 | } |
| 409 | |
finally |
| 410 | |
{ |
| 411 | 16 | if (newFile != null) |
| 412 | |
{ |
| 413 | 2057 | newFile.close(); |
| 414 | |
} |
| 415 | 2045 | } |
| 416 | 2045 | } |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
public long getMP3StartByte(File file) throws InvalidAudioFrameException, IOException |
| 427 | |
{ |
| 428 | |
try |
| 429 | |
{ |
| 430 | |
|
| 431 | 0 | long startByte = AbstractID3v2Tag.getV2TagSizeIfExists(file); |
| 432 | |
|
| 433 | 0 | MP3AudioHeader audioHeader = new MP3AudioHeader(file, startByte); |
| 434 | 0 | if (startByte != audioHeader.getMp3StartByte()) |
| 435 | |
{ |
| 436 | 0 | logger.info("First header found after tag:" + audioHeader); |
| 437 | 0 | audioHeader = checkAudioStart(startByte, audioHeader); |
| 438 | |
} |
| 439 | 0 | return audioHeader.getMp3StartByte(); |
| 440 | |
} |
| 441 | 0 | catch (InvalidAudioFrameException iafe) |
| 442 | |
{ |
| 443 | 0 | throw iafe; |
| 444 | |
} |
| 445 | 0 | catch (IOException ioe) |
| 446 | |
{ |
| 447 | 0 | throw ioe; |
| 448 | |
} |
| 449 | |
} |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
public File extractID3v2TagDataIntoFile(File outputFile) throws TagNotFoundException, IOException |
| 464 | |
{ |
| 465 | 0 | int startByte = (int) ((MP3AudioHeader) audioHeader).getMp3StartByte(); |
| 466 | 0 | if (startByte >= 0) |
| 467 | |
{ |
| 468 | |
|
| 469 | |
|
| 470 | 0 | FileInputStream fis = new FileInputStream(file); |
| 471 | 0 | FileChannel fc = fis.getChannel(); |
| 472 | 0 | ByteBuffer bb = ByteBuffer.allocate(startByte); |
| 473 | 0 | fc.read(bb); |
| 474 | |
|
| 475 | |
|
| 476 | 0 | FileOutputStream out = new FileOutputStream(outputFile); |
| 477 | 0 | out.write(bb.array()); |
| 478 | 0 | out.close(); |
| 479 | 0 | fc.close(); |
| 480 | 0 | fis.close(); |
| 481 | 0 | return outputFile; |
| 482 | |
} |
| 483 | 0 | throw new TagNotFoundException("There is no ID3v2Tag data in this file"); |
| 484 | |
} |
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
public MP3AudioHeader getMP3AudioHeader() |
| 491 | |
{ |
| 492 | 112 | return (MP3AudioHeader) getAudioHeader(); |
| 493 | |
} |
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
public boolean hasID3v1Tag() |
| 501 | |
{ |
| 502 | 89 | return (id3v1tag != null); |
| 503 | |
} |
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
public boolean hasID3v2Tag() |
| 511 | |
{ |
| 512 | 113 | return (id3v2tag != null); |
| 513 | |
} |
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
public MP3File(File file) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException |
| 538 | |
{ |
| 539 | 1416 | this(file, LOAD_ALL); |
| 540 | 1400 | } |
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
public void setID3v1Tag(ID3v1Tag id3v1tag) |
| 548 | |
{ |
| 549 | 37 | logger.info("setting tagv1:v1 tag"); |
| 550 | 37 | this.id3v1tag = id3v1tag; |
| 551 | 37 | } |
| 552 | |
|
| 553 | |
public void setID3v1Tag(Tag id3v1tag) |
| 554 | |
{ |
| 555 | 0 | logger.info("setting tagv1:v1 tag"); |
| 556 | 0 | this.id3v1tag = (ID3v1Tag) id3v1tag; |
| 557 | 0 | } |
| 558 | |
|
| 559 | |
|
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
public void setID3v1Tag(AbstractTag mp3tag) |
| 568 | |
{ |
| 569 | 0 | logger.info("setting tagv1:abstract"); |
| 570 | 0 | id3v1tag = new ID3v11Tag(mp3tag); |
| 571 | 0 | } |
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
public ID3v1Tag getID3v1Tag() |
| 579 | |
{ |
| 580 | 74 | return id3v1tag; |
| 581 | |
} |
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
|
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
public void setID3v2Tag(AbstractTag mp3tag) |
| 592 | |
{ |
| 593 | 4 | id3v2tag = new ID3v24Tag(mp3tag); |
| 594 | |
|
| 595 | 4 | } |
| 596 | |
|
| 597 | |
|
| 598 | |
|
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
public void setID3v2Tag(AbstractID3v2Tag id3v2tag) |
| 605 | |
{ |
| 606 | 1898 | this.id3v2tag = id3v2tag; |
| 607 | 1898 | if (id3v2tag instanceof ID3v24Tag) |
| 608 | |
{ |
| 609 | 865 | this.id3v2Asv24tag = (ID3v24Tag) this.id3v2tag; |
| 610 | |
} |
| 611 | |
else |
| 612 | |
{ |
| 613 | 1033 | this.id3v2Asv24tag = new ID3v24Tag(id3v2tag); |
| 614 | |
} |
| 615 | 1898 | } |
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
public void setID3v2TagOnly(AbstractID3v2Tag id3v2tag) |
| 624 | |
{ |
| 625 | 248 | this.id3v2tag = id3v2tag; |
| 626 | 248 | this.id3v2Asv24tag = null; |
| 627 | 248 | } |
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
|
| 634 | |
public AbstractID3v2Tag getID3v2Tag() |
| 635 | |
{ |
| 636 | 4408 | return id3v2tag; |
| 637 | |
} |
| 638 | |
|
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
public ID3v24Tag getID3v2TagAsv24() |
| 643 | |
{ |
| 644 | 97 | return id3v2Asv24tag; |
| 645 | |
} |
| 646 | |
|
| 647 | |
|
| 648 | |
|
| 649 | |
|
| 650 | |
|
| 651 | |
|
| 652 | |
|
| 653 | |
|
| 654 | |
|
| 655 | |
|
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
|
| 664 | |
|
| 665 | |
|
| 666 | |
|
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | |
|
| 674 | |
|
| 675 | |
|
| 676 | |
|
| 677 | |
|
| 678 | |
|
| 679 | |
|
| 680 | |
|
| 681 | |
|
| 682 | |
|
| 683 | |
|
| 684 | |
|
| 685 | |
|
| 686 | |
|
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
public void delete(AbstractTag mp3tag) throws FileNotFoundException, IOException |
| 694 | |
{ |
| 695 | 0 | mp3tag.delete(new RandomAccessFile(this.file, "rws")); |
| 696 | 0 | } |
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
|
| 701 | |
|
| 702 | |
|
| 703 | |
|
| 704 | |
public void save() throws IOException, TagException |
| 705 | |
{ |
| 706 | 1239 | save(this.file); |
| 707 | 1239 | } |
| 708 | |
|
| 709 | |
|
| 710 | |
|
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
public void commit() throws CannotWriteException |
| 715 | |
{ |
| 716 | |
try |
| 717 | |
{ |
| 718 | 398 | save(); |
| 719 | |
} |
| 720 | 0 | catch (IOException ioe) |
| 721 | |
{ |
| 722 | 0 | throw new CannotWriteException(ioe); |
| 723 | |
} |
| 724 | 0 | catch (TagException te) |
| 725 | |
{ |
| 726 | 0 | throw new CannotWriteException(te); |
| 727 | 398 | } |
| 728 | 398 | } |
| 729 | |
|
| 730 | |
|
| 731 | |
|
| 732 | |
|
| 733 | |
|
| 734 | |
|
| 735 | |
|
| 736 | |
public void precheck(File file) throws IOException |
| 737 | |
{ |
| 738 | 1239 | if (!file.exists()) |
| 739 | |
{ |
| 740 | 0 | logger.severe(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_NOT_FOUND.getMsg(file.getName())); |
| 741 | 0 | throw new IOException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_NOT_FOUND.getMsg(file.getName())); |
| 742 | |
} |
| 743 | |
|
| 744 | 1239 | if (!file.canWrite()) |
| 745 | |
{ |
| 746 | 0 | logger.severe(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(file.getName())); |
| 747 | 0 | throw new IOException(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(file.getName())); |
| 748 | |
} |
| 749 | |
|
| 750 | 1239 | if (file.length() <= MINIMUM_FILESIZE) |
| 751 | |
{ |
| 752 | 0 | logger.severe(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(file.getName())); |
| 753 | 0 | throw new IOException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(file.getName())); |
| 754 | |
} |
| 755 | 1239 | } |
| 756 | |
|
| 757 | |
|
| 758 | |
|
| 759 | |
|
| 760 | |
|
| 761 | |
|
| 762 | |
|
| 763 | |
|
| 764 | |
|
| 765 | |
public void save(File file) throws IOException |
| 766 | |
{ |
| 767 | 1239 | logger.info("Saving : " + file.getAbsolutePath()); |
| 768 | |
|
| 769 | |
|
| 770 | 1239 | precheck(file); |
| 771 | |
|
| 772 | 1239 | RandomAccessFile rfile = null; |
| 773 | |
try |
| 774 | |
{ |
| 775 | |
|
| 776 | 1239 | if (TagOptionSingleton.getInstance().isId3v2Save()) |
| 777 | |
{ |
| 778 | 1239 | if (id3v2tag == null) |
| 779 | |
{ |
| 780 | 41 | rfile = new RandomAccessFile(file, "rws"); |
| 781 | 41 | (new ID3v24Tag()).delete(rfile); |
| 782 | 41 | (new ID3v23Tag()).delete(rfile); |
| 783 | 41 | (new ID3v22Tag()).delete(rfile); |
| 784 | 41 | logger.info("Deleting ID3v2 tag:"+file.getName()); |
| 785 | 41 | rfile.close(); |
| 786 | |
} |
| 787 | |
else |
| 788 | |
{ |
| 789 | 1198 | logger.info("Writing ID3v2 tag:"+file.getName()); |
| 790 | 1198 | id3v2tag.write(file, ((MP3AudioHeader) this.getAudioHeader()).getMp3StartByte()); |
| 791 | |
} |
| 792 | |
} |
| 793 | 1239 | rfile = new RandomAccessFile(file, "rws"); |
| 794 | |
|
| 795 | |
|
| 796 | 1239 | if (TagOptionSingleton.getInstance().isLyrics3Save()) |
| 797 | |
{ |
| 798 | 1239 | if (lyrics3tag != null) |
| 799 | |
{ |
| 800 | 0 | lyrics3tag.write(rfile); |
| 801 | |
} |
| 802 | |
} |
| 803 | |
|
| 804 | 1239 | if (TagOptionSingleton.getInstance().isId3v1Save()) |
| 805 | |
{ |
| 806 | 1239 | logger.info("Processing ID3v1"); |
| 807 | 1239 | if (id3v1tag == null) |
| 808 | |
{ |
| 809 | 963 | logger.info("Deleting ID3v1"); |
| 810 | 963 | (new ID3v1Tag()).delete(rfile); |
| 811 | |
} |
| 812 | |
else |
| 813 | |
{ |
| 814 | 276 | logger.info("Saving ID3v1"); |
| 815 | 276 | id3v1tag.write(rfile); |
| 816 | |
} |
| 817 | |
} |
| 818 | 1239 | } |
| 819 | 0 | catch (FileNotFoundException ex) |
| 820 | |
{ |
| 821 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_NOT_FOUND.getMsg(file.getName()), ex); |
| 822 | 0 | throw ex; |
| 823 | |
} |
| 824 | 0 | catch (IOException iex) |
| 825 | |
{ |
| 826 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(file.getName(), iex.getMessage()), iex); |
| 827 | 0 | throw iex; |
| 828 | |
} |
| 829 | 0 | catch (RuntimeException re) |
| 830 | |
{ |
| 831 | 0 | logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(file.getName(), re.getMessage()), re); |
| 832 | 0 | throw re; |
| 833 | |
} |
| 834 | |
finally |
| 835 | |
{ |
| 836 | 0 | if (rfile != null) |
| 837 | |
{ |
| 838 | 1239 | rfile.close(); |
| 839 | |
} |
| 840 | 1239 | } |
| 841 | 1239 | } |
| 842 | |
|
| 843 | |
|
| 844 | |
|
| 845 | |
|
| 846 | |
public String displayStructureAsXML() |
| 847 | |
{ |
| 848 | 32 | createXMLStructureFormatter(); |
| 849 | 32 | tagFormatter.openHeadingElement("file", this.getFile().getAbsolutePath()); |
| 850 | 32 | if (this.getID3v1Tag() != null) |
| 851 | |
{ |
| 852 | 0 | this.getID3v1Tag().createStructure(); |
| 853 | |
} |
| 854 | 32 | if (this.getID3v2Tag() != null) |
| 855 | |
{ |
| 856 | 32 | this.getID3v2Tag().createStructure(); |
| 857 | |
} |
| 858 | 32 | tagFormatter.closeHeadingElement("file"); |
| 859 | 32 | return tagFormatter.toString(); |
| 860 | |
} |
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
public String displayStructureAsPlainText() |
| 866 | |
{ |
| 867 | 0 | createPlainTextStructureFormatter(); |
| 868 | 0 | tagFormatter.openHeadingElement("file", this.getFile().getAbsolutePath()); |
| 869 | 0 | if (this.getID3v1Tag() != null) |
| 870 | |
{ |
| 871 | 0 | this.getID3v1Tag().createStructure(); |
| 872 | |
} |
| 873 | 0 | if (this.getID3v2Tag() != null) |
| 874 | |
{ |
| 875 | 0 | this.getID3v2Tag().createStructure(); |
| 876 | |
} |
| 877 | 0 | tagFormatter.closeHeadingElement("file"); |
| 878 | 0 | return tagFormatter.toString(); |
| 879 | |
} |
| 880 | |
|
| 881 | |
private static void createXMLStructureFormatter() |
| 882 | |
{ |
| 883 | 32 | tagFormatter = new XMLTagDisplayFormatter(); |
| 884 | 32 | } |
| 885 | |
|
| 886 | |
private static void createPlainTextStructureFormatter() |
| 887 | |
{ |
| 888 | 0 | tagFormatter = new PlainTextTagDisplayFormatter(); |
| 889 | 0 | } |
| 890 | |
|
| 891 | |
public static AbstractTagDisplayFormatter getStructureFormatter() |
| 892 | |
{ |
| 893 | 8428 | return tagFormatter; |
| 894 | |
} |
| 895 | |
|
| 896 | |
|
| 897 | |
|
| 898 | |
|
| 899 | |
|
| 900 | |
|
| 901 | |
|
| 902 | |
|
| 903 | |
public void setTag(Tag tag) |
| 904 | |
{ |
| 905 | 81 | this.tag = tag; |
| 906 | 81 | if (tag instanceof ID3v1Tag) |
| 907 | |
{ |
| 908 | 8 | setID3v1Tag((ID3v1Tag) tag); |
| 909 | |
} |
| 910 | |
else |
| 911 | |
{ |
| 912 | 73 | setID3v2Tag((AbstractID3v2Tag) tag); |
| 913 | |
} |
| 914 | 81 | } |
| 915 | |
|
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
@Override |
| 922 | |
|
| 923 | |
public Tag createDefaultTag() |
| 924 | |
{ |
| 925 | 25 | return new ID3v23Tag(); |
| 926 | |
} |
| 927 | |
} |
| 928 | |
|