| 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.audio.mp3.MP3File; |
| 19 | |
import org.jaudiotagger.tag.InvalidFrameException; |
| 20 | |
import org.jaudiotagger.tag.InvalidTagException; |
| 21 | |
import org.jaudiotagger.tag.TagField; |
| 22 | |
import org.jaudiotagger.tag.TagOptionSingleton; |
| 23 | |
import org.jaudiotagger.tag.id3.framebody.AbstractID3v2FrameBody; |
| 24 | |
import org.jaudiotagger.tag.id3.framebody.FrameBodyUnsupported; |
| 25 | |
|
| 26 | |
import java.io.ByteArrayOutputStream; |
| 27 | |
import java.lang.reflect.Constructor; |
| 28 | |
import java.lang.reflect.InvocationTargetException; |
| 29 | |
import java.nio.ByteBuffer; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public abstract class AbstractID3v2Frame extends AbstractTagFrame implements TagField |
| 40 | |
{ |
| 41 | |
|
| 42 | |
protected static final String TYPE_FRAME = "frame"; |
| 43 | |
protected static final String TYPE_FRAME_SIZE = "frameSize"; |
| 44 | |
protected static final String UNSUPPORTED_ID = "Unsupported"; |
| 45 | |
|
| 46 | |
|
| 47 | 3419 | protected String identifier = ""; |
| 48 | |
|
| 49 | |
|
| 50 | |
protected int frameSize; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 3419 | private String loggingFilename = ""; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
protected AbstractID3v2Frame() |
| 61 | 3180 | { |
| 62 | |
; |
| 63 | 3180 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 3419 | StatusFlags statusFlags = null; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 3419 | EncodingFlags encodingFlags = null; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public AbstractID3v2Frame(AbstractID3v2Frame frame) |
| 79 | |
{ |
| 80 | 0 | super(frame); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public AbstractID3v2Frame(AbstractID3v2FrameBody body) |
| 87 | 0 | { |
| 88 | 0 | this.frameBody = body; |
| 89 | 0 | this.frameBody.setHeader(this); |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public AbstractID3v2Frame(String identifier) |
| 97 | 239 | { |
| 98 | 239 | logger.info("Creating empty frame of type" + identifier); |
| 99 | 239 | this.identifier = identifier; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
try |
| 104 | |
{ |
| 105 | 239 | Class<AbstractID3v2FrameBody> c = (Class<AbstractID3v2FrameBody>) Class.forName("org.jaudiotagger.tag.id3.framebody.FrameBody" + identifier); |
| 106 | 239 | frameBody = c.newInstance(); |
| 107 | |
} |
| 108 | 0 | catch (ClassNotFoundException cnfe) |
| 109 | |
{ |
| 110 | 0 | logger.severe(cnfe.getMessage()); |
| 111 | 0 | frameBody = new FrameBodyUnsupported(identifier); |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | catch (InstantiationException ie) |
| 115 | |
{ |
| 116 | 0 | logger.log(Level.SEVERE, "InstantiationException:" + identifier, ie); |
| 117 | 0 | throw new RuntimeException(ie); |
| 118 | |
} |
| 119 | |
|
| 120 | 0 | catch (IllegalAccessException iae) |
| 121 | |
{ |
| 122 | 0 | logger.log(Level.SEVERE, "IllegalAccessException:" + identifier, iae); |
| 123 | 0 | throw new RuntimeException(iae); |
| 124 | 239 | } |
| 125 | 239 | frameBody.setHeader(this); |
| 126 | 239 | if (this instanceof ID3v24Frame) |
| 127 | |
{ |
| 128 | 161 | frameBody.setTextEncoding(TagOptionSingleton.getInstance().getId3v24DefaultTextEncoding()); |
| 129 | |
} |
| 130 | 78 | else if (this instanceof ID3v23Frame) |
| 131 | |
{ |
| 132 | 78 | frameBody.setTextEncoding(TagOptionSingleton.getInstance().getId3v23DefaultTextEncoding()); |
| 133 | |
} |
| 134 | |
|
| 135 | 239 | logger.info("Created empty frame of type" + identifier); |
| 136 | 239 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
protected String getLoggingFilename() |
| 144 | |
{ |
| 145 | 4036 | return loggingFilename; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
protected void setLoggingFilename(String loggingFilename) |
| 154 | |
{ |
| 155 | 1848 | this.loggingFilename = loggingFilename; |
| 156 | 1848 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public String getId() |
| 167 | |
{ |
| 168 | 251 | return getIdentifier(); |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public String getIdentifier() |
| 177 | |
{ |
| 178 | 14014 | return identifier; |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
public void copyContent(TagField field) |
| 183 | |
{ |
| 184 | |
|
| 185 | 0 | } |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
@SuppressWarnings("unchecked") |
| 196 | |
protected AbstractID3v2FrameBody readBody(String identifier, ByteBuffer byteBuffer, int frameSize) throws InvalidFrameException |
| 197 | |
{ |
| 198 | |
|
| 199 | |
|
| 200 | 1565 | logger.finest("Creating framebody:start"); |
| 201 | |
|
| 202 | |
AbstractID3v2FrameBody frameBody; |
| 203 | |
try |
| 204 | |
{ |
| 205 | 1565 | Class<AbstractID3v2FrameBody> c = (Class<AbstractID3v2FrameBody>) Class.forName("org.jaudiotagger.tag.id3.framebody.FrameBody" + identifier); |
| 206 | 1560 | Class<?>[] constructorParameterTypes = {Class.forName("java.nio.ByteBuffer"), Integer.TYPE}; |
| 207 | 1560 | Object[] constructorParameterValues = {byteBuffer, frameSize}; |
| 208 | 1560 | Constructor<AbstractID3v2FrameBody> construct = c.getConstructor(constructorParameterTypes); |
| 209 | 1560 | frameBody = (construct.newInstance(constructorParameterValues)); |
| 210 | |
} |
| 211 | |
|
| 212 | 5 | catch (ClassNotFoundException cex) |
| 213 | |
{ |
| 214 | 5 | logger.info(getLoggingFilename() + ":" + "Identifier not recognised:" + identifier + " using FrameBodyUnsupported"); |
| 215 | |
try |
| 216 | |
{ |
| 217 | 5 | frameBody = new FrameBodyUnsupported(byteBuffer, frameSize); |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | 0 | catch (InvalidFrameException ife) |
| 222 | |
{ |
| 223 | 0 | throw ife; |
| 224 | |
} |
| 225 | 0 | catch (InvalidTagException te) |
| 226 | |
{ |
| 227 | 0 | throw new InvalidFrameException(te.getMessage()); |
| 228 | 5 | } |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | 3 | catch (InvocationTargetException ite) |
| 233 | |
{ |
| 234 | 3 | logger.severe(getLoggingFilename() + ":" + "An error occurred within abstractID3v2FrameBody for identifier:" + identifier + ":" + ite.getCause().getMessage()); |
| 235 | 3 | if (ite.getCause() instanceof Error) |
| 236 | |
{ |
| 237 | 0 | throw (Error) ite.getCause(); |
| 238 | |
} |
| 239 | 3 | else if (ite.getCause() instanceof RuntimeException) |
| 240 | |
{ |
| 241 | 0 | throw (RuntimeException) ite.getCause(); |
| 242 | |
} |
| 243 | |
else |
| 244 | |
{ |
| 245 | 3 | throw new InvalidFrameException(ite.getCause().getMessage()); |
| 246 | |
} |
| 247 | |
} |
| 248 | |
|
| 249 | 0 | catch (NoSuchMethodException sme) |
| 250 | |
{ |
| 251 | 0 | logger.log(Level.SEVERE, getLoggingFilename() + ":" + "No such method:" + sme.getMessage(), sme); |
| 252 | 0 | throw new RuntimeException(sme.getMessage()); |
| 253 | |
} |
| 254 | |
|
| 255 | 0 | catch (InstantiationException ie) |
| 256 | |
{ |
| 257 | 0 | logger.log(Level.SEVERE, getLoggingFilename() + ":" + "Instantiation exception:" + ie.getMessage(), ie); |
| 258 | 0 | throw new RuntimeException(ie.getMessage()); |
| 259 | |
} |
| 260 | |
|
| 261 | 0 | catch (IllegalAccessException iae) |
| 262 | |
{ |
| 263 | 0 | logger.log(Level.SEVERE, getLoggingFilename() + ":" + "Illegal access exception :" + iae.getMessage(), iae); |
| 264 | 0 | throw new RuntimeException(iae.getMessage()); |
| 265 | 1562 | } |
| 266 | 1562 | logger.finest(getLoggingFilename() + ":" + "Created framebody:end" + frameBody.getIdentifier()); |
| 267 | 1562 | frameBody.setHeader(this); |
| 268 | 1562 | return frameBody; |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
@SuppressWarnings("unchecked") |
| 288 | |
protected AbstractID3v2FrameBody readBody(String identifier, AbstractID3v2FrameBody body) throws InvalidFrameException |
| 289 | |
{ |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
AbstractID3v2FrameBody frameBody; |
| 294 | |
try |
| 295 | |
{ |
| 296 | 92 | Class<AbstractID3v2FrameBody> c = (Class<AbstractID3v2FrameBody>) Class.forName("org.jaudiotagger.tag.id3.framebody.FrameBody" + identifier); |
| 297 | 92 | Class<?>[] constructorParameterTypes = {body.getClass()}; |
| 298 | 92 | Object[] constructorParameterValues = {body}; |
| 299 | 92 | Constructor<AbstractID3v2FrameBody> construct = c.getConstructor(constructorParameterTypes); |
| 300 | 92 | frameBody = (construct.newInstance(constructorParameterValues)); |
| 301 | |
} |
| 302 | 0 | catch (ClassNotFoundException cex) |
| 303 | |
{ |
| 304 | 0 | logger.info("Identifier not recognised:" + identifier + " unable to create framebody"); |
| 305 | 0 | throw new InvalidFrameException("FrameBody" + identifier + " does not exist"); |
| 306 | |
} |
| 307 | |
|
| 308 | 0 | catch (NoSuchMethodException sme) |
| 309 | |
{ |
| 310 | 0 | logger.log(Level.SEVERE, "No such method:" + sme.getMessage(), sme); |
| 311 | 0 | throw new InvalidFrameException("FrameBody" + identifier + " does not have a constructor that takes:" + body.getClass().getName()); |
| 312 | |
} |
| 313 | 0 | catch (InvocationTargetException ite) |
| 314 | |
{ |
| 315 | 0 | logger.severe("An error occurred within abstractID3v2FrameBody"); |
| 316 | 0 | logger.log(Level.SEVERE, "Invocation target exception:" + ite.getCause().getMessage(), ite.getCause()); |
| 317 | 0 | if (ite.getCause() instanceof Error) |
| 318 | |
{ |
| 319 | 0 | throw (Error) ite.getCause(); |
| 320 | |
} |
| 321 | 0 | else if (ite.getCause() instanceof RuntimeException) |
| 322 | |
{ |
| 323 | 0 | throw (RuntimeException) ite.getCause(); |
| 324 | |
} |
| 325 | |
else |
| 326 | |
{ |
| 327 | 0 | throw new InvalidFrameException(ite.getCause().getMessage()); |
| 328 | |
} |
| 329 | |
} |
| 330 | |
|
| 331 | |
|
| 332 | 0 | catch (InstantiationException ie) |
| 333 | |
{ |
| 334 | 0 | logger.log(Level.SEVERE, "Instantiation exception:" + ie.getMessage(), ie); |
| 335 | 0 | throw new RuntimeException(ie.getMessage()); |
| 336 | |
} |
| 337 | |
|
| 338 | 0 | catch (IllegalAccessException iae) |
| 339 | |
{ |
| 340 | 0 | logger.log(Level.SEVERE, "Illegal access exception :" + iae.getMessage(), iae); |
| 341 | 0 | throw new RuntimeException(iae.getMessage()); |
| 342 | 92 | } |
| 343 | |
|
| 344 | 92 | logger.finer("frame Body created" + frameBody.getIdentifier()); |
| 345 | 92 | frameBody.setHeader(this); |
| 346 | 92 | return frameBody; |
| 347 | |
} |
| 348 | |
|
| 349 | |
public byte[] getRawContent() |
| 350 | |
{ |
| 351 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 352 | 0 | write(baos); |
| 353 | 0 | return baos.toByteArray(); |
| 354 | |
} |
| 355 | |
|
| 356 | |
public abstract void write(ByteArrayOutputStream tagBuffer); |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
public void isBinary(boolean b) |
| 362 | |
{ |
| 363 | |
|
| 364 | 0 | } |
| 365 | |
|
| 366 | |
|
| 367 | |
public boolean isEmpty() |
| 368 | |
{ |
| 369 | 0 | AbstractTagFrameBody body = this.getBody(); |
| 370 | 0 | if (body == null) |
| 371 | |
{ |
| 372 | 0 | return true; |
| 373 | |
} |
| 374 | |
|
| 375 | 0 | return false; |
| 376 | |
} |
| 377 | |
|
| 378 | |
protected StatusFlags getStatusFlags() |
| 379 | |
{ |
| 380 | 0 | return statusFlags; |
| 381 | |
} |
| 382 | |
|
| 383 | |
protected EncodingFlags getEncodingFlags() |
| 384 | |
{ |
| 385 | 0 | return encodingFlags; |
| 386 | |
} |
| 387 | |
|
| 388 | |
class StatusFlags |
| 389 | |
{ |
| 390 | |
protected static final String TYPE_FLAGS = "statusFlags"; |
| 391 | |
|
| 392 | |
protected byte originalFlags; |
| 393 | |
protected byte writeFlags; |
| 394 | |
|
| 395 | |
protected StatusFlags() |
| 396 | 2600 | { |
| 397 | |
|
| 398 | 2600 | } |
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
public byte getOriginalFlags() |
| 404 | |
{ |
| 405 | 752 | return originalFlags; |
| 406 | |
} |
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
public byte getWriteFlags() |
| 412 | |
{ |
| 413 | 768 | return writeFlags; |
| 414 | |
} |
| 415 | |
|
| 416 | |
public void createStructure() |
| 417 | |
{ |
| 418 | 0 | } |
| 419 | |
|
| 420 | |
|
| 421 | |
} |
| 422 | |
|
| 423 | |
class EncodingFlags |
| 424 | |
{ |
| 425 | |
protected static final String TYPE_FLAGS = "encodingFlags"; |
| 426 | |
|
| 427 | |
protected byte flags; |
| 428 | |
|
| 429 | |
protected EncodingFlags() |
| 430 | 478 | { |
| 431 | 478 | resetFlags(); |
| 432 | 478 | } |
| 433 | |
|
| 434 | |
protected EncodingFlags(byte flags) |
| 435 | 2122 | { |
| 436 | 2122 | setFlags(flags); |
| 437 | 2122 | } |
| 438 | |
|
| 439 | |
public byte getFlags() |
| 440 | |
{ |
| 441 | 918 | return flags; |
| 442 | |
} |
| 443 | |
|
| 444 | |
public void setFlags(byte flags) |
| 445 | |
{ |
| 446 | 3368 | this.flags = flags; |
| 447 | 3368 | } |
| 448 | |
|
| 449 | |
public void resetFlags() |
| 450 | |
{ |
| 451 | 1246 | setFlags((byte) 0); |
| 452 | 1246 | } |
| 453 | |
|
| 454 | |
public void createStructure() |
| 455 | |
{ |
| 456 | 0 | } |
| 457 | |
} |
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
public void createStructure() |
| 463 | |
{ |
| 464 | 0 | MP3File.getStructureFormatter().openHeadingElement(TYPE_FRAME, getIdentifier()); |
| 465 | 0 | MP3File.getStructureFormatter().closeHeadingElement(TYPE_FRAME); |
| 466 | 0 | } |
| 467 | |
} |