Coverage Report - org.jaudiotagger.audio.generic.AudioFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioFileReader
84%
21/25
50%
4/8
5
 
 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.generic;
 20  
 
 21  
 import org.jaudiotagger.audio.AudioFile;
 22  
 import org.jaudiotagger.audio.exceptions.CannotReadException;
 23  
 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
 24  
 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
 25  
 import org.jaudiotagger.tag.Tag;
 26  
 import org.jaudiotagger.tag.TagException;
 27  
 import org.jaudiotagger.logging.ErrorMessage;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.IOException;
 31  
 import java.io.RandomAccessFile;
 32  
 import java.util.logging.Logger;
 33  
 import java.util.logging.Level;
 34  
 
 35  
 /*
 36  
  * This abstract class is the skeleton for tag readers. It handles the creation/closing of
 37  
  * the randomaccessfile objects and then call the subclass method getEncodingInfo and getTag.
 38  
  * These two method have to be implemented in the subclass.
 39  
  * 
 40  
  *@author        Raphael Slinckx
 41  
  *@version        $Id: AudioFileReader.java 813 2009-09-03 09:23:25Z paultaylor $
 42  
  *@since        v0.02
 43  
  */
 44  
 
 45  85
 public abstract class AudioFileReader
 46  
 {
 47  
 
 48  
     // Logger Object
 49  4
       public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.generic");
 50  
     private static final int MINIMUM_SIZE_FOR_VALID_AUDIO_FILE = 150;
 51  
 
 52  
     /*
 53  
     * Returns the encoding info object associated wih the current File.
 54  
     * The subclass can assume the RAF pointer is at the first byte of the file.
 55  
     * The RandomAccessFile must be kept open after this function, but can point
 56  
     * at any offset in the file.
 57  
     *
 58  
     * @param raf The RandomAccessFile associtaed with the current file
 59  
     * @exception IOException is thrown when the RandomAccessFile operations throw it (you should never throw them manually)
 60  
     * @exception CannotReadException when an error occured during the parsing of the encoding infos
 61  
     */
 62  
     protected abstract GenericAudioHeader getEncodingInfo(RandomAccessFile raf) throws CannotReadException, IOException;
 63  
 
 64  
     /*
 65  
       * Same as above but returns the Tag contained in the file, or a new one.
 66  
       *
 67  
       * @param raf The RandomAccessFile associted with the current file
 68  
       * @exception IOException is thrown when the RandomAccessFile operations throw it (you should never throw them manually)
 69  
       * @exception CannotReadException when an error occured during the parsing of the tag
 70  
       */
 71  
     protected abstract Tag getTag(RandomAccessFile raf) throws CannotReadException, IOException;
 72  
 
 73  
     /*
 74  
       * Reads the given file, and return an AudioFile object containing the Tag
 75  
       * and the encoding infos present in the file. If the file has no tag, an
 76  
       * empty one is returned. If the encodinginfo is not valid , an exception is thrown.
 77  
       *
 78  
       * @param f The file to read
 79  
       * @exception CannotReadException If anything went bad during the read of this file
 80  
       */
 81  
     public AudioFile read(File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
 82  
     {
 83  752
         if(logger.isLoggable(Level.INFO))
 84  
         {
 85  752
             logger.info(ErrorMessage.GENERAL_READ.getMsg(f.getAbsolutePath()));   
 86  
         }
 87  
 
 88  752
         if (!f.canRead())
 89  
         {
 90  0
             throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
 91  
         }
 92  
 
 93  752
         if (f.length() <= MINIMUM_SIZE_FOR_VALID_AUDIO_FILE)
 94  
         {
 95  0
             throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
 96  
         }
 97  
 
 98  752
         RandomAccessFile raf = null;
 99  
         try
 100  
         {
 101  752
             raf = new RandomAccessFile(f, "r");
 102  752
             raf.seek(0);
 103  
 
 104  752
             GenericAudioHeader info = getEncodingInfo(raf);
 105  739
             raf.seek(0);
 106  739
             Tag tag = getTag(raf);
 107  738
             return new AudioFile(f, info, tag);
 108  
 
 109  
         }
 110  13
         catch (CannotReadException cre)
 111  
         {
 112  13
             throw cre;
 113  
         }
 114  1
         catch (Exception e)
 115  
         {
 116  
             //TODO is this masking exceptions, i.e NullBoxIDException get converted to CannotReadException
 117  1
             throw new CannotReadException(f.getAbsolutePath()+":" + e.getMessage(), e);
 118  
         }
 119  
         finally
 120  
         {
 121  14
             try
 122  
             {
 123  752
                 if (raf != null)
 124  
                 {
 125  752
                     raf.close();
 126  
                 }
 127  
             }
 128  0
             catch (Exception ex)
 129  
             {
 130  0
                 logger.log(Level.WARNING, ErrorMessage.GENERAL_READ_FAILED_UNABLE_TO_CLOSE_RANDOM_ACCESS_FILE.getMsg(f.getAbsolutePath()));
 131  1504
             }
 132  
         }
 133  
     }
 134  
 }