Coverage Report - org.jaudiotagger.audio.flac.FlacTagReader
 
Classes in this File Line Coverage Branch Coverage Complexity
FlacTagReader
84%
26/31
100%
7/7
0
FlacTagReader$1
100%
1/1
N/A
0
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 package org.jaudiotagger.audio.flac;
 20  
 
 21  
 import org.jaudiotagger.audio.exceptions.CannotReadException;
 22  
 import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
 23  
 import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader;
 24  
 import org.jaudiotagger.tag.InvalidFrameException;
 25  
 import org.jaudiotagger.tag.flac.FlacTag;
 26  
 import org.jaudiotagger.tag.vorbiscomment.VorbisCommentReader;
 27  
 import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.RandomAccessFile;
 31  
 import java.util.ArrayList;
 32  
 import java.util.List;
 33  
 import java.util.logging.Logger;
 34  
 
 35  
 /**
 36  
  * Read Flac Tag
 37  
  */
 38  84
 public class FlacTagReader
 39  
 {
 40  
     // Logger Object
 41  42
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac");
 42  
 
 43  84
     private VorbisCommentReader vorbisCommentReader = new VorbisCommentReader();
 44  
 
 45  
 
 46  
     public FlacTag read(RandomAccessFile raf) throws CannotReadException, IOException
 47  
     {
 48  23
         FlacStreamReader flacStream = new FlacStreamReader(raf);
 49  23
         flacStream.findStream();
 50  
 
 51  
         //Hold the metadata
 52  23
         VorbisCommentTag tag = null;
 53  23
         List<MetadataBlockDataPicture> images = new ArrayList<MetadataBlockDataPicture>();
 54  
 
 55  
         //Seems like we have a valid stream
 56  23
         boolean isLastBlock = false;
 57  134
         while (!isLastBlock)
 58  
         {
 59  
             //Read the header
 60  111
             MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
 61  
 
 62  
             //Is it one containing some sort of metadata, therefore interested in it?
 63  5
             switch (mbh.getBlockType())
 64  
             {
 65  
                 //We got a vorbiscomment comment block, parse it
 66  
                 case VORBIS_COMMENT:
 67  21
                     byte[] commentHeaderRawPacket = new byte[mbh.getDataLength()];
 68  21
                     raf.read(commentHeaderRawPacket);
 69  21
                     tag = vorbisCommentReader.read(commentHeaderRawPacket, false);
 70  21
                     break;
 71  
 
 72  
                 case PICTURE:
 73  
                     try
 74  
                     {
 75  17
                         MetadataBlockDataPicture mbdp = new MetadataBlockDataPicture(mbh, raf);
 76  17
                         images.add(mbdp);
 77  
                     }
 78  0
                     catch (IOException ioe)
 79  
                     {
 80  0
                         logger.warning("Unable to read picture metablock, ignoring:" + ioe.getMessage());
 81  
                     }
 82  0
                     catch (InvalidFrameException ive)
 83  
                     {
 84  0
                         logger.warning("Unable to read picture metablock, ignoring" + ive.getMessage());
 85  17
                     }
 86  
 
 87  0
                     break;
 88  
 
 89  
                     //This is not a metadata block we are interested in so we skip to next block
 90  
                 default:
 91  73
                     raf.seek(raf.getFilePointer() + mbh.getDataLength());
 92  
                     break;
 93  
             }
 94  
 
 95  111
             isLastBlock = mbh.isLastBlock();
 96  111
             mbh = null;
 97  111
         }
 98  
 
 99  
         //Note there may not be either a tag or any images, no problem this is valid however to make it easier we
 100  
         //just initialize Flac with an empty VorbisTag
 101  23
         if (tag == null)
 102  
         {
 103  2
             tag = VorbisCommentTag.createNewTag();
 104  
         }
 105  23
         FlacTag flacTag = new FlacTag(tag, images);
 106  23
         return flacTag;
 107  
     }
 108  
 }
 109