Coverage Report - org.jaudiotagger.audio.generic.AudioFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioFileReader
83%
19/23
50%
3/6
4.667
 
 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,v 1.8 2008/11/21 10:32:48 paultaylor Exp $
 42  
  *@since        v0.02
 43  
  */
 44  
 
 45  392
 public abstract class AudioFileReader
 46  
 {
 47  
 
 48  
     // Logger Object
 49  42
       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  201
         if (!f.canRead())
 84  
         {
 85  0
             throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
 86  
         }
 87  
 
 88  201
         if (f.length() <= MINIMUM_SIZE_FOR_VALID_AUDIO_FILE)
 89  
         {
 90  0
             throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
 91  
         }
 92  
 
 93  201
         RandomAccessFile raf = null;
 94  
         try
 95  
         {
 96  201
             raf = new RandomAccessFile(f, "r");
 97  201
             raf.seek(0);
 98  
 
 99  201
             GenericAudioHeader info = getEncodingInfo(raf);
 100  196
             raf.seek(0);
 101  196
             Tag tag = getTag(raf);
 102  195
             return new AudioFile(f, info, tag);
 103  
 
 104  
         }
 105  4
         catch (CannotReadException cre)
 106  
         {
 107  4
             throw cre;
 108  
         }
 109  2
         catch (Exception e)
 110  
         {
 111  
             //TODO is this masking exceptions, i.e NullBoxIDException get converted to CannotReadException
 112  2
             throw new CannotReadException(f.getAbsolutePath()+":" + e.getMessage(), e);
 113  
         }
 114  
         finally
 115  
         {
 116  6
             try
 117  
             {
 118  201
                 if (raf != null)
 119  
                 {
 120  201
                     raf.close();
 121  
                 }
 122  
             }
 123  0
             catch (Exception ex)
 124  
             {
 125  0
                 logger.log(Level.WARNING, ErrorMessage.GENERAL_READ_FAILED_UNABLE_TO_CLOSE_RANDOM_ACCESS_FILE.getMsg(f.getAbsolutePath()));
 126  402
             }
 127  
         }
 128  
     }
 129  
 }