Search in sources :

Example 1 with InvalidFrameIdentifierException

use of org.jaudiotagger.tag.InvalidFrameIdentifierException in project MusicDNA by harjot-oberai.

the class ID3v23Frame method read.

/**
     * Read the frame from a byteBuffer
     *
     * @param byteBuffer buffer to read from
     */
public void read(ByteBuffer byteBuffer) throws InvalidFrameException, InvalidDataTypeException {
    String identifier = readIdentifier(byteBuffer);
    if (!isValidID3v2FrameIdentifier(identifier)) {
        logger.config(getLoggingFilename() + ":Invalid identifier:" + identifier);
        byteBuffer.position(byteBuffer.position() - (getFrameIdSize() - 1));
        throw new InvalidFrameIdentifierException(getLoggingFilename() + ":" + identifier + ":is not a valid ID3v2.30 frame");
    }
    //Read the size field (as Big Endian Int - byte buffers always initialised to Big Endian order)
    frameSize = byteBuffer.getInt();
    if (frameSize < 0) {
        logger.warning(getLoggingFilename() + ":Invalid Frame Size:" + frameSize + ":" + identifier);
        throw new InvalidFrameException(identifier + " is invalid frame:" + frameSize);
    } else if (frameSize == 0) {
        logger.warning(getLoggingFilename() + ":Empty Frame Size:" + identifier);
        //We don'timer process this frame or add to frameMap because contains no useful information
        //Skip the two flag bytes so in correct position for subsequent frames
        byteBuffer.get();
        byteBuffer.get();
        throw new EmptyFrameException(identifier + " is empty frame");
    } else if (frameSize > byteBuffer.remaining()) {
        logger.warning(getLoggingFilename() + ":Invalid Frame size of " + frameSize + " larger than size of" + byteBuffer.remaining() + " before mp3 audio:" + identifier);
        throw new InvalidFrameException(identifier + " is invalid frame:" + frameSize + " larger than size of" + byteBuffer.remaining() + " before mp3 audio:" + identifier);
    }
    //Read the flag bytes
    statusFlags = new StatusFlags(byteBuffer.get());
    encodingFlags = new EncodingFlags(byteBuffer.get());
    String id;
    //If this identifier is a valid v24 identifier or easily converted to v24
    id = ID3Tags.convertFrameID23To24(identifier);
    // Cant easily be converted to v24 but is it a valid v23 identifier
    if (id == null) {
        //  frame body for it.
        if (ID3Tags.isID3v23FrameIdentifier(identifier)) {
            id = identifier;
        } else // Unknown so will be created as FrameBodyUnsupported
        {
            id = UNSUPPORTED_ID;
        }
    }
    logger.fine(getLoggingFilename() + ":Identifier was:" + identifier + " reading using:" + id + "with frame size:" + frameSize);
    //Read extra bits appended to frame header for various encodings
    //These are not included in header size but are included in frame size but won'timer be read when we actually
    //try to read the frame body data
    int extraHeaderBytesCount = 0;
    int decompressedFrameSize = -1;
    if (((EncodingFlags) encodingFlags).isCompression()) {
        //Read the Decompressed Size
        decompressedFrameSize = byteBuffer.getInt();
        extraHeaderBytesCount = FRAME_COMPRESSION_UNCOMPRESSED_SIZE;
        logger.fine(getLoggingFilename() + ":Decompressed frame size is:" + decompressedFrameSize);
    }
    if (((EncodingFlags) encodingFlags).isEncryption()) {
        //Consume the encryption byte
        extraHeaderBytesCount += FRAME_ENCRYPTION_INDICATOR_SIZE;
        encryptionMethod = byteBuffer.get();
    }
    if (((EncodingFlags) encodingFlags).isGrouping()) {
        //Read the Grouping byte, but do nothing with it
        extraHeaderBytesCount += FRAME_GROUPING_INDICATOR_SIZE;
        groupIdentifier = byteBuffer.get();
    }
    if (((EncodingFlags) encodingFlags).isNonStandardFlags()) {
        //Probably corrupt so treat as a standard frame
        logger.severe(getLoggingFilename() + ":InvalidEncodingFlags:" + Hex.asHex(((EncodingFlags) encodingFlags).getFlags()));
    }
    if (((EncodingFlags) encodingFlags).isCompression()) {
        if (decompressedFrameSize > (100 * frameSize)) {
            throw new InvalidFrameException(identifier + " is invalid frame, frame size " + frameSize + " cannot be:" + decompressedFrameSize + " when uncompressed");
        }
    }
    //Work out the real size of the frameBody data
    int realFrameSize = frameSize - extraHeaderBytesCount;
    if (realFrameSize <= 0) {
        throw new InvalidFrameException(identifier + " is invalid frame, realframeSize is:" + realFrameSize);
    }
    ByteBuffer frameBodyBuffer;
    //Read the body data
    try {
        if (((EncodingFlags) encodingFlags).isCompression()) {
            frameBodyBuffer = ID3Compression.uncompress(identifier, getLoggingFilename(), byteBuffer, decompressedFrameSize, realFrameSize);
            if (((EncodingFlags) encodingFlags).isEncryption()) {
                frameBody = readEncryptedBody(id, frameBodyBuffer, decompressedFrameSize);
            } else {
                frameBody = readBody(id, frameBodyBuffer, decompressedFrameSize);
            }
        } else if (((EncodingFlags) encodingFlags).isEncryption()) {
            frameBody = readEncryptedBody(identifier, byteBuffer, frameSize);
        } else {
            //Create Buffer that only contains the body of this frame rather than the remainder of tag
            frameBodyBuffer = byteBuffer.slice();
            frameBodyBuffer.limit(realFrameSize);
            frameBody = readBody(id, frameBodyBuffer, realFrameSize);
        }
        //it then be created as FrameBodyUnsupported
        if (!(frameBody instanceof ID3v23FrameBody)) {
            logger.config(getLoggingFilename() + ":Converted frameBody with:" + identifier + " to deprecated frameBody");
            frameBody = new FrameBodyDeprecated((AbstractID3v2FrameBody) frameBody);
        }
    } finally {
        //Update position of main buffer, so no attempt is made to reread these bytes
        byteBuffer.position(byteBuffer.position() + realFrameSize);
    }
}
Also used : InvalidFrameIdentifierException(org.jaudiotagger.tag.InvalidFrameIdentifierException) InvalidFrameException(org.jaudiotagger.tag.InvalidFrameException) ByteBuffer(java.nio.ByteBuffer) EmptyFrameException(org.jaudiotagger.tag.EmptyFrameException)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 EmptyFrameException (org.jaudiotagger.tag.EmptyFrameException)1 InvalidFrameException (org.jaudiotagger.tag.InvalidFrameException)1 InvalidFrameIdentifierException (org.jaudiotagger.tag.InvalidFrameIdentifierException)1