FileDocCategorySizeDatePackage
FlacTagCreator.javaAPI DocJaudiotagger 2.0.44802Wed Jun 08 11:59:18 BST 2011org.jaudiotagger.audio.flac

FlacTagCreator

public class FlacTagCreator extends org.jaudiotagger.audio.generic.AbstractTagCreator
Create the tag data ready for writing to flac file

Fields Summary
public static Logger
logger
public static final int
DEFAULT_PADDING
private static final org.jaudiotagger.tag.vorbiscomment.VorbisCommentCreator
creator
Constructors Summary
Methods Summary
public java.nio.ByteBufferconvert(org.jaudiotagger.tag.Tag tag, int paddingSize)

param
tag
param
paddingSize extra padding to be added
return
throws
UnsupportedEncodingException


                     
           
    
        logger.config("Convert flac tag:padding:" + paddingSize);
        FlacTag flacTag = (FlacTag) tag;

        int tagLength = 0;
        ByteBuffer vorbiscomment = null;
        if (flacTag.getVorbisCommentTag() != null)
        {
            vorbiscomment = creator.convert(flacTag.getVorbisCommentTag());
            tagLength = vorbiscomment.capacity() + MetadataBlockHeader.HEADER_LENGTH;
        }
        for (MetadataBlockDataPicture image : flacTag.getImages())
        {
            tagLength += image.getBytes().length + MetadataBlockHeader.HEADER_LENGTH;
        }

        logger.config("Convert flac tag:taglength:" + tagLength);
        ByteBuffer buf = ByteBuffer.allocate(tagLength + paddingSize);

        MetadataBlockHeader vorbisHeader;
        //If there are other metadata blocks
        if (flacTag.getVorbisCommentTag() != null)
        {
            if ((paddingSize > 0) || (flacTag.getImages().size() > 0))
            {
                vorbisHeader = new MetadataBlockHeader(false, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
            }
            else
            {
                vorbisHeader = new MetadataBlockHeader(true, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
            }
            buf.put(vorbisHeader.getBytes());
            buf.put(vorbiscomment);
        }

        //Images
        ListIterator<MetadataBlockDataPicture> li = flacTag.getImages().listIterator();
        while (li.hasNext())
        {
            MetadataBlockDataPicture imageField = li.next();
            MetadataBlockHeader imageHeader;

            if (paddingSize > 0 || li.hasNext())
            {
                imageHeader = new MetadataBlockHeader(false, BlockType.PICTURE, imageField.getLength());
            }
            else
            {
                imageHeader = new MetadataBlockHeader(true, BlockType.PICTURE, imageField.getLength());
            }
            buf.put(imageHeader.getBytes());
            buf.put(imageField.getBytes());
        }

        //Padding
        logger.config("Convert flac tag at" + buf.position());
        if (paddingSize > 0)
        {
            int paddingDataSize = paddingSize - MetadataBlockHeader.HEADER_LENGTH;
            MetadataBlockHeader paddingHeader = new MetadataBlockHeader(true, BlockType.PADDING, paddingDataSize);
            MetadataBlockDataPadding padding = new MetadataBlockDataPadding(paddingDataSize);
            buf.put(paddingHeader.getBytes());
            buf.put(padding.getBytes());
        }
        buf.rewind();
        return buf;