| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.jaudiotagger.tag.id3; |
| 17 | |
|
| 18 | |
import org.jaudiotagger.FileConstants; |
| 19 | |
import org.jaudiotagger.audio.mp3.MP3File; |
| 20 | |
import org.jaudiotagger.logging.ErrorMessage; |
| 21 | |
import org.jaudiotagger.tag.*; |
| 22 | |
import org.jaudiotagger.tag.datatype.Artwork; |
| 23 | |
import org.jaudiotagger.tag.datatype.DataTypes; |
| 24 | |
import org.jaudiotagger.tag.id3.framebody.*; |
| 25 | |
import org.jaudiotagger.tag.id3.valuepair.ImageFormats; |
| 26 | |
import org.jaudiotagger.tag.reference.PictureTypes; |
| 27 | |
|
| 28 | |
import java.io.File; |
| 29 | |
import java.io.IOException; |
| 30 | |
import java.nio.ByteBuffer; |
| 31 | |
import java.nio.channels.WritableByteChannel; |
| 32 | |
import java.util.ArrayList; |
| 33 | |
import java.util.Comparator; |
| 34 | |
import java.util.LinkedHashMap; |
| 35 | |
import java.util.List; |
| 36 | |
import java.util.logging.Level; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 136 | public class ID3v22Tag extends AbstractID3v2Tag |
| 46 | |
{ |
| 47 | |
|
| 48 | |
protected static final String TYPE_COMPRESSION = "compression"; |
| 49 | |
protected static final String TYPE_UNSYNCHRONISATION = "unsyncronisation"; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static final int MASK_V22_UNSYNCHRONIZATION = FileConstants.BIT7; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static final int MASK_V22_COMPRESSION = FileConstants.BIT6; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 487 | protected boolean compression = false; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 487 | protected boolean unsynchronization = false; |
| 71 | |
|
| 72 | |
public static final byte RELEASE = 2; |
| 73 | |
public static final byte MAJOR_VERSION = 2; |
| 74 | |
public static final byte REVISION = 0; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public byte getRelease() |
| 80 | |
{ |
| 81 | 12 | return RELEASE; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public byte getMajorVersion() |
| 88 | |
{ |
| 89 | 534 | return MAJOR_VERSION; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public byte getRevision() |
| 96 | |
{ |
| 97 | 534 | return REVISION; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public ID3v22Tag() |
| 104 | 129 | { |
| 105 | 129 | frameMap = new LinkedHashMap(); |
| 106 | 129 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
protected void copyPrimitives(AbstractID3v2Tag copyObj) |
| 112 | |
{ |
| 113 | 80 | logger.info("Copying primitives"); |
| 114 | 80 | super.copyPrimitives(copyObj); |
| 115 | |
|
| 116 | |
|
| 117 | 80 | if (copyObj instanceof ID3v22Tag) |
| 118 | |
{ |
| 119 | 0 | ID3v22Tag copyObject = (ID3v22Tag) copyObj; |
| 120 | 0 | this.compression = copyObject.compression; |
| 121 | 0 | this.unsynchronization = copyObject.unsynchronization; |
| 122 | 0 | } |
| 123 | 80 | else if (copyObj instanceof ID3v23Tag) |
| 124 | |
{ |
| 125 | 0 | ID3v23Tag copyObject = (ID3v23Tag) copyObj; |
| 126 | 0 | this.compression = copyObject.compression; |
| 127 | 0 | this.unsynchronization = copyObject.unsynchronization; |
| 128 | 0 | } |
| 129 | 80 | else if (copyObj instanceof ID3v24Tag) |
| 130 | |
{ |
| 131 | 80 | ID3v24Tag copyObject = (ID3v24Tag) copyObj; |
| 132 | 80 | this.compression = false; |
| 133 | 80 | this.unsynchronization = copyObject.unsynchronization; |
| 134 | |
} |
| 135 | 80 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public ID3v22Tag(ID3v22Tag copyObject) |
| 144 | |
{ |
| 145 | |
|
| 146 | 0 | super(copyObject); |
| 147 | 0 | logger.info("Creating tag from another tag of same type"); |
| 148 | 0 | copyPrimitives(copyObject); |
| 149 | 0 | copyFrames(copyObject); |
| 150 | 0 | } |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public ID3v22Tag(AbstractTag mp3tag) |
| 157 | 80 | { |
| 158 | 80 | frameMap = new LinkedHashMap(); |
| 159 | 80 | logger.info("Creating tag from a tag of a different version"); |
| 160 | |
|
| 161 | 80 | if (mp3tag != null) |
| 162 | |
{ |
| 163 | |
ID3v24Tag convertedTag; |
| 164 | |
|
| 165 | 80 | if ((!(mp3tag instanceof ID3v23Tag)) && (mp3tag instanceof ID3v22Tag)) |
| 166 | |
{ |
| 167 | 0 | throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument"); |
| 168 | |
} |
| 169 | |
|
| 170 | 80 | else if (mp3tag instanceof ID3v24Tag) |
| 171 | |
{ |
| 172 | 36 | convertedTag = (ID3v24Tag) mp3tag; |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
else |
| 177 | |
{ |
| 178 | 44 | convertedTag = new ID3v24Tag(mp3tag); |
| 179 | |
} |
| 180 | |
|
| 181 | 80 | copyPrimitives(convertedTag); |
| 182 | |
|
| 183 | 80 | copyFrames(convertedTag); |
| 184 | 80 | logger.info("Created tag from a tag of a different version"); |
| 185 | |
} |
| 186 | 80 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
public ID3v22Tag(ByteBuffer buffer, String loggingFilename) throws TagException |
| 196 | 278 | { |
| 197 | 278 | setLoggingFilename(loggingFilename); |
| 198 | 278 | this.read(buffer); |
| 199 | 266 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
public ID3v22Tag(ByteBuffer buffer) throws TagException |
| 210 | |
{ |
| 211 | 0 | this(buffer, ""); |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public String getIdentifier() |
| 218 | |
{ |
| 219 | 12 | return "ID3v2_2.20"; |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
public int getSize() |
| 229 | |
{ |
| 230 | 0 | int size = TAG_HEADER_LENGTH; |
| 231 | 0 | size += super.getSize(); |
| 232 | 0 | return size; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
public boolean equals(Object obj) |
| 241 | |
{ |
| 242 | 0 | if (!(obj instanceof ID3v22Tag)) |
| 243 | |
{ |
| 244 | 0 | return false; |
| 245 | |
} |
| 246 | 0 | ID3v22Tag object = (ID3v22Tag) obj; |
| 247 | 0 | if (this.compression != object.compression) |
| 248 | |
{ |
| 249 | 0 | return false; |
| 250 | |
} |
| 251 | 0 | return this.unsynchronization == object.unsynchronization && super.equals(obj); |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
protected void addFrame(AbstractID3v2Frame frame) |
| 256 | |
{ |
| 257 | |
try |
| 258 | |
{ |
| 259 | |
|
| 260 | 228 | if ((frame.getIdentifier().equals(ID3v24Frames.FRAME_ID_YEAR)) && (frame.getBody() instanceof FrameBodyTDRC)) |
| 261 | |
{ |
| 262 | 12 | translateFrame(frame); |
| 263 | |
} |
| 264 | |
else |
| 265 | |
{ |
| 266 | 216 | ID3v22Frame newFrame = new ID3v22Frame(frame); |
| 267 | 216 | copyFrameIntoMap(newFrame.getIdentifier(), newFrame); |
| 268 | |
} |
| 269 | |
} |
| 270 | 0 | catch (InvalidFrameException ife) |
| 271 | |
{ |
| 272 | 0 | logger.log(Level.SEVERE, "Unable to convert frame:" + frame.getIdentifier()); |
| 273 | 228 | } |
| 274 | 228 | } |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
public int readSize(ByteBuffer buffer) |
| 283 | |
{ |
| 284 | |
|
| 285 | 0 | byte flags = buffer.get(); |
| 286 | |
|
| 287 | |
|
| 288 | 0 | int size = ID3SyncSafeInteger.bufferToValue(buffer); |
| 289 | |
|
| 290 | |
|
| 291 | 0 | return size + TAG_HEADER_LENGTH; |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
private void readHeaderFlags(ByteBuffer byteBuffer) throws TagException |
| 301 | |
{ |
| 302 | |
|
| 303 | 266 | byte flags = byteBuffer.get(); |
| 304 | 266 | unsynchronization = (flags & MASK_V22_UNSYNCHRONIZATION) != 0; |
| 305 | 266 | compression = (flags & MASK_V22_COMPRESSION) != 0; |
| 306 | |
|
| 307 | 266 | if (unsynchronization) |
| 308 | |
{ |
| 309 | 24 | logger.info(ErrorMessage.ID3_TAG_UNSYNCHRONIZED.getMsg(getLoggingFilename())); |
| 310 | |
} |
| 311 | |
|
| 312 | 266 | if (compression) |
| 313 | |
{ |
| 314 | 8 | logger.info(ErrorMessage.ID3_TAG_COMPRESSED.getMsg(getLoggingFilename())); |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | 266 | if ((flags & FileConstants.BIT5) != 0) |
| 319 | |
{ |
| 320 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT5)); |
| 321 | |
} |
| 322 | 266 | if ((flags & FileConstants.BIT4) != 0) |
| 323 | |
{ |
| 324 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT4)); |
| 325 | |
} |
| 326 | 266 | if ((flags & FileConstants.BIT3) != 0) |
| 327 | |
{ |
| 328 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT3)); |
| 329 | |
} |
| 330 | 266 | if ((flags & FileConstants.BIT2) != 0) |
| 331 | |
{ |
| 332 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT2)); |
| 333 | |
} |
| 334 | 266 | if ((flags & FileConstants.BIT1) != 0) |
| 335 | |
{ |
| 336 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT1)); |
| 337 | |
} |
| 338 | 266 | if ((flags & FileConstants.BIT0) != 0) |
| 339 | |
{ |
| 340 | 0 | logger.warning(ErrorMessage.ID3_INVALID_OR_UNKNOWN_FLAG_SET.getMsg(getLoggingFilename(), FileConstants.BIT3)); |
| 341 | |
} |
| 342 | 266 | } |
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
@Override |
| 348 | |
public void read(ByteBuffer byteBuffer) throws TagException |
| 349 | |
{ |
| 350 | |
int size; |
| 351 | 278 | if (!seek(byteBuffer)) |
| 352 | |
{ |
| 353 | 12 | throw new TagNotFoundException("ID3v2.20 tag not found"); |
| 354 | |
} |
| 355 | 266 | logger.info(getLoggingFilename() + ":" + "Reading tag from file"); |
| 356 | |
|
| 357 | |
|
| 358 | 266 | readHeaderFlags(byteBuffer); |
| 359 | |
|
| 360 | |
|
| 361 | 266 | size = ID3SyncSafeInteger.bufferToValue(byteBuffer); |
| 362 | |
|
| 363 | |
|
| 364 | 266 | ByteBuffer bufferWithoutHeader = byteBuffer.slice(); |
| 365 | |
|
| 366 | |
|
| 367 | 266 | if (unsynchronization) |
| 368 | |
{ |
| 369 | 24 | bufferWithoutHeader = ID3Unsynchronization.synchronize(bufferWithoutHeader); |
| 370 | |
} |
| 371 | 266 | readFrames(bufferWithoutHeader, size); |
| 372 | 266 | logger.info(getLoggingFilename() + ":" + "Loaded Frames,there are:" + frameMap.keySet().size()); |
| 373 | 266 | } |
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
protected void readFrames(ByteBuffer byteBuffer, int size) |
| 381 | |
{ |
| 382 | |
|
| 383 | |
ID3v22Frame next; |
| 384 | 266 | frameMap = new LinkedHashMap(); |
| 385 | |
|
| 386 | 266 | this.fileReadSize = size; |
| 387 | 266 | logger.finest(getLoggingFilename() + ":" + "Start of frame body at:" + byteBuffer.position() + ",frames sizes and padding is:" + size); |
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | 1221 | while (byteBuffer.position() < size) |
| 394 | |
{ |
| 395 | |
try |
| 396 | |
{ |
| 397 | |
|
| 398 | 1209 | logger.finest(getLoggingFilename() + ":" + "looking for next frame at:" + byteBuffer.position()); |
| 399 | 1209 | next = new ID3v22Frame(byteBuffer, getLoggingFilename()); |
| 400 | 951 | String id = next.getIdentifier(); |
| 401 | 951 | loadFrameIntoMap(id, next); |
| 402 | |
} |
| 403 | |
|
| 404 | 4 | catch (EmptyFrameException ex) |
| 405 | |
{ |
| 406 | 4 | logger.warning(getLoggingFilename() + ":" + "Empty Frame:" + ex.getMessage()); |
| 407 | 4 | this.emptyFrameBytes += ID3v22Frame.FRAME_HEADER_SIZE; |
| 408 | |
} |
| 409 | 253 | catch (InvalidFrameIdentifierException ifie) |
| 410 | |
{ |
| 411 | 253 | logger.info(getLoggingFilename() + ":" + "Invalid Frame Identifier:" + ifie.getMessage()); |
| 412 | 253 | this.invalidFrameBytes++; |
| 413 | |
|
| 414 | 253 | break; |
| 415 | |
} |
| 416 | |
|
| 417 | 1 | catch (InvalidFrameException ife) |
| 418 | |
{ |
| 419 | 1 | logger.warning(getLoggingFilename() + ":" + "Invalid Frame:" + ife.getMessage()); |
| 420 | 1 | this.invalidFrameBytes++; |
| 421 | |
|
| 422 | 1 | break; |
| 423 | 955 | } |
| 424 | |
} |
| 425 | 266 | } |
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
protected void translateFrame(AbstractID3v2Frame frame) |
| 434 | |
{ |
| 435 | 12 | FrameBodyTDRC tmpBody = (FrameBodyTDRC) frame.getBody(); |
| 436 | |
ID3v22Frame newFrame; |
| 437 | 12 | if (tmpBody.getYear().length() != 0) |
| 438 | |
{ |
| 439 | |
|
| 440 | 12 | newFrame = new ID3v22Frame(ID3v22Frames.FRAME_ID_V2_TYER); |
| 441 | 12 | ((AbstractFrameBodyTextInfo) newFrame.getBody()).setText(tmpBody.getYear()); |
| 442 | 12 | frameMap.put(newFrame.getIdentifier(), newFrame); |
| 443 | |
} |
| 444 | 12 | if (tmpBody.getTime().length() != 0) |
| 445 | |
{ |
| 446 | |
|
| 447 | 0 | newFrame = new ID3v22Frame(ID3v22Frames.FRAME_ID_V2_TIME); |
| 448 | 0 | ((AbstractFrameBodyTextInfo) newFrame.getBody()).setText(tmpBody.getTime()); |
| 449 | 0 | frameMap.put(newFrame.getIdentifier(), newFrame); |
| 450 | |
} |
| 451 | 12 | } |
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
private ByteBuffer writeHeaderToBuffer(int padding, int size) throws IOException |
| 464 | |
{ |
| 465 | 252 | compression = false; |
| 466 | |
|
| 467 | |
|
| 468 | 252 | ByteBuffer headerBuffer = ByteBuffer.allocate(TAG_HEADER_LENGTH); |
| 469 | |
|
| 470 | |
|
| 471 | 252 | headerBuffer.put(TAG_ID); |
| 472 | |
|
| 473 | 252 | headerBuffer.put(getMajorVersion()); |
| 474 | |
|
| 475 | 252 | headerBuffer.put(getRevision()); |
| 476 | |
|
| 477 | |
|
| 478 | 252 | byte flags = (byte) 0; |
| 479 | 252 | if (unsynchronization) |
| 480 | |
{ |
| 481 | 16 | flags |= (byte) MASK_V22_UNSYNCHRONIZATION; |
| 482 | |
} |
| 483 | 252 | if (compression) |
| 484 | |
{ |
| 485 | 0 | flags |= (byte) MASK_V22_COMPRESSION; |
| 486 | |
} |
| 487 | |
|
| 488 | 252 | headerBuffer.put(flags); |
| 489 | |
|
| 490 | |
|
| 491 | 252 | headerBuffer.put(ID3SyncSafeInteger.valueToBuffer(padding + size)); |
| 492 | 252 | headerBuffer.flip(); |
| 493 | 252 | return headerBuffer; |
| 494 | |
} |
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
@Override |
| 500 | |
public void write(File file, long audioStartLocation) throws IOException |
| 501 | |
{ |
| 502 | 252 | logger.info("Writing tag to file"); |
| 503 | |
|
| 504 | |
|
| 505 | 252 | byte[] bodyByteBuffer = writeFramesToBuffer().toByteArray(); |
| 506 | |
|
| 507 | |
|
| 508 | 252 | unsynchronization = TagOptionSingleton.getInstance().isUnsyncTags() && ID3Unsynchronization.requiresUnsynchronization(bodyByteBuffer); |
| 509 | 252 | if (isUnsynchronization()) |
| 510 | |
{ |
| 511 | 16 | bodyByteBuffer = ID3Unsynchronization.unsynchronize(bodyByteBuffer); |
| 512 | 16 | logger.info(getLoggingFilename() + ":bodybytebuffer:sizeafterunsynchronisation:" + bodyByteBuffer.length); |
| 513 | |
} |
| 514 | |
|
| 515 | 252 | int sizeIncPadding = calculateTagSize(bodyByteBuffer.length + TAG_HEADER_LENGTH, (int) audioStartLocation); |
| 516 | 252 | int padding = sizeIncPadding - (bodyByteBuffer.length + TAG_HEADER_LENGTH); |
| 517 | 252 | logger.info(getLoggingFilename() + ":Current audiostart:" + audioStartLocation); |
| 518 | 252 | logger.info(getLoggingFilename() + ":Size including padding:" + sizeIncPadding); |
| 519 | 252 | logger.info(getLoggingFilename() + ":Padding:" + padding); |
| 520 | |
|
| 521 | 252 | ByteBuffer headerBuffer = writeHeaderToBuffer(padding, bodyByteBuffer.length); |
| 522 | 252 | writeBufferToFile(file,headerBuffer, bodyByteBuffer,padding,sizeIncPadding,audioStartLocation); |
| 523 | 252 | } |
| 524 | |
|
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
|
| 529 | |
@Override |
| 530 | |
public void write(WritableByteChannel channel) throws IOException |
| 531 | |
{ |
| 532 | 0 | logger.info(getLoggingFilename() + ":Writing tag to channel"); |
| 533 | |
|
| 534 | 0 | byte[] bodyByteBuffer = writeFramesToBuffer().toByteArray(); |
| 535 | 0 | logger.info(getLoggingFilename() + ":bodybytebuffer:sizebeforeunsynchronisation:" + bodyByteBuffer.length); |
| 536 | |
|
| 537 | |
|
| 538 | 0 | unsynchronization = TagOptionSingleton.getInstance().isUnsyncTags() && ID3Unsynchronization.requiresUnsynchronization(bodyByteBuffer); |
| 539 | 0 | if (isUnsynchronization()) |
| 540 | |
{ |
| 541 | 0 | bodyByteBuffer = ID3Unsynchronization.unsynchronize(bodyByteBuffer); |
| 542 | 0 | logger.info(getLoggingFilename() + ":bodybytebuffer:sizeafterunsynchronisation:" + bodyByteBuffer.length); |
| 543 | |
} |
| 544 | 0 | ByteBuffer headerBuffer = writeHeaderToBuffer(0, bodyByteBuffer.length); |
| 545 | |
|
| 546 | 0 | channel.write(headerBuffer); |
| 547 | 0 | channel.write(ByteBuffer.wrap(bodyByteBuffer)); |
| 548 | 0 | } |
| 549 | |
|
| 550 | |
public void createStructure() |
| 551 | |
{ |
| 552 | 12 | MP3File.getStructureFormatter().openHeadingElement(TYPE_TAG, getIdentifier()); |
| 553 | |
|
| 554 | 12 | super.createStructureHeader(); |
| 555 | |
|
| 556 | |
|
| 557 | 12 | MP3File.getStructureFormatter().openHeadingElement(TYPE_HEADER, ""); |
| 558 | 12 | MP3File.getStructureFormatter().addElement(TYPE_COMPRESSION, this.compression); |
| 559 | 12 | MP3File.getStructureFormatter().addElement(TYPE_UNSYNCHRONISATION, this.unsynchronization); |
| 560 | 12 | MP3File.getStructureFormatter().closeHeadingElement(TYPE_HEADER); |
| 561 | |
|
| 562 | 12 | super.createStructureBody(); |
| 563 | |
|
| 564 | 12 | MP3File.getStructureFormatter().closeHeadingElement(TYPE_TAG); |
| 565 | 12 | } |
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
public boolean isUnsynchronization() |
| 571 | |
{ |
| 572 | 296 | return unsynchronization; |
| 573 | |
} |
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
public boolean isCompression() |
| 579 | |
{ |
| 580 | 36 | return compression; |
| 581 | |
} |
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
|
| 588 | |
|
| 589 | |
public ID3v22Frame createFrame(String id) |
| 590 | |
{ |
| 591 | 148 | return new ID3v22Frame(id); |
| 592 | |
} |
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | |
|
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
public TagField createField(ID3v22FieldKey id3Key, String value) throws KeyNotFoundException, FieldDataInvalidException |
| 608 | |
{ |
| 609 | 0 | if (id3Key == null) |
| 610 | |
{ |
| 611 | 0 | throw new KeyNotFoundException(); |
| 612 | |
} |
| 613 | 0 | return super.doCreateTagField(new FrameAndSubId(id3Key.getFrameId(), id3Key.getSubId()), value); |
| 614 | |
} |
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
public String getFirst(ID3v22FieldKey id3v22FieldKey) throws KeyNotFoundException |
| 624 | |
{ |
| 625 | 48 | if (id3v22FieldKey == null) |
| 626 | |
{ |
| 627 | 0 | throw new KeyNotFoundException(); |
| 628 | |
} |
| 629 | |
|
| 630 | 48 | FrameAndSubId frameAndSubId = new FrameAndSubId(id3v22FieldKey.getFrameId(), id3v22FieldKey.getSubId()); |
| 631 | 48 | if (id3v22FieldKey == ID3v22FieldKey.TRACK) |
| 632 | |
{ |
| 633 | 4 | AbstractID3v2Frame frame = getFirstField(frameAndSubId.getFrameId()); |
| 634 | 4 | return String.valueOf(((FrameBodyTRCK)frame.getBody()).getTrackNo()); |
| 635 | |
} |
| 636 | 44 | else if (id3v22FieldKey == ID3v22FieldKey.TRACK_TOTAL) |
| 637 | |
{ |
| 638 | 0 | AbstractID3v2Frame frame = getFirstField(frameAndSubId.getFrameId()); |
| 639 | 0 | return String.valueOf(((FrameBodyTRCK)frame.getBody()).getTrackTotal()); |
| 640 | |
} |
| 641 | 44 | else if (id3v22FieldKey == ID3v22FieldKey.DISC_NO) |
| 642 | |
{ |
| 643 | 0 | AbstractID3v2Frame frame = getFirstField(frameAndSubId.getFrameId()); |
| 644 | 0 | return String.valueOf(((FrameBodyTPOS)frame.getBody()).getDiscNo()); |
| 645 | |
} |
| 646 | 44 | else if (id3v22FieldKey == ID3v22FieldKey.DISC_TOTAL) |
| 647 | |
{ |
| 648 | 0 | AbstractID3v2Frame frame = getFirstField(frameAndSubId.getFrameId()); |
| 649 | 0 | return String.valueOf(((FrameBodyTPOS)frame.getBody()).getDiscTotal()); |
| 650 | |
} |
| 651 | |
else |
| 652 | |
{ |
| 653 | 44 | return super.doGetFirst(frameAndSubId); |
| 654 | |
} |
| 655 | |
} |
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
public void deleteField(ID3v22FieldKey id3v22FieldKey) throws KeyNotFoundException |
| 664 | |
{ |
| 665 | 0 | if (id3v22FieldKey == null) |
| 666 | |
{ |
| 667 | 0 | throw new KeyNotFoundException(); |
| 668 | |
} |
| 669 | 0 | super.doDeleteTagField(new FrameAndSubId(id3v22FieldKey.getFrameId(), id3v22FieldKey.getSubId())); |
| 670 | 0 | } |
| 671 | |
|
| 672 | |
|
| 673 | |
protected FrameAndSubId getFrameAndSubIdFromGenericKey(FieldKey genericKey) |
| 674 | |
{ |
| 675 | 565 | ID3v22FieldKey id3v22FieldKey = ID3v22Frames.getInstanceOf().getId3KeyFromGenericKey(genericKey); |
| 676 | 565 | if (id3v22FieldKey == null) |
| 677 | |
{ |
| 678 | 0 | throw new KeyNotFoundException(); |
| 679 | |
} |
| 680 | 565 | return new FrameAndSubId(id3v22FieldKey.getFrameId(), id3v22FieldKey.getSubId()); |
| 681 | |
} |
| 682 | |
|
| 683 | |
protected ID3Frames getID3Frames() |
| 684 | |
{ |
| 685 | 64 | return ID3v22Frames.getInstanceOf(); |
| 686 | |
} |
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
public Comparator getPreferredFrameOrderComparator() |
| 694 | |
{ |
| 695 | 252 | return ID3v22PreferredFrameOrderComparator.getInstanceof(); |
| 696 | |
} |
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
|
| 701 | |
public List<Artwork> getArtworkList() |
| 702 | |
{ |
| 703 | 24 | List<TagField> coverartList = getFields(FieldKey.COVER_ART); |
| 704 | 24 | List<Artwork> artworkList = new ArrayList<Artwork>(coverartList.size()); |
| 705 | |
|
| 706 | 24 | for(TagField next:coverartList) |
| 707 | |
{ |
| 708 | 12 | FrameBodyPIC coverArt = (FrameBodyPIC) ((AbstractID3v2Frame) next).getBody(); |
| 709 | 12 | Artwork artwork = new Artwork(); |
| 710 | 12 | artwork.setMimeType(ImageFormats.getMimeTypeForFormat(coverArt.getFormatType())); |
| 711 | 12 | artwork.setPictureType(coverArt.getPictureType()); |
| 712 | 12 | artwork.setBinaryData(coverArt.getImageData()); |
| 713 | 12 | artworkList.add(artwork); |
| 714 | 12 | } |
| 715 | 24 | return artworkList; |
| 716 | |
} |
| 717 | |
|
| 718 | |
|
| 719 | |
|
| 720 | |
|
| 721 | |
public TagField createField(Artwork artwork) throws FieldDataInvalidException |
| 722 | |
{ |
| 723 | 4 | AbstractID3v2Frame frame = createFrame(getFrameAndSubIdFromGenericKey(FieldKey.COVER_ART).getFrameId()); |
| 724 | 4 | FrameBodyPIC body = (FrameBodyPIC) frame.getBody(); |
| 725 | 4 | body.setObjectValue(DataTypes.OBJ_PICTURE_DATA, artwork.getBinaryData()); |
| 726 | 4 | body.setObjectValue(DataTypes.OBJ_PICTURE_TYPE, artwork.getPictureType()); |
| 727 | 4 | body.setObjectValue(DataTypes.OBJ_IMAGE_FORMAT, ImageFormats.getFormatForMimeType(artwork.getMimeType())); |
| 728 | 4 | body.setObjectValue(DataTypes.OBJ_DESCRIPTION, ""); |
| 729 | 4 | return frame; |
| 730 | |
} |
| 731 | |
|
| 732 | |
public TagField createArtworkField(byte[] data, String mimeType) |
| 733 | |
{ |
| 734 | 8 | AbstractID3v2Frame frame = createFrame(getFrameAndSubIdFromGenericKey(FieldKey.COVER_ART).getFrameId()); |
| 735 | 8 | FrameBodyPIC body = (FrameBodyPIC) frame.getBody(); |
| 736 | 8 | body.setObjectValue(DataTypes.OBJ_PICTURE_DATA, data); |
| 737 | 8 | body.setObjectValue(DataTypes.OBJ_PICTURE_TYPE, PictureTypes.DEFAULT_ID); |
| 738 | 8 | body.setObjectValue(DataTypes.OBJ_IMAGE_FORMAT, ImageFormats.getFormatForMimeType(mimeType)); |
| 739 | 8 | body.setObjectValue(DataTypes.OBJ_DESCRIPTION, ""); |
| 740 | 8 | return frame; |
| 741 | |
} |
| 742 | |
} |