Package org.jaudiotagger.tag.id3

Source Code of org.jaudiotagger.tag.id3.ID3Compression

package org.jaudiotagger.tag.id3;

import org.jaudiotagger.logging.ErrorMessage;
import org.jaudiotagger.tag.InvalidFrameException;

import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

/**
* compresses frame data
* <p/>
* Is currently required for V23Frames and V24Frames
* <p/>
*/
//TODO also need to support compress framedata
public class ID3Compression {
    //Logger
    public static Logger logger = Logger.getLogger("org.jaudiotagger.tag.id3");

    /**
     * Decompress realFrameSize bytes to decompressedFrameSize bytes and return as ByteBuffer
     *
     * @param byteBuffer
     * @param decompressedFrameSize
     * @param realFrameSize
     * @return
     * @throws org.jaudiotagger.tag.InvalidFrameException
     *
     */
    protected static ByteBuffer uncompress(String identifier, String filename, ByteBuffer byteBuffer, int decompressedFrameSize, int realFrameSize) throws InvalidFrameException {
        logger.config(filename + ":About to decompress " + realFrameSize + " bytes, expect result to be:" + decompressedFrameSize + " bytes");
        // Decompress the bytes into this buffer, size initialized from header field
        byte[] result = new byte[decompressedFrameSize];
        byte[] input = new byte[realFrameSize];

        //Store position ( just after frame header and any extra bits)
        //Read frame data into array, and then put buffer back to where it was
        int position = byteBuffer.position();
        byteBuffer.get(input, 0, realFrameSize);
        byteBuffer.position(position);

        Inflater decompresser = new Inflater();
        decompresser.setInput(input);
        try {
            int inflatedTo = decompresser.inflate(result);
            logger.config(filename + ":Decompressed to " + inflatedTo + " bytes");
        } catch (DataFormatException dfe) {
            logger.log(Level.CONFIG, "Unable to decompress this frame:" + identifier, dfe);

            //Update position of main buffer, so no attempt is made to reread these bytes
            byteBuffer.position(byteBuffer.position() + realFrameSize);
            throw new InvalidFrameException(ErrorMessage.ID3_UNABLE_TO_DECOMPRESS_FRAME.getMsg(identifier, filename, dfe.getMessage()));
        }
        decompresser.end();
        return ByteBuffer.wrap(result);
    }

}
TOP

Related Classes of org.jaudiotagger.tag.id3.ID3Compression

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.