Coverage Report - org.jaudiotagger.audio.AudioFileIO
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioFileIO
88%
54/61
70%
7/10
1.727
 
 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 org.jaudiotagger.audio.asf.AsfFileReader;
 22  
 import org.jaudiotagger.audio.asf.AsfFileWriter;
 23  
 import org.jaudiotagger.audio.exceptions.CannotReadException;
 24  
 import org.jaudiotagger.audio.exceptions.CannotWriteException;
 25  
 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
 26  
 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
 27  
 import org.jaudiotagger.audio.flac.FlacFileReader;
 28  
 import org.jaudiotagger.audio.flac.FlacFileWriter;
 29  
 import org.jaudiotagger.audio.generic.*;
 30  
 import org.jaudiotagger.audio.mp3.MP3FileReader;
 31  
 import org.jaudiotagger.audio.mp3.MP3FileWriter;
 32  
 import org.jaudiotagger.audio.mp4.Mp4FileReader;
 33  
 import org.jaudiotagger.audio.mp4.Mp4FileWriter;
 34  
 import org.jaudiotagger.audio.ogg.OggFileReader;
 35  
 import org.jaudiotagger.audio.ogg.OggFileWriter;
 36  
 import org.jaudiotagger.audio.real.RealFileReader;
 37  
 import org.jaudiotagger.audio.wav.WavFileReader;
 38  
 import org.jaudiotagger.audio.wav.WavFileWriter;
 39  
 import org.jaudiotagger.tag.TagException;
 40  
 
 41  
 import java.io.File;
 42  
 import java.io.IOException;
 43  
 import java.util.HashMap;
 44  
 import java.util.Iterator;
 45  
 import java.util.Map;
 46  
 import java.util.logging.Logger;
 47  
 
 48  
 /**
 49  
  * <p/>
 50  
  * The main entry point for the Tag Reading/Writing operations, this class will
 51  
  * select the appropriate reader/writer for the given file.
 52  
  * </p>
 53  
  * <p/>
 54  
  * It selects the appropriate reader/writer based on the file extension (case
 55  
  * ignored).
 56  
  * </p>
 57  
  * <p/>
 58  
  * Here is an simple example of use:
 59  
  * </p>
 60  
  * <p/>
 61  
  * <code>
 62  
  * AudioFile audioFile = AudioFileIO.read(new File("audiofile.mp3")); //Reads the given file.<br/>
 63  
  * int bitrate = audioFile.getBitrate(); //Retreives the bitrate of the file.<br/>
 64  
  * String artist = audioFile.getTag().getFirst(TagFieldKey.ARTIST); //Retreive the artist name.<br/>
 65  
  * audioFile.getTag().setGenre("Progressive Rock"); //Sets the genre to Prog. Rock, note the file on disk is still unmodified.<br/>
 66  
  * AudioFileIO.write(audioFile); //Write the modifications in the file on disk.
 67  
  * </code>
 68  
  * </p>
 69  
  * <p/>
 70  
  * You can also use the <code>commit()</code> method defined for
 71  
  * <code>AudioFile</code>s to achieve the same goal as
 72  
  * <code>AudioFileIO.write(File)</code>, like this:
 73  
  * </p>
 74  
  * <p/>
 75  
  * <code>
 76  
  * AudioFile audioFile = AudioFileIO.read(new File("audiofile.mp3"));<br/>
 77  
  * audioFile.getTag().setGenre("Progressive Rock");<br/>
 78  
  * audioFile.commit(); //Write the modifications in the file on disk.<br/>
 79  
  * </code>
 80  
  * </p>
 81  
  *
 82  
  * @author Raphael Slinckx
 83  
  * @version $Id: AudioFileIO.java 836 2009-11-12 15:44:07Z paultaylor $
 84  
  * @see AudioFile
 85  
  * @see org.jaudiotagger.tag.Tag
 86  
  * @since v0.01
 87  
  */
 88  
 public class AudioFileIO
 89  
 {
 90  
 
 91  
     //Logger
 92  4
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
 93  
 
 94  
     // !! Do not forget to also add new supported extensions to AudioFileFilter
 95  
     // !!
 96  
 
 97  
     /**
 98  
      * This field contains the default instance for static use.
 99  
      */
 100  
     private static AudioFileIO defaultInstance;
 101  
 
 102  
     /**
 103  
      * <p/>
 104  
      * Delete the tag, if any, contained in the given file.
 105  
      * </p>
 106  
      *
 107  
      * @param f The file where the tag will be deleted
 108  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 109  
      *                              wasn't recognized, or other IO error occured.
 110  
      * @throws org.jaudiotagger.audio.exceptions.CannotReadException
 111  
      */
 112  
     public static void delete(AudioFile f) throws CannotReadException, CannotWriteException
 113  
     {
 114  73
         getDefaultAudioFileIO().deleteTag(f);
 115  73
     }
 116  
 
 117  
     /**
 118  
      * This method returns the default isntance for static use.<br>
 119  
      *
 120  
      * @return The default instance.
 121  
      */
 122  
     public static AudioFileIO getDefaultAudioFileIO()
 123  
     {
 124  2033
         if (defaultInstance == null)
 125  
         {
 126  4
             defaultInstance = new AudioFileIO();
 127  
         }
 128  2033
         return defaultInstance;
 129  
     }
 130  
 
 131  
     /**
 132  
      * <p/>
 133  
      * Read the tag contained in the given file.
 134  
      * </p>
 135  
      *
 136  
      * @param f The file to read.
 137  
      * @return The AudioFile with the file tag and the file encoding infos.
 138  
      * @throws CannotReadException If the file could not be read, the extension wasn't
 139  
      *                             recognized, or an IO error occured during the read.
 140  
      * @throws org.jaudiotagger.tag.TagException
 141  
      * @throws org.jaudiotagger.audio.exceptions.ReadOnlyFileException
 142  
      * @throws java.io.IOException
 143  
      * @throws org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
 144  
      */
 145  
     public static AudioFile read(File f)
 146  
             throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
 147  
     {
 148  1593
         return getDefaultAudioFileIO().readFile(f);
 149  
     }
 150  
 
 151  
     /**
 152  
      * <p/>
 153  
      * Write the tag contained in the audiofile in the actual file on the disk.
 154  
      * </p>
 155  
      *
 156  
      * @param f The AudioFile to be written
 157  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 158  
      *                              wasn't recognized, or other IO error occured.
 159  
      */
 160  
     public static void write(AudioFile f) throws CannotWriteException
 161  
     {
 162  367
         getDefaultAudioFileIO().writeFile(f);
 163  366
     }
 164  
 
 165  
     /**
 166  
      * This member is used to broadcast modification events to registered
 167  
      */
 168  
     private final ModificationHandler modificationHandler;
 169  
 
 170  
     // These tables contains all the readers/writers associated with extension
 171  
     // as a key
 172  4
     private Map<String, AudioFileReader> readers = new HashMap<String, AudioFileReader>();
 173  4
     private Map<String, AudioFileWriter> writers = new HashMap<String, AudioFileWriter>();
 174  
 
 175  
 
 176  
     /**
 177  
      * Creates an instance.
 178  
      */
 179  
     public AudioFileIO()
 180  4
     {
 181  4
         this.modificationHandler = new ModificationHandler();
 182  4
         prepareReadersAndWriters();
 183  4
     }
 184  
 
 185  
     /**
 186  
      * Adds an listener for all file formats.
 187  
      *
 188  
      * @param listener listener
 189  
      */
 190  
     public void addAudioFileModificationListener(
 191  
             AudioFileModificationListener listener)
 192  
     {
 193  0
         this.modificationHandler.addAudioFileModificationListener(listener);
 194  0
     }
 195  
 
 196  
     /**
 197  
      * <p/>
 198  
      * Delete the tag, if any, contained in the given file.
 199  
      * </p>
 200  
      *
 201  
      * @param f The file where the tag will be deleted
 202  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 203  
      *                              wasn't recognized, or other IO error occured.
 204  
      * @throws org.jaudiotagger.audio.exceptions.CannotReadException
 205  
      */
 206  
     public void deleteTag(AudioFile f) throws CannotReadException, CannotWriteException
 207  
     {
 208  73
         String ext = Utils.getExtension(f.getFile());
 209  
 
 210  73
         Object afw = writers.get(ext);
 211  73
         if (afw == null)
 212  
         {
 213  0
             throw new CannotWriteException(
 214  
                     "No Deleter associated to this extension: " + ext);
 215  
         }
 216  
 
 217  73
         ((AudioFileWriter) afw).delete(f);
 218  73
     }
 219  
 
 220  
     /**
 221  
      * Creates the readers and writers.
 222  
      */
 223  
     private void prepareReadersAndWriters()
 224  
     {
 225  
 
 226  
         // Tag Readers
 227  4
         readers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileReader());
 228  4
         readers.put(SupportedFileFormat.FLAC.getFilesuffix(), new FlacFileReader());
 229  4
         readers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileReader());
 230  4
         readers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileReader());
 231  4
         readers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileReader());
 232  4
         readers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileReader());
 233  4
         readers.put(SupportedFileFormat.M4B.getFilesuffix(), new Mp4FileReader());
 234  4
         readers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileReader());
 235  4
         readers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileReader());
 236  4
         final RealFileReader realReader = new RealFileReader();
 237  4
         readers.put(SupportedFileFormat.RA.getFilesuffix(), realReader);
 238  4
         readers.put(SupportedFileFormat.RM.getFilesuffix(), realReader);
 239  
 
 240  
         // Tag Writers
 241  4
         writers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileWriter());
 242  4
         writers.put(SupportedFileFormat.FLAC.getFilesuffix(), new FlacFileWriter());
 243  4
         writers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileWriter());
 244  4
         writers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileWriter());
 245  4
         writers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileWriter());
 246  4
         writers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileWriter());
 247  4
         writers.put(SupportedFileFormat.M4B.getFilesuffix(), new Mp4FileWriter());                
 248  4
         writers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileWriter());
 249  4
         writers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileWriter());
 250  
 
 251  
         // Register modificationHandler
 252  4
         Iterator<AudioFileWriter> it = writers.values().iterator();
 253  4
         for (AudioFileWriter curr : writers.values())
 254  
         {
 255  36
             curr.setAudioFileModificationListener(this.modificationHandler);
 256  
         }
 257  4
     }
 258  
 
 259  
     /**
 260  
      * <p/>
 261  
      * Read the tag contained in the given file.
 262  
      * </p>
 263  
      *
 264  
      * @param f The file to read.
 265  
      * @return The AudioFile with the file tag and the file encoding infos.
 266  
      * @throws CannotReadException If the file could not be read, the extension wasn't
 267  
      *                             recognized, or an IO error occured during the read.
 268  
      * @throws org.jaudiotagger.tag.TagException
 269  
      * @throws org.jaudiotagger.audio.exceptions.ReadOnlyFileException
 270  
      * @throws java.io.IOException
 271  
      * @throws org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
 272  
      */
 273  
     public AudioFile readFile(File f)
 274  
             throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
 275  
     {
 276  1593
         String ext = Utils.getExtension(f);
 277  
 
 278  1593
         AudioFileReader afr = readers.get(ext);
 279  1593
         if (afr == null)
 280  
         {
 281  0
             throw new CannotReadException(
 282  
                     "No Reader associated to this extension: " + ext);
 283  
         }
 284  
 
 285  1593
         return afr.read(f);
 286  
     }
 287  
 
 288  
     /**
 289  
      * Removes an listener for all file formats.
 290  
      *
 291  
      * @param listener listener
 292  
      */
 293  
     public void removeAudioFileModificationListener(
 294  
             AudioFileModificationListener listener)
 295  
     {
 296  0
         this.modificationHandler.removeAudioFileModificationListener(listener);
 297  0
     }
 298  
 
 299  
     /**
 300  
      * <p/>
 301  
      * Write the tag contained in the audiofile in the actual file on the disk.
 302  
      * </p>
 303  
      *
 304  
      * @param f The AudioFile to be written
 305  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 306  
      *                              wasn't recognized, or other IO error occured.
 307  
      */
 308  
     public void writeFile(AudioFile f) throws CannotWriteException
 309  
     {
 310  367
         String ext = Utils.getExtension(f.getFile());
 311  
 
 312  367
         AudioFileWriter afw = writers.get(ext);
 313  367
         if (afw == null)
 314  
         {
 315  0
             throw new CannotWriteException("No Writer associated to this extension: " + ext);
 316  
         }
 317  
 
 318  367
         afw.write(f);
 319  366
     }
 320  
 }