Coverage Report - org.jaudiotagger.audio.AudioFileIO
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioFileIO
89%
54/61
70%
7/10
0
 
 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().getFirstArtist(); //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,v 1.14 2008/10/28 10:26:24 paultaylor Exp $
 84  
  * @see AudioFile
 85  
  * @see org.jaudiotagger.tag.Tag
 86  
  * @since v0.01
 87  
  */
 88  
 public class AudioFileIO
 89  
 {
 90  
 
 91  
     //Logger
 92  42
     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  
      */
 111  
     public static void delete(AudioFile f) throws CannotReadException, CannotWriteException
 112  
     {
 113  17
         getDefaultAudioFileIO().deleteTag(f);
 114  17
     }
 115  
 
 116  
     /**
 117  
      * This method returns the default isntance for static use.<br>
 118  
      *
 119  
      * @return The default instance.
 120  
      */
 121  
     public static AudioFileIO getDefaultAudioFileIO()
 122  
     {
 123  465
         if (defaultInstance == null)
 124  
         {
 125  42
             defaultInstance = new AudioFileIO();
 126  
         }
 127  465
         return defaultInstance;
 128  
     }
 129  
 
 130  
     /**
 131  
      * <p/>
 132  
      * Read the tag contained in the given file.
 133  
      * </p>
 134  
      *
 135  
      * @param f The file to read.
 136  
      * @return The AudioFile with the file tag and the file encoding infos.
 137  
      * @throws CannotReadException If the file could not be read, the extension wasn't
 138  
      *                             recognized, or an IO error occured during the read.
 139  
      */
 140  
     public static AudioFile read(File f)
 141  
             throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
 142  
     {
 143  356
         return getDefaultAudioFileIO().readFile(f);
 144  
     }
 145  
 
 146  
     /**
 147  
      * <p/>
 148  
      * Write the tag contained in the audiofile in the actual file on the disk.
 149  
      * </p>
 150  
      *
 151  
      * @param f The AudioFile to be written
 152  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 153  
      *                              wasn't recognized, or other IO error occured.
 154  
      */
 155  
     public static void write(AudioFile f) throws CannotWriteException
 156  
     {
 157  92
         getDefaultAudioFileIO().writeFile(f);
 158  92
     }
 159  
 
 160  
     /**
 161  
      * This member is used to broadcast modification events to registered
 162  
      */
 163  
     private final ModificationHandler modificationHandler;
 164  
 
 165  
     // These tables contains all the readers/writers associated with extension
 166  
     // as a key
 167  42
     private Map<String, AudioFileReader> readers = new HashMap<String, AudioFileReader>();
 168  42
     private Map<String, AudioFileWriter> writers = new HashMap<String, AudioFileWriter>();
 169  
 
 170  
 
 171  
     /**
 172  
      * Creates an instance.
 173  
      */
 174  
     public AudioFileIO()
 175  42
     {
 176  42
         this.modificationHandler = new ModificationHandler();
 177  42
         prepareReadersAndWriters();
 178  42
     }
 179  
 
 180  
     /**
 181  
      * Adds an listener for all file formats.
 182  
      *
 183  
      * @param listener listener
 184  
      */
 185  
     public void addAudioFileModificationListener(
 186  
             AudioFileModificationListener listener)
 187  
     {
 188  0
         this.modificationHandler.addAudioFileModificationListener(listener);
 189  0
     }
 190  
 
 191  
     /**
 192  
      * <p/>
 193  
      * Delete the tag, if any, contained in the given file.
 194  
      * </p>
 195  
      *
 196  
      * @param f The file where the tag will be deleted
 197  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 198  
      *                              wasn't recognized, or other IO error occured.
 199  
      */
 200  
     public void deleteTag(AudioFile f) throws CannotReadException, CannotWriteException
 201  
     {
 202  17
         String ext = Utils.getExtension(f.getFile());
 203  
 
 204  17
         Object afw = writers.get(ext);
 205  17
         if (afw == null)
 206  
         {
 207  0
             throw new CannotWriteException(
 208  
                     "No Deleter associated to this extension: " + ext);
 209  
         }
 210  
 
 211  17
         ((AudioFileWriter) afw).delete(f);
 212  17
     }
 213  
 
 214  
     /**
 215  
      * Creates the readers and writers.
 216  
      */
 217  
     private void prepareReadersAndWriters()
 218  
     {
 219  
 
 220  
         // Tag Readers
 221  42
         readers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileReader());
 222  42
         readers.put(SupportedFileFormat.FLAC.getFilesuffix(), new FlacFileReader());
 223  42
         readers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileReader());
 224  42
         readers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileReader());
 225  42
         readers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileReader());
 226  42
         readers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileReader());
 227  42
         readers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileReader());
 228  42
         readers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileReader());
 229  42
         final RealFileReader realReader = new RealFileReader();
 230  42
         readers.put(SupportedFileFormat.RA.getFilesuffix(), realReader);
 231  42
         readers.put(SupportedFileFormat.RM.getFilesuffix(), realReader);
 232  
 
 233  
         // Tag Writers
 234  42
         writers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileWriter());
 235  42
         writers.put(SupportedFileFormat.FLAC.getFilesuffix(), new FlacFileWriter());
 236  42
         writers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileWriter());
 237  42
         writers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileWriter());
 238  42
         writers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileWriter());
 239  42
         writers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileWriter());
 240  42
         writers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileWriter());
 241  42
         writers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileWriter());
 242  
 
 243  
         // Register modificationHandler
 244  42
         Iterator<AudioFileWriter> it = writers.values().iterator();
 245  378
         while (it.hasNext())
 246  
         {
 247  336
             AudioFileWriter curr = it.next();
 248  336
             curr.setAudioFileModificationListener(this.modificationHandler);
 249  336
         }
 250  42
     }
 251  
 
 252  
     /**
 253  
      * <p/>
 254  
      * Read the tag contained in the given file.
 255  
      * </p>
 256  
      *
 257  
      * @param f The file to read.
 258  
      * @return The AudioFile with the file tag and the file encoding infos.
 259  
      * @throws CannotReadException If the file could not be read, the extension wasn't
 260  
      *                             recognized, or an IO error occured during the read.
 261  
      */
 262  
     public AudioFile readFile(File f)
 263  
             throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
 264  
     {
 265  356
         String ext = Utils.getExtension(f);
 266  
 
 267  356
         AudioFileReader afr = readers.get(ext);
 268  356
         if (afr == null)
 269  
         {
 270  0
             throw new CannotReadException(
 271  
                     "No Reader associated to this extension: " + ext);
 272  
         }
 273  
 
 274  356
         return afr.read(f);
 275  
     }
 276  
 
 277  
     /**
 278  
      * Removes an listener for all file formats.
 279  
      *
 280  
      * @param listener listener
 281  
      */
 282  
     public void removeAudioFileModificationListener(
 283  
             AudioFileModificationListener listener)
 284  
     {
 285  0
         this.modificationHandler.removeAudioFileModificationListener(listener);
 286  0
     }
 287  
 
 288  
     /**
 289  
      * <p/>
 290  
      * Write the tag contained in the audiofile in the actual file on the disk.
 291  
      * </p>
 292  
      *
 293  
      * @param f The AudioFile to be written
 294  
      * @throws CannotWriteException If the file could not be written/accessed, the extension
 295  
      *                              wasn't recognized, or other IO error occured.
 296  
      */
 297  
     public void writeFile(AudioFile f) throws CannotWriteException
 298  
     {
 299  92
         String ext = Utils.getExtension(f.getFile());
 300  
 
 301  92
         AudioFileWriter afw = writers.get(ext);
 302  92
         if (afw == null)
 303  
         {
 304  0
             throw new CannotWriteException("No Writer associated to this extension: " + ext);
 305  
         }
 306  
 
 307  92
         afw.write(f);
 308  92
     }
 309  
 }