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