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
008 /*
009 Copyright (C) 2001 Jonathan Hilliker
010 This library is free software; you can redistribute it and/or
011 modify it under the terms of the GNU Lesser General Public
012 License as published by the Free Software Foundation; either
013 version 2.1 of the License, or (at your option) any later version.
014 This library is distributed in the hope that it will be useful,
015 but WITHOUT ANY WARRANTY; without even the implied warranty of
016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 Lesser General Public License for more details.
018 You should have received a copy of the GNU Lesser General Public
019 License along with this library; if not, write to the Free Software
020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021 */
022 /**
023 * If the id3v2 tag has an extended header, this class will read/write the
024 * information contained within it. NOTE: this class is untested and has no
025 * mutators. In other words, this class will only be used if an mp3 already has
026 * an extended header (at this point at least).
027 */
028
029 public class ID3v2ExtendedHeader {
030
031 /**
032 * The position in the File
033 */
034 private static final int EXT_HEAD_LOCATION = 10;
035 /**
036 * Minimum Size of Ext Header
037 */
038 private static final int MIN_SIZE = 6;
039 /**
040 * Size of CRC
041 */
042 private static final int CRC_SIZE = 5;
043 /**
044 * ???
045 */
046 private static final int[] MAX_TAG_FRAMES_TABLE = {128, 64, 32, 32};
047 /**
048 * ???
049 */
050 private static final int[] MAX_TAG_SIZE_TABLE = {8000000, 1024000, 320000, 32000};
051 /**
052 * ???
053 */
054 private static final int[] MAX_TEXT_SIZE_TABLE = {-1, 1024, 128, 30};
055
056 /**
057 * the File
058 */
059 private File mp3 = null;
060 /**
061 * the Size (of what)?
062 */
063 private int size;
064 /**
065 * ???
066 */
067 private int numFlagBytes;
068 /**
069 * ???
070 */
071 private boolean update;
072 /**
073 * ???
074 */
075 private boolean crced;
076 /**
077 * ???
078 */
079 private byte[] crc;
080 /**
081 * ???
082 */
083 private int maxFrames;
084 /**
085 * ???
086 */
087 private int maxTagSize;
088 /**
089 * ???
090 */
091 private boolean textEncode;
092 /**
093 * ???
094 */
095 private int maxTextSize;
096 /**
097 * ???
098 */
099 private boolean imageEncode;
100 /**
101 * ???
102 */
103 private int imageRestrict;
104
105
106 /**
107 * Create an extended header object from the file passed. Information in the
108 * file's extended header will be read and stored.
109 *
110 *@param mp3 the file to read/write to
111 *@exception FileNotFoundException if an error occurs
112 *@exception IOException if an error occurs
113 *@exception ID3v2FormatException if an error occurs
114 */
115 public ID3v2ExtendedHeader(File mp3)
116 throws IOException, ID3v2FormatException {
117
118 this.mp3 = mp3;
119
120 size = 0;
121 numFlagBytes = 0;
122 update = false;
123 crced = false;
124 crc = new byte[CRC_SIZE];
125 maxFrames = -1;
126 maxTagSize = -1;
127 textEncode = false;
128 maxTextSize = -1;
129 imageEncode = false;
130 imageRestrict = -1;
131
132 RandomAccessFile in = null;
133
134 try {
135 in = new RandomAccessFile(mp3, "r");
136 readExtendedHeader(in);
137 } finally {
138 if (in != null) {
139 in.close();
140 }
141 }
142 }
143
144
145 /**
146 * Read the information in the file's extended header
147 *
148 *@param raf the open file to read from
149 *@exception FileNotFoundException if an error occurs
150 *@exception IOException if an error occurs
151 *@exception ID3v2FormatException if an error occurs
152 */
153 private void readExtendedHeader(RandomAccessFile raf)
154 throws IOException, ID3v2FormatException {
155
156 raf.seek(EXT_HEAD_LOCATION);
157
158 byte[] buf = new byte[4];
159 if (raf.read(buf) != buf.length) {
160 throw new IOException("Error reading extended header:size");
161 }
162
163 size = BinaryParser.convertToSynchsafeInt(buf);
164 if (size < MIN_SIZE) {
165 throw new ID3v2FormatException("The extended header size data"
166 + " is less than the minimum required size.");
167 }
168
169 buf = new byte[1];
170 if (raf.read(buf) != buf.length) {
171 throw new IOException("Error reading extended header:numflags");
172 }
173
174 numFlagBytes = (int) buf[0];
175 if(numFlagBytes < 0){
176 throw new ID3v2FormatException("Illegal number of flag bytes in extended header.");
177 }
178 buf = new byte[numFlagBytes + 1];
179
180 if (raf.read(buf) != buf.length) {
181 throw new IOException("Error reading extended header:flags");
182 }
183
184 parseFlags(buf);
185 }
186
187
188 /**
189 * Parse the extended header flag bytes
190 *
191 *@param flags the array of extended flags
192 *@exception ID3v2FormatException if an error occurs
193 */
194 private void parseFlags(byte[] flags) throws ID3v2FormatException {
195 int bytesRead = 1;
196
197 if (BinaryParser.bitSet(flags[0], 6)) {
198 update = true;
199 bytesRead += 1;
200 }
201 if (BinaryParser.bitSet(flags[0], 5)) {
202 crced = true;
203 bytesRead += 1;
204 for (int i = 0; i < crc.length; i++) {
205 crc[i] = flags[bytesRead++];
206 }
207 }
208 if (BinaryParser.bitSet(flags[0], 4)) {
209 bytesRead += 1;
210 maxTagSize = BinaryParser.convertToDecimal(
211 flags[bytesRead], 6, 7);
212 textEncode = BinaryParser.bitSet(flags[bytesRead], 5);
213 maxTextSize = BinaryParser.convertToDecimal(
214 flags[bytesRead], 3, 4);
215 imageEncode = BinaryParser.bitSet(flags[bytesRead], 2);
216 imageRestrict = BinaryParser.convertToDecimal(
217 flags[bytesRead], 0, 1);
218 bytesRead += 1;
219 }
220
221 if (bytesRead != numFlagBytes) {
222 throw new ID3v2FormatException("The number of found flag bytes "
223 + "in the extended header is not "
224 + "equal to the number specified " + "in the extended header.");
225 }
226 }
227
228
229 /**
230 * Return an array of bytes representing this extended header in the standard
231 * format to be written to a file.
232 *
233 *@return a binary represenation of this extended header
234 */
235 public byte[] getBytes() {
236 byte[] b = new byte[size];
237 int bytesCopied = 0;
238
239 System.arraycopy(BinaryParser.convertToSynchsafeBytes(size), 0, b,
240 bytesCopied, 4);
241 bytesCopied += 4;
242 b[bytesCopied++] = (byte) numFlagBytes;
243 System.arraycopy(getFlagBytes(), 0, b, bytesCopied, numFlagBytes);
244 bytesCopied += numFlagBytes;
245
246 return b;
247 }
248
249
250 /**
251 * A helper function for the getBytes method that returns a byte array
252 * representing the extended flags field of the extended header.
253 *
254 *@return the extended flags field of the extended header
255 */
256 private byte[] getFlagBytes() {
257 byte[] b = new byte[numFlagBytes];
258 int bytesCopied = 1;
259 b[0] = 0;
260
261 if (update) {
262 b[0] = BinaryParser.setBit(b[0], 7);
263 b[bytesCopied++] = 0;
264 }
265 if (crced) {
266 b[0] = BinaryParser.setBit(b[0], 6);
267 b[bytesCopied++] = (byte) crc.length;
268 System.arraycopy(crc, 0, b, bytesCopied, crc.length);
269 bytesCopied += crc.length;
270 }
271 if ((maxTagSize != -1) || textEncode || (maxTextSize != -1)
272 || imageEncode || (imageRestrict != -1)) {
273
274 b[0] = BinaryParser.setBit(b[0], 5);
275 b[bytesCopied++] = 0x01;
276 byte restrict = 0;
277 if (maxTagSize != -1) {
278 if (BinaryParser.bitSet((byte) maxTagSize, 0)) {
279 restrict = BinaryParser.setBit(restrict, 6);
280 }
281 if (BinaryParser.bitSet((byte) maxTagSize, 1)) {
282 restrict = BinaryParser.setBit(restrict, 7);
283 }
284 }
285 if (textEncode) {
286 restrict = BinaryParser.setBit(restrict, 5);
287 }
288 if (maxTextSize != -1) {
289 if (BinaryParser.bitSet((byte) maxTextSize, 0)) {
290 restrict = BinaryParser.setBit(restrict, 3);
291 }
292 if (BinaryParser.bitSet((byte) maxTextSize, 1)) {
293 restrict = BinaryParser.setBit(restrict, 4);
294 }
295 }
296 if (imageEncode) {
297 restrict = BinaryParser.setBit(restrict, 2);
298 }
299 if (imageRestrict != -1) {
300 if (BinaryParser.bitSet((byte) imageRestrict, 0)) {
301 restrict = BinaryParser.setBit(restrict, 0);
302 }
303 if (BinaryParser.bitSet((byte) imageRestrict, 1)) {
304 restrict = BinaryParser.setBit(restrict, 1);
305 }
306 }
307
308 b[bytesCopied++] = restrict;
309 }
310
311 return b;
312 }
313
314
315 /**
316 * Returns the size of the extended header
317 *
318 *@return the size of the extended header
319 */
320 public int getSize() {
321 return size;
322 }
323
324
325 /**
326 * Returns the number of extended flag bytes
327 *
328 *@return the number of extended flag bytes
329 */
330 public int getNumFlagBytes() {
331 return numFlagBytes;
332 }
333
334
335 /**
336 * Returns the maximum number of frames if set. If unset, returns -1
337 *
338 *@return the maximum number of frames or -1 if unset
339 */
340 public int getMaxFrames() {
341 int retval = -1;
342
343 if ((maxTagSize >= 0) && (maxTagSize < MAX_TAG_FRAMES_TABLE.length)) {
344 retval = MAX_TAG_FRAMES_TABLE[maxTagSize];
345 }
346
347 return retval;
348 }
349
350
351 /**
352 * Returns the maximum tag size or -1 if unset
353 *
354 *@return the maximum tag size or -1 if unset
355 */
356 public int getMaxTagSize() {
357 int retval = -1;
358
359 if ((maxTagSize >= 0) && (maxTagSize < MAX_TAG_SIZE_TABLE.length)) {
360 retval = MAX_TAG_SIZE_TABLE[maxTagSize];
361 }
362
363 return retval;
364 }
365
366
367 /**
368 * Returns true if the text encode flag is set
369 *
370 *@return true if the text encode flag is set
371 */
372 public boolean getTextEncode() {
373 return textEncode;
374 }
375
376
377 /**
378 * Returns the maximum length of a string if set or -1
379 *
380 *@return the maximum length of a string if set or -1
381 */
382 public int getMaxTextSize() {
383 int retval = -1;
384
385 if ((maxTextSize >= 0) && (maxTextSize < MAX_TEXT_SIZE_TABLE.length)) {
386 retval = MAX_TEXT_SIZE_TABLE[maxTextSize];
387 }
388
389 return retval;
390 }
391
392
393 /**
394 * Returns true if the image encode flag is set
395 *
396 *@return true if the image encode flag is set
397 */
398 public boolean getImageEncode() {
399 return imageEncode;
400 }
401
402
403 /**
404 * Returns the value of the image restriction field or -1 if not set
405 *
406 *@return the value of the image restriction field or -1 if not set
407 */
408 public int getImageRestriction() {
409 return imageRestrict;
410 }
411
412
413 /**
414 * Returns true if this tag is an update of a previous tag
415 *
416 *@return true if this tag is an update of a previous tag
417 */
418 public boolean getUpdate() {
419 return update;
420 }
421
422
423 /**
424 * Returns true if CRC information is provided for this tag
425 *
426 *@return true if CRC information is provided for this tag
427 */
428 public boolean getCRCed() {
429 return crced;
430 }
431
432
433 /**
434 * If there is crc data in the extended header, then the attached 5 byte crc
435 * will be returned. An empty array will be returned if this has not been set.
436 *
437 *@return the attached crc data if there is any
438 */
439 public byte[] getCRC() {
440 return crc;
441 }
442
443
444 /**
445 * Returns a string representation of this object that contains all
446 * information within.
447 *
448 *@return a string representation of this object
449 */
450 public String toString() {
451 return "ExtendedSize:\t\t\t" + getSize() + " bytes"
452 + "\nNumFlagBytes:\t\t\t" + getNumFlagBytes()
453 + "\nUpdated:\t\t\t" + getUpdate()
454 + "\nCRC:\t\t\t\t" + getCRCed()
455 + "\nMaxFrames:\t\t\t" + getMaxFrames()
456 + "\nMaxTagSize:\t\t\t" + getMaxTagSize()
457 + "\nTextEncoded:\t\t\t" + getTextEncode()
458 + "\nMaxTextSize:\t\t\t" + getMaxTextSize()
459 + "\nImageEncoded:\t\t\t" + getImageEncode()
460 + "\nImageRestriction:\t\t" + getImageRestriction();
461 }
462
463 }
464