Coverage Report - org.jaudiotagger.audio.flac.FlacInfoReader
 
Classes in this File Line Coverage Branch Coverage Complexity
FlacInfoReader
95%
41/43
80%
8/10
3.333
 
 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.BlockType;
 23  
 import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataStreamInfo;
 24  
 import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader;
 25  
 import org.jaudiotagger.audio.generic.GenericAudioHeader;
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 import java.io.RandomAccessFile;
 30  
 import java.util.logging.Logger;
 31  
 
 32  
 /**
 33  
  * Read info from Flac file
 34  
  */
 35  52
 public class FlacInfoReader
 36  
 {
 37  
     // Logger Object
 38  42
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac");
 39  
 
 40  
     private static final int NO_OF_BITS_IN_BYTE = 8;
 41  
     private static final int KILOBYTES_TO_BYTES_MULTIPLIER = 1000;
 42  
 
 43  
     public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException
 44  
     {
 45  24
         FlacStreamReader flacStream = new FlacStreamReader(raf);
 46  24
         flacStream.findStream();
 47  
 
 48  23
         MetadataBlockDataStreamInfo mbdsi = null;
 49  23
         boolean isLastBlock = false;
 50  
 
 51  
         //Search for StreamInfo Block, but even after we found it we still have to continue through all
 52  
         //the metadata blocks so that we can find the start of the audio frames which we need to calculate
 53  
         //the bitrate
 54  134
         while (!isLastBlock)
 55  
         {
 56  111
             MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
 57  111
             if (mbh.getBlockType() == BlockType.STREAMINFO)
 58  
             {
 59  23
                 mbdsi = new MetadataBlockDataStreamInfo(mbh, raf);
 60  23
                 if (!mbdsi.isValid())
 61  
                 {
 62  0
                     throw new CannotReadException("FLAC StreamInfo not valid");
 63  
                 }
 64  
             }
 65  
             else
 66  
             {
 67  88
                 raf.seek(raf.getFilePointer() + mbh.getDataLength());
 68  
             }
 69  
 
 70  111
             isLastBlock = mbh.isLastBlock();
 71  111
             mbh = null; //Free memory
 72  111
         }
 73  
 
 74  23
         if (mbdsi == null)
 75  
         {
 76  0
             throw new CannotReadException("Unable to find Flac StreamInfo");
 77  
         }
 78  
 
 79  23
         GenericAudioHeader info = new GenericAudioHeader();
 80  23
         info.setLength(mbdsi.getLength());
 81  23
         info.setPreciseLength(mbdsi.getPreciseLength());
 82  23
         info.setChannelNumber(mbdsi.getChannelNumber());
 83  23
         info.setSamplingRate(mbdsi.getSamplingRate());
 84  23
         info.setEncodingType(mbdsi.getEncodingType());
 85  23
         info.setExtraEncodingInfos("");
 86  23
         info.setBitrate(computeBitrate(mbdsi.getPreciseLength(), raf.length() - raf.getFilePointer()));
 87  23
         return info;
 88  
     }
 89  
 
 90  
     private int computeBitrate(float length, long size)
 91  
     {
 92  23
         return (int) ((size / KILOBYTES_TO_BYTES_MULTIPLIER) * NO_OF_BITS_IN_BYTE / length);
 93  
     }
 94  
 
 95  
     /**
 96  
      * Count the number of metadatablocks, useful for debugging
 97  
      *
 98  
      * @param f
 99  
      * @return
 100  
      * @throws CannotReadException
 101  
      * @throws IOException
 102  
      */
 103  
     public int countMetaBlocks(File f) throws CannotReadException, IOException
 104  
     {
 105  12
         RandomAccessFile raf = new RandomAccessFile(f, "r");
 106  12
         FlacStreamReader flacStream = new FlacStreamReader(raf);
 107  12
         flacStream.findStream();
 108  
 
 109  
 
 110  12
         boolean isLastBlock = false;
 111  
 
 112  12
         int count = 0;
 113  68
         while (!isLastBlock)
 114  
         {
 115  56
             MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
 116  56
             logger.info("Found block:" + mbh.getBlockType());
 117  56
             raf.seek(raf.getFilePointer() + mbh.getDataLength());
 118  56
             isLastBlock = mbh.isLastBlock();
 119  56
             mbh = null; //Free memory
 120  56
             count++;
 121  56
         }
 122  12
         raf.close();
 123  12
         return count;
 124  
     }
 125  
 }