FileDocCategorySizeDatePackage
FlacTagReader.javaAPI DocJaudiotagger 2.0.44889Sat Oct 22 13:09:06 BST 2011org.jaudiotagger.audio.flac

FlacTagReader

public class FlacTagReader extends Object
Read Flac Tag

Fields Summary
public static Logger
logger
private org.jaudiotagger.tag.vorbiscomment.VorbisCommentReader
vorbisCommentReader
Constructors Summary
Methods Summary
public org.jaudiotagger.tag.flac.FlacTagread(java.io.RandomAccessFile raf)



          
    
        FlacStreamReader flacStream = new FlacStreamReader(raf);
        flacStream.findStream();

        //Hold the metadata
        VorbisCommentTag tag = null;
        List<MetadataBlockDataPicture> images = new ArrayList<MetadataBlockDataPicture>();

        //Seems like we have a valid stream
        boolean isLastBlock = false;
        while (!isLastBlock)
        {
            if(logger.isLoggable(Level.CONFIG))
            {
                logger.config("Looking for MetaBlockHeader at:"+raf.getFilePointer());
            }
            
            //Read the header
            MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
            if(mbh==null)
            {
                break;
            }

            if(logger.isLoggable(Level.CONFIG))
            {
                logger.config("Reading MetadataBlockHeader:"+mbh.toString() + " ending at "+raf.getFilePointer());
            }
            
            //Is it one containing some sort of metadata, therefore interested in it?
            switch (mbh.getBlockType())
            {
                //We got a vorbiscomment comment block, parse it
                case VORBIS_COMMENT:
                    byte[] commentHeaderRawPacket = new byte[mbh.getDataLength()];
                    raf.read(commentHeaderRawPacket);
                    tag = vorbisCommentReader.read(commentHeaderRawPacket, false);
                    break;

                case PICTURE:
                    try
                    {
                        MetadataBlockDataPicture mbdp = new MetadataBlockDataPicture(mbh, raf);
                        images.add(mbdp);
                    }
                    catch (IOException ioe)
                    {
                        logger.warning("Unable to read picture metablock, ignoring:" + ioe.getMessage());
                    }
                    catch (InvalidFrameException ive)
                    {
                        logger.warning("Unable to read picture metablock, ignoring" + ive.getMessage());
                    }

                    break;

                //This is not a metadata block we are interested in so we skip to next block
                default:
                    if(logger.isLoggable(Level.CONFIG))
                    {
                        logger.config("Ignoring MetadataBlock:"+mbh.getBlockType());
                    }
                    raf.seek(raf.getFilePointer() + mbh.getDataLength());
                    break;
            }

            isLastBlock = mbh.isLastBlock();
            mbh = null;
        }

        //Note there may not be either a tag or any images, no problem this is valid however to make it easier we
        //just initialize Flac with an empty VorbisTag
        if (tag == null)
        {
            tag = VorbisCommentTag.createNewTag();
        }
        FlacTag flacTag = new FlacTag(tag, images);
        return flacTag;