| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public class AudioFileIO |
| 89 | |
{ |
| 90 | |
|
| 91 | |
|
| 92 | 42 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio"); |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
private static AudioFileIO defaultInstance; |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public static void delete(AudioFile f) throws CannotReadException, CannotWriteException |
| 112 | |
{ |
| 113 | 17 | getDefaultAudioFileIO().deleteTag(f); |
| 114 | 17 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 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 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 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 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public static void write(AudioFile f) throws CannotWriteException |
| 156 | |
{ |
| 157 | 92 | getDefaultAudioFileIO().writeFile(f); |
| 158 | 92 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private final ModificationHandler modificationHandler; |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 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 | |
|
| 173 | |
|
| 174 | |
public AudioFileIO() |
| 175 | 42 | { |
| 176 | 42 | this.modificationHandler = new ModificationHandler(); |
| 177 | 42 | prepareReadersAndWriters(); |
| 178 | 42 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public void addAudioFileModificationListener( |
| 186 | |
AudioFileModificationListener listener) |
| 187 | |
{ |
| 188 | 0 | this.modificationHandler.addAudioFileModificationListener(listener); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 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 | |
|
| 216 | |
|
| 217 | |
private void prepareReadersAndWriters() |
| 218 | |
{ |
| 219 | |
|
| 220 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 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 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
public void removeAudioFileModificationListener( |
| 283 | |
AudioFileModificationListener listener) |
| 284 | |
{ |
| 285 | 0 | this.modificationHandler.removeAudioFileModificationListener(listener); |
| 286 | 0 | } |
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 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 | |
} |