FileDocCategorySizeDatePackage
ID3V1Tag.javaAPI Docjid3 0.4633959Thu May 05 06:15:00 BST 2005org.blinkenlights.jid3.v1

ID3V1Tag

public abstract class ID3V1Tag extends ID3Tag
author
paul The base class for ID3 V1 tags.

Fields Summary
protected String
m_sTitle
protected String
m_sArtist
protected String
m_sAlbum
protected String
m_sYear
protected String
m_sComment
protected Genre
m_oGenre
Constructors Summary
public ID3V1Tag()
Constructor.


           
     
    
        super();
    
Methods Summary
public java.lang.StringgetAlbum()
Get the name of the album from which the recording comes.

return
the set name of the album

        return m_sAlbum;
    
public java.lang.StringgetArtist()
Get the artist of the recording.

return
the set artist for the recording

        return m_sArtist;
    
public java.lang.StringgetComment()
Get the comment.

return
the set comment

        return m_sComment;
    
public org.blinkenlights.jid3.v1.ID3V1Tag$GenregetGenre()
Get the genre of the recording.

return
the predefined genre of the recording

        return m_oGenre;
    
public java.lang.StringgetTitle()
Get the title of the recording.

return
the set title for the recording

        return m_sTitle;
    
public java.lang.StringgetYear()
Get the year in which the recording was made.

return
the set year of the recording

        return m_sYear;
    
private static intindexOfFirstNull(byte[] abyString)
Utility method to find the index of the first zero byte in a byte array.

param
abyString the byte array to examine
return
the index position of the first zero byte in the array, or the length of the array if no zeros exist

        for (int i=0; i < abyString.length; i++)
        {
            if (abyString[i] == 0)
            {
                return i;
            }
        }
        
        return abyString.length;
    
public static org.blinkenlights.jid3.v1.ID3V1Tagread(java.io.InputStream oIS)
Read an ID3 V1 tag from an input stream.

param
oIS the input stream from which to read a V1 tag
return
an object representing the tag just read
throws
ID3Exception if an error occurs while reading the tag

        try
        {
            // title (30 bytes)
            byte[] abyTitle = new byte[30];
            if (oIS.read(abyTitle) != 30)
            {
                throw new ID3Exception("Unexpected EOF while reading title.");
            }
            String sTitle = new String(abyTitle, 0, indexOfFirstNull(abyTitle));
            // artist (30 bytes)
            byte[] abyArtist = new byte[30];
            if (oIS.read(abyArtist) != 30)
            {
                throw new ID3Exception("Unexpected EOF while reading artist.");
            }
            String sArtist = new String(abyArtist, 0, indexOfFirstNull(abyArtist));
            // album (30 bytes)
            byte[] abyAlbum = new byte[30];
            if (oIS.read(abyAlbum) != 30)
            {
                throw new ID3Exception("Unexpected EOF while reading album.");
            }
            String sAlbum = new String(abyAlbum, 0, indexOfFirstNull(abyAlbum));
            // year (4 bytes)
            byte[] abyYear = new byte[4];
            if (oIS.read(abyYear) != 4)
            {
                throw new ID3Exception("Unexpected EOF while reading year.");
            }
            String sYear = new String(abyYear, 0, indexOfFirstNull(abyYear));
            // comment (30 bytes) possibly including an album track # as the last byte
            String sComment;
            boolean bHasTrackNum;
            int iTrackNum = -1;
            byte[] abyTemp = new byte[30];
            if (oIS.read(abyTemp) != 30)
            {
                throw new ID3Exception("Unexpected EOF while reading comment.");
            }
            if ((abyTemp[28] == 0) && (abyTemp[29] != 0))
            {
                // this is a v1.1 tag with comment and track number
                byte[] abyComment = new byte[29];
                System.arraycopy(abyTemp, 0, abyComment, 0, 29);
                sComment = new String(abyComment, 0, indexOfFirstNull(abyComment));
                iTrackNum = abyTemp[29] & 0xff;
                bHasTrackNum = true;
            }
            else
            {
                // this is just a comment
                sComment = new String(abyTemp, 0, indexOfFirstNull(abyTemp));
                bHasTrackNum = false;
            }
            // genre
            int iGenre = oIS.read();
            
            // create appropriate ID3V1Tag object and set properties
            ID3V1Tag oID3V1Tag = null;
            if (bHasTrackNum)
            {
                // v1.1 tag
                oID3V1Tag = new ID3V1_1Tag();
                ((ID3V1_1Tag)oID3V1Tag).setAlbumTrack(iTrackNum);
            }
            else
            {
                // v1.0 tag
                oID3V1Tag = new ID3V1_0Tag();
            }
            oID3V1Tag.setTitle(sTitle);
            oID3V1Tag.setArtist(sArtist);
            oID3V1Tag.setAlbum(sAlbum);
            oID3V1Tag.setYear(sYear);
            oID3V1Tag.setComment(sComment);
            try
            {
                oID3V1Tag.setGenre(Genre.lookupGenre(iGenre));
            }
            catch (ID3Exception e)
            {
                if (ID3Tag.usingStrict())
                {
                    throw e;
                }
                else
                {
                    oID3V1Tag.setGenre(new Genre((byte)iGenre, "Unknown"));
                }
            }
            
            return oID3V1Tag;
        }
        catch (Exception e)
        {
            throw new ID3Exception(e);
        }
    
public voidsetAlbum(java.lang.String sAlbum)
Set the name of the album from which the recording comes.

param
sAlbum the name of the album (truncated to 30 characters, if longer)

        if (sAlbum.length() > 30)
        {
            sAlbum = sAlbum.substring(0, 30);
        }
        
        m_sAlbum = sAlbum;
    
public voidsetArtist(java.lang.String sArtist)
Set the artist for the recording.

param
sArtist the artist of the recording (truncated to 30 characters, if longer)

        if (sArtist.length() > 30)
        {
            sArtist = sArtist.substring(0, 30);
        }
        
        m_sArtist = sArtist;
    
public abstract voidsetComment(java.lang.String sComment)
Set the comment field.

param
sComment a comment field (truncated to 30 characters, if longer, or 28 characters in a v1.1 tag)

public voidsetGenre(org.blinkenlights.jid3.v1.ID3V1Tag$Genre oGenre)
Set the genre of the recording, using one of the predefined genre values.

param
oGenre the genre of the recording

        m_oGenre = oGenre;
    
public voidsetTitle(java.lang.String sTitle)
Set the title of the recording.

param
sTitle the title of the recording (truncated to 30 characters, if longer)

        if (sTitle.length() > 30)
        {
            sTitle = sTitle.substring(0, 30);
        }
        
        m_sTitle = sTitle;
    
public voidsetYear(java.lang.String sYear)
Set the year in which the recording was made.

param
sYear the year of the recording (up to 4 characters, should be numeric)

        if (sYear.length() > 4)
        {
            sYear = sYear.substring(0, 4);
        }
        
        m_sYear = sYear;
    
public java.lang.StringtoString()

        // lookup genre string, if possible (print byte value otherwise)
        String sGenre = null;
        try
        {
            sGenre = ID3V1Tag.Genre.lookupGenre(m_oGenre.m_byGenre).toString();
        }
        catch (Exception e)
        {
            sGenre = Byte.toString(m_oGenre.m_byGenre);
        }
        
        return "SongTitle = [" + m_sTitle + "]\n" +
               "Artist = [" + m_sArtist + "]\n" +
               "Album = [" + m_sAlbum + "]\n" +
               "Year = [" + m_sYear + "]\n" +
               "Comment = [" + m_sComment + "]\n" +
               "Genre = " + sGenre; //m_oGenre.m_byGenre;
    
public abstract voidwrite(java.io.OutputStream oOS)
Write tag to output stream.

param
oOS output stream to which tag is to be written
throws
ID3Exception if an error occurs while writing the tag