1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.tags;
22 
23 import java.io.File;
24 import java.io.IOException;
25 
26 import net.sourceforge.atunes.utils.Logger;
27 
28 import org.jaudiotagger.audio.AudioFile;
29 import org.jaudiotagger.audio.AudioFileIO;
30 import org.jaudiotagger.audio.exceptions.CannotReadException;
31 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
32 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
33 import org.jaudiotagger.tag.TagException;
34 
35 /**
36  * Reads files with JAudiotagger returning an org.jaudiotagger.audio.AudioFile
37  * object
38  *
39  * @author alex
40  *
41  */
42 public class JAudiotaggerFileReader {
43 
44 	/**
45 	 * Reads file with jaudiotagger
46 	 *
47 	 * @param file
48 	 * @return audio file or null
49 	 */
getAudioFile(final File file)50 	public AudioFile getAudioFile(final File file) {
51 		AudioFile audioFile = null;
52 		try {
53 			audioFile = AudioFileIO.read(file);
54 		} catch (NegativeArraySizeException e) {
55 			// Reading wrong tags can happen this
56 			// java.lang.NegativeArraySizeException: null
57 			// org.jaudiotagger.tag.id3.ID3Compression.uncompress(ID3Compression.java:38)
58 			logExceptionReading(file, e);
59 		} catch (NumberFormatException e) {
60 			// With fields with very big numbers this exception can be thrown
61 			logExceptionReading(file, e);
62 		} catch (IllegalArgumentException e) {
63 			// Reading wrong tags can happen this
64 			// java.lang.IllegalArgumentException: null
65 			// java.nio.Buffer.position(Unknown Source)
66 			// org.jaudiotagger.tag.id3.ID3v23Frame.read(ID3v23Frame.java:460)
67 			logExceptionReading(file, e);
68 		} catch (RuntimeException e) {
69 			// Reading wrong tags can happen this
70 			// java.lang.RuntimeException:
71 			// org.jaudiotagger.tag.id3.framebody.FrameBodyEQUA.<init>(java.nio.ByteBuffer,
72 			// int)
73 			logExceptionReading(file, e);
74 		} catch (CannotReadException e) {
75 			logExceptionReading(file, e);
76 		} catch (IOException e) {
77 			logExceptionReading(file, e);
78 		} catch (TagException e) {
79 			logExceptionReading(file, e);
80 		} catch (ReadOnlyFileException e) {
81 			logExceptionReading(file, e);
82 		} catch (InvalidAudioFrameException e) {
83 			logExceptionReading(file, e);
84 		}
85 		return audioFile;
86 	}
87 
logExceptionReading(final File file, final Exception e)88 	private void logExceptionReading(final File file, final Exception e) {
89 		Logger.error(net.sourceforge.atunes.utils.FileUtils.getPath(file));
90 		Logger.error(e.getMessage());
91 	}
92 }
93