KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > format > audio > mpeg > ID3Tag


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.format.audio.mpeg;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.rmi.RemoteException JavaDoc;
13
14 import javax.emb.MediaException;
15
16 /**
17  * An ID3 tag, used in MPEG audio files to store meta-data.
18  * See the <a HREF="http://www.id3.org">ID3 web site</a> for details.
19  *
20  * @version <tt>$Revision 1.1 $</tt>
21  * @author <a HREF="mailto:ogreen@users.sourceforge.net">Owen Green</a>
22  */

23 class ID3Tag
24 {
25    private int size = 0;
26    private boolean unsynchronisation;
27    private boolean extendedHeader;
28    private boolean experimentalHeader;
29    private boolean footerPresent;
30
31    private byte versionMajor = 0;
32    private byte versionMinor = 0;
33
34    /**
35     * Constructs a new ID3 tag extracted from the supplied <code>Media</code>
36     * instance
37     * @param mediaObject the <code>Media</code> instance to extract ID3 information from
38     */

39    public ID3Tag(InputStream JavaDoc content) throws MediaException, RemoteException JavaDoc
40    {
41       try
42       {
43          int offset = 3;
44
45          //retrieve the size now, so we can pull the whole tag from the media object
46

47          content.read(new byte[6]);
48
49          byte[] tagSize = new byte[4];
50          content.read(tagSize);
51          size = convertSynchsafeInt(tagSize);
52
53          //pull the whole thing out
54
byte[] tagData = new byte[size];
55
56          content.read(tagData);
57
58          versionMajor = tagData[offset++];
59          versionMinor = tagData[offset++];
60
61          byte flags = tagData[offset++];
62
63          unsynchronisation = (flags & 0x80) > 0;
64          extendedHeader = (flags & 0x40) > 0;
65          experimentalHeader = (flags & 0x20) > 0;
66          footerPresent = (flags & 0x10) > 0;
67       }
68       catch (IOException JavaDoc e)
69       {
70          throw new MediaException(e);
71       }
72    }
73
74    /**
75     * Returns the size of the tag in bytes
76     */

77    public int getSize()
78    {
79       return size;
80    }
81
82    private int convertSynchsafeInt(byte[] data)
83    {
84       if (data.length < 4)
85       {
86          throw new IllegalArgumentException JavaDoc("Synchsafe integer must be 4 bytes long");
87       }
88
89       return convertSynchsafeInt(data[0], data[1], data[2], data[3]);
90    }
91
92    private int convertSynchsafeInt(byte msb, byte b3, byte b2, byte lsb)
93    {
94       return (msb << 23) + (b3 << 15) + (b2 << 7) + lsb;
95    }
96 }
Popular Tags