Coverage Report - org.jaudiotagger.audio.AudioFile
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioFile
47%
33/69
25%
8/32
2.824
 
 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;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.RandomAccessFile;
 24  
 import java.util.logging.Logger;
 25  
 import java.util.ArrayList;
 26  
 
 27  
 import org.jaudiotagger.audio.exceptions.CannotWriteException;
 28  
 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
 29  
 import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
 30  
 import org.jaudiotagger.tag.asf.AsfTag;
 31  
 import org.jaudiotagger.audio.wav.WavTag;
 32  
 import org.jaudiotagger.audio.real.RealTag;
 33  
 import org.jaudiotagger.tag.Tag;
 34  
 import org.jaudiotagger.tag.mp4.Mp4Tag;
 35  
 import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
 36  
 import org.jaudiotagger.tag.flac.FlacTag;
 37  
 
 38  
 /**
 39  
  * <p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
 40  
  * <p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
 41  
  * <p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.</p>
 42  
  * <p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
 43  
  *
 44  
  * @author Raphael Slinckx
 45  
  * @version $Id: AudioFile.java 845 2009-11-13 14:10:57Z paultaylor $
 46  
  * @see AudioFileIO
 47  
  * @see Tag
 48  
  * @since v0.01
 49  
  */
 50  
 public class AudioFile
 51  
 {
 52  
     //Logger
 53  4
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
 54  
 
 55  
     /**
 56  
      * The physical file that this instance represents.
 57  
      */
 58  
     protected File file;
 59  
 
 60  
     /**
 61  
      * The Audio header info
 62  
      */
 63  
     protected AudioHeader audioHeader;
 64  
 
 65  
     /**
 66  
      * The tag
 67  
      */
 68  
     protected Tag tag;
 69  
 
 70  
     public AudioFile()
 71  2061
     {
 72  
 
 73  2061
     }
 74  
 
 75  
     /**
 76  
      * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
 77  
      * <p>Create the AudioFile representing file f, the encodingaudioHeaders and containing the tag</p>
 78  
      *
 79  
      * @param f           The file of the audiofile
 80  
      * @param audioHeader the encoding audioHeaders over this file
 81  
      * @param tag         the tag contained in this file or null if no tag exists
 82  
      */
 83  
     public AudioFile(File f, AudioHeader audioHeader, Tag tag)
 84  936
     {
 85  936
         this.file = f;
 86  936
         this.audioHeader = audioHeader;
 87  936
         this.tag = tag;
 88  936
     }
 89  
 
 90  
 
 91  
     /**
 92  
      * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
 93  
      * <p>Create the AudioFile representing file denoted by pathname s, the encodingaudioHeaders and containing the tag</p>
 94  
      *
 95  
      * @param s           The pathname of the audiofile
 96  
      * @param audioHeader the encoding audioHeaders over this file
 97  
      * @param tag         the tag contained in this file
 98  
      */
 99  
     public AudioFile(String s, AudioHeader audioHeader, Tag tag)
 100  0
     {
 101  0
         this.file = new File(s);
 102  0
         this.audioHeader = audioHeader;
 103  0
         this.tag = tag;
 104  0
     }
 105  
 
 106  
     /**
 107  
      * <p>Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the <code>AudioFileIO.write(this)</code> method.</p>
 108  
      *
 109  
      * @throws CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
 110  
      * @see AudioFileIO
 111  
      */
 112  
     public void commit() throws CannotWriteException
 113  
     {
 114  339
         AudioFileIO.write(this);
 115  338
     }
 116  
 
 117  
     /**
 118  
      * Set the file to store the info in
 119  
      *
 120  
      * @param file
 121  
      */
 122  
     public void setFile(File file)
 123  
     {
 124  0
         this.file = file;
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Retrieve the physical file
 129  
      *
 130  
      * @return
 131  
      */
 132  
     public File getFile()
 133  
     {
 134  5119
         return file;
 135  
     }
 136  
 
 137  
     public void setTag(Tag tag)
 138  
     {
 139  8
         this.tag = tag;
 140  8
     }
 141  
 
 142  
     /**
 143  
      * Return audio header
 144  
      * @return
 145  
      */
 146  
     public AudioHeader getAudioHeader()
 147  
     {
 148  1908
         return audioHeader;
 149  
     }
 150  
 
 151  
     /**
 152  
      * <p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like
 153  
      * artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do
 154  
      * not allow there to be no tag so in this case the reader would return an empty tag whereas for others such
 155  
      * as mp3 it is purely optional.
 156  
      *
 157  
      * @return Returns the tag contained in this AudioFile, or null if no tag exists.
 158  
      */
 159  
     public Tag getTag()
 160  
     {
 161  4531
         return tag;
 162  
     }
 163  
 
 164  
     /**
 165  
      * <p>Returns a multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.</p>
 166  
      *
 167  
      * @return A multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.
 168  
      *         TODO Maybe this can be changed ?
 169  
      */
 170  
     public String toString()
 171  
     {
 172  0
         return "AudioFile " + getFile().getAbsolutePath()
 173  
                 + "  --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";
 174  
     }
 175  
 
 176  
     /**
 177  
      * Checks the file is accessible with the correct permissions, otherwise exception occurs
 178  
      *
 179  
      * @param file
 180  
      * @param readOnly
 181  
      * @throws ReadOnlyFileException
 182  
      * @throws FileNotFoundException
 183  
      * @return
 184  
      */
 185  
     protected RandomAccessFile checkFilePermissions(File file, boolean readOnly) throws ReadOnlyFileException, FileNotFoundException
 186  
     {
 187  
         RandomAccessFile newFile;
 188  
 
 189  2061
         logger.info("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
 190  2061
         if (!file.exists())
 191  
         {
 192  0
             logger.severe("Unable to find:" + file.getPath());
 193  0
             throw new FileNotFoundException("Unable to find:" + file.getPath());
 194  
         }
 195  
 
 196  
         // Unless opened as readonly the file must be writable
 197  2061
         if (readOnly)
 198  
         {
 199  645
             newFile = new RandomAccessFile(file, "r");
 200  
         }
 201  
         else
 202  
         {
 203  1416
             if (!file.canWrite())
 204  
             {
 205  4
                 logger.severe("Unable to write:" + file.getPath());
 206  4
                 throw new ReadOnlyFileException("Unable to write to:" + file.getPath());
 207  
             }
 208  1412
             newFile = new RandomAccessFile(file, "rws");
 209  
         }
 210  2057
         return newFile;
 211  
     }
 212  
 
 213  
     /**
 214  
      * Optional debugging method
 215  
      *
 216  
      * @return
 217  
      */
 218  
     public String displayStructureAsXML()
 219  
     {
 220  0
         return "";
 221  
     }
 222  
 
 223  
     /**
 224  
      * Optional debugging method
 225  
      *
 226  
      * @return
 227  
      */
 228  
     public String displayStructureAsPlainText()
 229  
     {
 230  0
         return "";
 231  
     }
 232  
 
 233  
 
 234  
     /** Create Default Tag
 235  
      *
 236  
      * @return
 237  
      */
 238  
     //TODO might be better to instantiate classes such as Mp4File,FlacFile ecetera
 239  
     //TODO Generic tag is very misleading because soem of these formats cannot actually save the tag
 240  
     public Tag createDefaultTag()
 241  
     {
 242  0
         if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 243  
         {
 244  0
             return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
 245  
         }
 246  0
         else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 247  
         {
 248  0
             return VorbisCommentTag.createNewTag();
 249  
         }
 250  0
         else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 251  
         {
 252  0
             return new Mp4Tag();
 253  
         }
 254  0
         else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 255  
         {
 256  0
             return new Mp4Tag();
 257  
         }
 258  0
         else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 259  
         {
 260  0
             return new Mp4Tag();
 261  
         }
 262  0
         else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 263  
         {
 264  0
             return new AsfTag();
 265  
         }
 266  0
         else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 267  
         {
 268  0
             return new WavTag();
 269  
         }
 270  0
         else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 271  
         {
 272  0
             return new RealTag();
 273  
         }
 274  0
         else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 275  
         {
 276  0
             return new RealTag();
 277  
         }
 278  
         else
 279  
         {
 280  0
             throw new RuntimeException("Unable to create default tag for this file format");
 281  
         }
 282  
 
 283  
     }
 284  
 
 285  
     /**
 286  
      * Get the tag or if the file doesnt have one at all, create a default tag  and return
 287  
      *
 288  
      * @return
 289  
      */
 290  
     public Tag getTagOrCreateDefault()
 291  
     {
 292  0
         Tag tag = getTag();
 293  0
         if(tag==null)
 294  
         {
 295  0
             return createDefaultTag();
 296  
         }
 297  0
         return tag;
 298  
     }
 299  
 
 300  
      /**
 301  
      * Get the tag or if the file doesnt have one at all, create a default tag  and set it
 302  
      *
 303  
      * @return
 304  
      */
 305  
     public Tag getTagOrCreateAndSetDefault()
 306  
     {
 307  22
         Tag tag = getTag();
 308  22
         if(tag==null)
 309  
         {
 310  21
             tag = createDefaultTag();
 311  21
             setTag(tag);
 312  21
             return tag;
 313  
         }
 314  1
         return tag;
 315  
     }
 316  
 
 317  
     /**
 318  
      *
 319  
      * @param file
 320  
      * @return filename with audioformat seperator stripped of.
 321  
      */
 322  
     public static String getBaseFilename(File file)
 323  
     {
 324  1523
         int index=file.getName().toLowerCase().lastIndexOf(".");
 325  1523
         if(index>0)
 326  
         {
 327  1523
             return file.getName().substring(0,index);
 328  
         }
 329  0
         return file.getName();
 330  
     }
 331  
 }