Source code: joeshmoe/mpeg/ID3Tagger.java
1 /*
2 * This source file is copyright (C) 1999 by joeshmoe.com
3 * It may be freely used for non-commercial / development purposes.
4 * Commericial use requires the payment of a nominal license fee.
5 * Contact license@joeshmoe.com to purchase a license for commericial use.
6 *
7 * It is requested that all users who modify this source code send their
8 * changes to joeshmoe.com
9 *
10 * Bugs reports / modifications can sent to bugs@joeshmoe.com.
11 * Modifications made by Tom Wadzinski <orca_twadzins@yahoo.com>
12 */
13
14 package joeshmoe.mpeg;
15
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20
21 /**
22 * Reads and sets ID3 tags in MPEG audio files. Currently only V1 ID3 tags
23 * are supported.
24 * @see ID3Tagger
25 **/
26
27 public class ID3Tagger {
28
29 /**
30 * reads an ID3 tag from a file. Returns null if no tag is present.
31 **/
32 public static ID3Tag readTag (File pFile)
33 throws IOException, FileNotFoundException
34 {
35 if (!pFile.exists())
36 throw new FileNotFoundException (pFile.getName());
37
38 RandomAccessFile f = new RandomAccessFile (pFile, "r");
39
40 if (f.length() < 128) {
41 f.close();
42 return null;
43 }
44
45 f.seek (f.length() - 128);
46
47 byte[] tagBytes = new byte[128];
48 f.read(tagBytes);
49
50 ID3Tag t = new ID3Tag();
51
52 // byte [] tagID = new byte [3];
53 // byte [] song = new byte [30];
54 // byte [] artist = new byte [30];
55 // byte [] album = new byte [30];
56 // byte [] year = new byte [4];
57 // byte [] comment = new byte [30];
58 // byte [] genre = new byte [1];
59
60
61 if ( ((char)tagBytes[0] == 'T') &&
62 ((char)tagBytes[1] == 'A') &&
63 ((char)tagBytes[2] == 'G') ) {
64 t.setSong(new String(tagBytes,3,30).trim());
65 t.setArtist(new String(tagBytes,33,30).trim());
66 t.setAlbum (new String(tagBytes,63,30).trim());
67 t.setYear (new String(tagBytes,93,4).trim());
68 t.setComment (new String(tagBytes,97,28).trim());
69 if (tagBytes[125] == 0) {
70 t.setTrack(tagBytes[126]);
71 }
72 else {
73 t.setTrack(0);
74 }
75
76 //setTrack (tagBytes[126]);
77 t.setGenreID ((int)tagBytes[127]);
78 //validTag = true;
79 }
80 else {
81 f.close();
82 return null;
83 //validTag = false;
84 }
85
86 // f.readFully (tagID);
87
88 // if (!(new String(tagID)).equals ("TAG")) { // no ID3 tag in file
89 // f.close();
90 // return null;
91 // }
92
93 // f.readFully(song);
94 // t.setSong(new String (song).trim());
95
96 // f.readFully(artist);
97 // t.setArtist (new String (artist).trim());
98
99 // f.readFully(album);
100 // t.setAlbum (new String (album).trim());
101
102 // f.readFully(year);
103 // t.setYear (new String (year).trim());
104
105 // f.readFully(comment);
106 // System.out.println("comment[28], comment[29]:" + comment[28] + "," + comment[29]);
107 // t.setComment (new String (comment).trim());
108
109 // f.readFully(genre);
110 // t.setGenreID ((int) genre[0]);
111
112 f.close();
113 return t;
114 }
115
116 /**
117 * write an ID3 tag to a file.
118 * Note: will write a tag to the specified file whether or not it is a
119 * valid MPEG file.
120 **/
121 public static boolean writeTag (File pFile, ID3Tag pTag)
122 throws IOException, FileNotFoundException
123 {
124 if (!pFile.exists())
125 throw new FileNotFoundException (pFile.getName());
126
127 RandomAccessFile f = new RandomAccessFile (pFile, "rw");
128
129 // see if the tag already exists
130 if (readTag (pFile) == null)
131 f.seek (f.length());
132 else
133 f.seek (f.length() - 128);
134
135 f.writeBytes ("TAG");
136
137 StringBuffer song;
138 if (pTag.getSong() != null)
139 song = new StringBuffer(pTag.getSong());
140 else
141 song = new StringBuffer();
142 song.setLength(30);
143 f.writeBytes (song.toString());
144
145 StringBuffer artist;
146 if (pTag.getArtist() != null)
147 artist = new StringBuffer (pTag.getArtist());
148 else
149 artist = new StringBuffer();
150 artist.setLength(30);
151 f.writeBytes (artist.toString());
152
153 StringBuffer album;
154 if (pTag.getAlbum() != null) {
155 album = new StringBuffer (pTag.getAlbum());
156 }
157 else
158 album = new StringBuffer();
159 album.setLength(30);
160 f.writeBytes (album.toString());
161
162 StringBuffer year;
163 if (pTag.getYear() != null)
164 year = new StringBuffer (pTag.getYear());
165 else
166 year = new StringBuffer();
167 year.setLength(4);
168 f.writeBytes (year.toString());
169
170 StringBuffer comment;
171 if (pTag.getComment() != null)
172 comment = new StringBuffer (pTag.getComment());
173 else
174 comment = new StringBuffer();
175 comment.setLength (30);
176 f.writeBytes (comment.toString());
177
178 f.writeByte (pTag.getGenreID());
179
180 f.close();
181
182 return true;
183 }
184 }