001 package helliker.id3;
002
003 import java.io.RandomAccessFile;
004 import java.io.IOException;
005 import java.io.FileNotFoundException;
006 import java.io.File;
007 import java.io.UnsupportedEncodingException;
008 import java.util.logging.Logger;
009
010 /*
011 Copyright (C) 2001 Jonathan Hilliker
012 This library is free software; you can redistribute it and/or
013 modify it under the terms of the GNU Lesser General Public
014 License as published by the Free Software Foundation; either
015 version 2.1 of the License, or (at your option) any later version.
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020 You should have received a copy of the GNU Lesser General Public
021 License along with this library; if not, write to the Free Software
022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023 */
024 /**
025 * This class reads all the information in the header of an id3v2 tag.
026 */
027
028 public class ID3v2Header {
029 private static final Logger logger = Logger.getLogger(ID3v2Header.class.getPackage().getName());
030
031 /**
032 * Start String
033 */
034 private static final String TAG_START = "ID3";
035 /**
036 * ???
037 */
038 private static final int HEAD_SIZE = 10;
039 /**
040 * ???
041 */
042 private static final int HEAD_LOCATION = 0;
043 /**
044 * ???
045 */
046 private static final int NEW_MAJOR_VERSION = 3;// So winamp will accept it use
047 /**
048 * ???
049 */
050 private static final int NEW_MINOR_VERSION = 0;// 3 instead of 4
051
052 /**
053 * The File
054 */
055 private File mp3 = null;
056 /**
057 * ???
058 */
059 private boolean headerExists;
060 /**
061 * ???
062 */
063 private int majorVersion;
064 /**
065 * ???
066 */
067 private int minorVersion;
068 /**
069 * ???
070 */
071 private boolean unsynchronisation;
072 /**
073 * ???
074 */
075 private boolean extended;
076 /**
077 * ???
078 */
079 private boolean experimental;
080 /**
081 * ???
082 */
083 private boolean footer;
084 /**
085 * ???
086 */
087 private int tagSize;
088
089
090 /**
091 * Create an id3v2header linked to the file passed as a parameter. An attempt
092 * will be made to read the header from the file. If a header exists, then
093 * information in the header will be extracted. If a header doesn't exist,
094 * default data will be used.
095 *
096 *@param mp3 the file to attempt to read data from
097 *@exception FileNotFoundException if an error occurs
098 *@exception IOException if an error occurs
099 */
100 public ID3v2Header(File mp3) throws IOException {
101 this.mp3 = mp3;
102
103 majorVersion = NEW_MAJOR_VERSION;
104 minorVersion = NEW_MINOR_VERSION;
105 unsynchronisation = false;
106 extended = false;
107 experimental = false;
108 footer = false;
109 tagSize = 0;
110
111 RandomAccessFile in = null;
112
113 try {
114 in = new RandomAccessFile(mp3, "r");
115 headerExists = checkHeader(in);
116
117 if (headerExists) {
118 readHeader(in);
119 }
120 } finally {
121 if (in != null) {
122 in.close();
123 }
124 }
125 }
126
127
128 /**
129 * Checks to see if there is an id3v2 header in the file provided to the
130 * constructor.
131 *
132 *@param raf the open file to read from
133 *@return true if an id3v2 header exists in the file
134 *@exception FileNotFoundException if an error occurs
135 *@exception IOException if an error occurs
136 */
137 private boolean checkHeader(RandomAccessFile raf)
138 throws IOException {
139
140 boolean exists = false;
141 raf.seek(HEAD_LOCATION);
142 byte[] buf = new byte[HEAD_SIZE];
143
144 if (raf.read(buf) != HEAD_SIZE) {
145 throw new IOException("Error encountered finding id3v2 header");
146 }
147
148 String result = new String(buf);
149 if (result.substring(0, TAG_START.length()).equals(TAG_START)) {
150 if ((buf[3] < 0xff) && (buf[4] < 0xff)) {
151 if ((buf[6] < 0x80) && (buf[7] < 0x80)
152 && (buf[8] < 0x80) && (buf[9] < 0x80)) {
153
154 exists = true;
155 }
156 }
157 }
158
159 return exists;
160 }
161
162
163 /**
164 * Extracts the information from the header.
165 *
166 *@param raf the open file to read from
167 *@exception FileNotFoundException if an error occurs
168 *@exception IOException if an error occurs
169 */
170 private void readHeader(RandomAccessFile raf)
171 throws IOException {
172
173 raf.seek(HEAD_LOCATION);
174 byte[] head = new byte[HEAD_SIZE];
175
176 if (raf.read(head) != HEAD_SIZE) {
177 throw new IOException("Error encountered reading id3v2 header");
178 }
179
180 majorVersion = (int) head[3];
181
182 //we tag our own new tags with ID3v2.3 but we read and write tags up to version 4
183 if (majorVersion <= 4){//NEW_MAJOR_VERSION) {
184 minorVersion = (int) head[4];
185 unsynchronisation = BinaryParser.bitSet(head[5], 7);
186 extended = BinaryParser.bitSet(head[5], 6);
187 experimental = BinaryParser.bitSet(head[5], 5);
188 footer = BinaryParser.bitSet(head[5], 4);
189
190 byte[] size = {head[6], head[7], head[8], head[9]};
191 tagSize = BinaryParser.convertToSynchsafeInt(size);
192 } else {
193 logger.severe("Cannot handle version " + majorVersion + " headers.");
194 throw new IOException("Cannot handle version " + majorVersion + " headers.");
195 }
196 }
197
198
199 /**
200 * Return an array of bytes representing the header. This can be used to
201 * easily write the header to a file. When this method is called it
202 * automatically updates the header to the newest format.
203 *
204 *@return a binary representation of this header
205 */
206 public byte[] getBytes() {
207 byte[] b = new byte[HEAD_SIZE];
208 int bytesCopied = 0;
209
210 if (majorVersion < NEW_MAJOR_VERSION) {
211 majorVersion = NEW_MAJOR_VERSION;
212 }
213
214 System.arraycopy(TAG_START.getBytes(), 0, b, bytesCopied,
215 TAG_START.length());
216 bytesCopied += TAG_START.length();
217 b[bytesCopied++] = (byte) majorVersion;
218 b[bytesCopied++] = (byte) minorVersion;
219 b[bytesCopied++] = getFlagByte();
220 System.arraycopy(BinaryParser.convertToSynchsafeBytes(tagSize),
221 0, b, bytesCopied, 4);
222 bytesCopied += 4;
223
224 return b;
225 }
226
227
228 /**
229 * A helper function for the getBytes function that returns a byte with the
230 * proper flags set.
231 *
232 *@return the flags byte of this header
233 */
234 private byte getFlagByte() {
235 byte ret = 0;
236
237 if (unsynchronisation) {
238 ret = BinaryParser.setBit(ret, 7);
239 }
240 if (extended) {
241 ret = BinaryParser.setBit(ret, 6);
242 }
243 if (experimental) {
244 ret = BinaryParser.setBit(ret, 5);
245 }
246 if (footer) {
247 ret = BinaryParser.setBit(ret, 4);
248 }
249
250 return ret;
251 }
252
253
254 /**
255 * Returns true if a header exists
256 *
257 *@return true if a header exists
258 */
259 public boolean headerExists() {
260 return headerExists;
261 }
262
263
264 /**
265 * Returns the size (in bytes) of this header. This is always 10.
266 *
267 *@return the size of this header
268 */
269 public int getHeaderSize() {
270 return HEAD_SIZE;
271 }
272
273
274 /**
275 * Returns the size (in bytes) of the frames and extended header portion
276 * and padding of the id3v2 tag according to the size field in the header.
277 *
278 *@return the size field of the header
279 */
280 public int getTagSize() {
281 return tagSize;
282 }
283
284
285 /**
286 * Sets the size of the frames and/or extended header and padding. If this function is
287 * called, the headerExists function will return true. This is called every
288 * time a frame is updated, added, or removed.
289 *
290 *@param size a value of type 'int'
291 */
292 public void setTagSize(int size) {
293 if (size > 0) {
294 tagSize = size;
295 headerExists = true;
296 }
297 }
298
299
300 /**
301 * Returns the major version of this id3v2 tag.
302 *
303 *@return the major version of this id3v2 tag.
304 */
305 public int getMajorVersion() {
306 return majorVersion;
307 }
308
309
310 /**
311 * Return the minor version/revision of this id3v2 tag.
312 *
313 *@return the minor version/revision of this id3v2 tag.
314 */
315 public int getMinorVersion() {
316 return minorVersion;
317 }
318
319
320 /**
321 * Returns true if the unsynchronisation bit is set in this header.
322 *
323 *@return true if the unsynchronisation bit is set in this header.
324 */
325 public boolean getUnsynchronisation() {
326 return unsynchronisation;
327 }
328
329
330 /**
331 * Set the unsynchronisation flag for this header.
332 *
333 *@param unsynch the new value of the unsynchronisation flag
334 */
335 public void setUnsynchronisation(boolean unsynch) {
336 unsynchronisation = unsynch;
337 }
338
339
340 /**
341 * Returns true if this tag has an extended header.
342 *
343 *@return true if this tag has an extended header
344 */
345 public boolean getExtendedHeader() {
346 return extended;
347 }
348
349
350 /**
351 * Set the value of the extended header bit of this header.
352 *
353 *@param extend the new value of the extended header bit
354 */
355 public void setExtendedHeader(boolean extend) {
356 extended = extend;
357 }
358
359
360 /**
361 * Returns true if the experimental bit of this header is set.
362 *
363 *@return true if the experimental bit of this header is set
364 */
365 public boolean getExperimental() {
366 return experimental;
367 }
368
369
370 /**
371 * Set the value of the experimental bit of this header.
372 *
373 *@param experiment the new value of the experimental bit
374 */
375 public void setExperimental(boolean experiment) {
376 experimental = experiment;
377 }
378
379
380 /**
381 * Returns true if this tag has a footer.
382 *
383 *@return true if this tag has a footer
384 */
385 public boolean getFooter() {
386 return footer;
387 }
388
389
390 /**
391 * Sets the value of the footer bit for this header.
392 *
393 *@param foot the new value of the footer bit for this header
394 */
395 public void setFooter(boolean foot) {
396 footer = foot;
397 }
398
399
400 /**
401 * Return a string representation of this object. Contains all information
402 * contained within.
403 *
404 *@return a string representation of this object
405 */
406 public String toString() {
407 return "ID3v2." + getMajorVersion() + "." + getMinorVersion() + "\n"
408 + "TagSize:\t\t\t" + getTagSize()
409 + " bytes\nUnsynchronisation:\t\t" + getUnsynchronisation()
410 + "\nExtended Header:\t\t" + getExtendedHeader()
411 + "\nExperimental:\t\t\t" + getExperimental()
412 + "\nFooter:\t\t\t\t" + getFooter();
413 }
414
415 }
416