FileDocCategorySizeDatePackage
ID3v1Tag.javaAPI DocJaudiotagger 2.0.428579Wed Dec 07 11:08:26 GMT 2011org.jaudiotagger.tag.id3

ID3v1Tag

public class ID3v1Tag extends AbstractID3v1Tag implements Tag
Represents an ID3v1 tag.
author
: Eric Farng
author
: Paul Taylor

Fields Summary
static EnumMap
tagFieldToID3v1Field
protected static final String
TYPE_COMMENT
protected static final int
FIELD_COMMENT_LENGTH
protected static final int
FIELD_COMMENT_POS
protected static final int
BYTE_TO_UNSIGNED
protected static final int
GENRE_UNDEFINED
protected String
album
protected String
artist
protected String
comment
protected String
title
protected String
year
protected byte
genre
private static final byte
RELEASE
private static final byte
MAJOR_VERSION
private static final byte
REVISION
Constructors Summary
public ID3v1Tag()
Creates a new ID3v1 datatype.


    
public ID3v1Tag(ID3v1Tag copyObject)

        super(copyObject);

        this.album = copyObject.album;
        this.artist = copyObject.artist;
        this.comment = copyObject.comment;
        this.title = copyObject.title;
        this.year = copyObject.year;
        this.genre = copyObject.genre;
    
public ID3v1Tag(AbstractTag mp3tag)


        if (mp3tag != null)
        {
            ID3v11Tag convertedTag;
            if (mp3tag instanceof ID3v1Tag)
            {
                throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument");
            }
            if (mp3tag instanceof ID3v11Tag)
            {
                convertedTag = (ID3v11Tag) mp3tag;
            }
            else
            {
                convertedTag = new ID3v11Tag(mp3tag);
            }
            this.album = convertedTag.album;
            this.artist = convertedTag.artist;
            this.comment = convertedTag.comment;
            this.title = convertedTag.title;
            this.year = convertedTag.year;
            this.genre = convertedTag.genre;
        }
    
public ID3v1Tag(RandomAccessFile file, String loggingFilename)
Creates a new ID3v1 datatype.

param
file
param
loggingFilename
throws
TagNotFoundException
throws
IOException

        setLoggingFilename(loggingFilename);
        FileChannel fc;
        ByteBuffer byteBuffer;

        fc = file.getChannel();
        fc.position(file.length() - TAG_LENGTH);
        byteBuffer = ByteBuffer.allocate(TAG_LENGTH);
        fc.read(byteBuffer);
        byteBuffer.flip();
        read(byteBuffer);
    
public ID3v1Tag(RandomAccessFile file)
Creates a new ID3v1 datatype.

param
file
throws
TagNotFoundException
throws
IOException
deprecated
use {@link #ID3v1Tag(RandomAccessFile,String)} instead

        this(file, "");
    
Methods Summary
public voidaddField(FieldKey genericKey, java.lang.String value)

        setField(genericKey,value);
    
public voidaddField(org.jaudiotagger.tag.images.Artwork artwork)

        throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
    
public voidaddField(TagField field)

        //TODO
    
public TagFieldcreateField(FieldKey genericKey, java.lang.String value)
Create Tag Field using generic key

        return new ID3v1TagField(tagFieldToID3v1Field.get(genericKey).name(), value);
    
public TagFieldcreateField(org.jaudiotagger.tag.images.Artwork artwork)

        throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
    
public voidcreateStructure()
Create structured representation of this item.

        MP3File.getStructureFormatter().openHeadingElement(TYPE_TAG, getIdentifier());
        //Header
        MP3File.getStructureFormatter().addElement(TYPE_TITLE, this.title);
        MP3File.getStructureFormatter().addElement(TYPE_ARTIST, this.artist);
        MP3File.getStructureFormatter().addElement(TYPE_ALBUM, this.album);
        MP3File.getStructureFormatter().addElement(TYPE_YEAR, this.year);
        MP3File.getStructureFormatter().addElement(TYPE_COMMENT, this.comment);
        MP3File.getStructureFormatter().addElement(TYPE_GENRE, this.genre);
        MP3File.getStructureFormatter().closeHeadingElement(TYPE_TAG);
    
public voiddeleteArtworkField()
Delete all instance of artwork Field

throws
KeyNotFoundException

        throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
    
public voiddeleteField(FieldKey genericKey)
Delete any instance of tag fields with this key

param
genericKey

        switch (genericKey)
        {
            case ARTIST:
                setArtist("");
                break;

            case ALBUM:
                setAlbum("");
                break;

            case TITLE:
                setTitle("");
                break;

            case GENRE:
                setGenre("");
                break;

            case YEAR:
                setYear("");
                break;

            case COMMENT:
                setComment("");
                break;
        }
    
public voiddeleteField(java.lang.String id)

        FieldKey key = FieldKey.valueOf(id);
        if(key!=null)
        {
            deleteField(key);
        }
    
public booleanequals(java.lang.Object obj)

param
obj
return
true if this and obj are equivalent

        if (!(obj instanceof ID3v1Tag))
        {
            return false;
        }
        ID3v1Tag object = (ID3v1Tag) obj;
        if (!this.album.equals(object.album))
        {
            return false;
        }
        if (!this.artist.equals(object.artist))
        {
            return false;
        }
        if (!this.comment.equals(object.comment))
        {
            return false;
        }
        if (this.genre != object.genre)
        {
            return false;
        }
        if (!this.title.equals(object.title))
        {
            return false;
        }
        return this.year.equals(object.year) && super.equals(obj);
    
public java.util.ListgetAlbum()

return
album within list or empty if does not exist

        if (getFirstAlbum().length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ALBUM.name(), getFirstAlbum());
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public java.util.ListgetArtist()

return
Artist within list or empty if does not exist

        if (getFirstArtist().length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ARTIST.name(), getFirstArtist());
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public java.util.ListgetArtworkList()

       return Collections.emptyList();
    
public java.util.ListgetComment()

return
comment within list or empty if does not exist

        if (getFirstComment().length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.COMMENT.name(), getFirstComment());
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public java.lang.StringgetEncoding()

        return "ISO-8859-1";
    
public intgetFieldCount()

        return 6;
    
public intgetFieldCountIncludingSubValues()

       return getFieldCount();
    
public java.util.ListgetFields(java.lang.String id)


        if (FieldKey.ARTIST.name().equals(id))
        {
            return getArtist();
        }
        else if (FieldKey.ALBUM.name().equals(id))
        {
            return getAlbum();
        }
        else if (FieldKey.TITLE.name().equals(id))
        {
            return getTitle();
        }
        else if (FieldKey.GENRE.name().equals(id))
        {
            return getGenre();
        }
        else if (FieldKey.YEAR.name().equals(id))
        {
            return getYear();
        }
        else if (FieldKey.COMMENT.name().equals(id))
        {
            return getComment();
        }
        return new ArrayList<TagField>();
    
public java.util.IteratorgetFields()

        throw new UnsupportedOperationException("TODO:Not done yet");
    
public java.util.ListgetFields(FieldKey genericKey)
Returns a {@linkplain List list} of {@link TagField} objects whose "{@linkplain TagField#getId() id}" is the specified one.

param
genericKey The generic field key
return
A list of {@link TagField} objects with the given "id".

        switch (genericKey)
        {
            case ARTIST:
                return getArtist();

            case ALBUM:
                return getAlbum();

            case TITLE:
                return getTitle();

            case GENRE:
                return getGenre();

            case YEAR:
                return getYear();

            case COMMENT:
                return getComment();

            default:
                return new ArrayList<TagField>();
        }
    
public java.lang.StringgetFirst(java.lang.String genericKey)
Retrieve the first value that exists for this key id

param
genericKey
return

        FieldKey matchingKey = FieldKey.valueOf(genericKey);
        if (matchingKey != null)
        {
            return getFirst(matchingKey);
        }
        else
        {
            return "";
        }
    
public java.lang.StringgetFirst(FieldKey genericKey)
Retrieve the first value that exists for this generic key

param
genericKey
return

        switch (genericKey)
        {
            case ARTIST:
                return getFirstArtist();

            case ALBUM:
                return getFirstAlbum();

            case TITLE:
                return getFirstTitle();

            case GENRE:
                return getFirstGenre();

            case YEAR:
                return getFirstYear();

            case COMMENT:
                return getFirstComment();

            default:
                return "";
        }
    
public java.lang.StringgetFirstAlbum()
Get Album

return
album

        return album;
    
public java.lang.StringgetFirstArtist()
Get Artist

return
artist

        return artist;
    
public org.jaudiotagger.tag.images.ArtworkgetFirstArtwork()

           
        return null;
    
public java.lang.StringgetFirstComment()
Get Comment

return
comment

        return comment;
    
public TagFieldgetFirstField(java.lang.String id)

        List<TagField> results = null;

        if (FieldKey.ARTIST.name().equals(id))
        {
            results = getArtist();
        }
        else if (FieldKey.ALBUM.name().equals(id))
        {
            results = getAlbum();
        }
        else if (FieldKey.TITLE.name().equals(id))
        {
            results = getTitle();
        }
        else if (FieldKey.GENRE.name().equals(id))
        {
            results = getGenre();
        }
        else if (FieldKey.YEAR.name().equals(id))
        {
            results = getYear();
        }
        else if (FieldKey.COMMENT.name().equals(id))
        {
            results = getComment();
        }

        if (results != null)
        {
            if (results.size() > 0)
            {
                return results.get(0);
            }
        }
        return null;
    
public TagFieldgetFirstField(FieldKey genericKey)

        List<TagField> l = getFields(genericKey);
        return (l.size() != 0) ? l.get(0) : null;
    
public java.lang.StringgetFirstGenre()
Get Genre

return
genre or empty string if not valid

        Integer genreId = genre & BYTE_TO_UNSIGNED;
        String genreValue = GenreTypes.getInstanceOf().getValueForId(genreId);
        if (genreValue == null)
        {
            return "";
        }
        else
        {
            return genreValue;
        }
    
public java.lang.StringgetFirstTitle()
Get title

return
Title

        return title;
    
public java.lang.StringgetFirstTrack()

        throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
    
public java.lang.StringgetFirstYear()
Get year

return
year

        return year;
    
public java.util.ListgetGenre()
Get Genre field

Only a single genre is available in ID3v1

return

        if (getFirst(FieldKey.GENRE).length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.GENRE.name(), getFirst(FieldKey.GENRE));
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public bytegetMajorVersion()
Retrieve the Major Version

        return MAJOR_VERSION;
    
public bytegetRelease()
Retrieve the Release


            
      
    
        return RELEASE;
    
public bytegetRevision()
Retrieve the Revision

        return REVISION;
    
public java.lang.StringgetSubValue(FieldKey id, int n, int m)
The m parameter is effectively ignored

param
id
param
n
param
m
return

        return getValue(id,n);
    
public java.util.ListgetTitle()
Get title field

Only a single title is available in ID3v1

return

        if (getFirst(FieldKey.TITLE).length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.TITLE.name(), getFirst(FieldKey.TITLE));
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public java.util.ListgetTrack()

        throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
    
public java.lang.StringgetValue(FieldKey genericKey, int index)

        return getFirst(genericKey);
    
public java.util.ListgetYear()
Get year field

Only a single year is available in ID3v1

return

        if (getFirst(FieldKey.YEAR).length() > 0)
        {
            ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.YEAR.name(), getFirst(FieldKey.YEAR));
            return returnFieldToList(field);
        }
        else
        {
            return new ArrayList<TagField>();
        }
    
public booleanhasCommonFields()

        //TODO
        return true;
    
public booleanhasField(FieldKey genericKey)

        return getFirst(genericKey).length() > 0;
    
public booleanhasField(java.lang.String id)

        try
        {
            FieldKey key = FieldKey.valueOf(id.toUpperCase());
            return hasField(key);
        }
        catch(java.lang.IllegalArgumentException iae)
        {
            return false;
        }
    
public booleanisEmpty()

        return !(getFirst(FieldKey.TITLE).length() > 0 ||
                getFirstArtist().length() > 0 ||
                getFirstAlbum().length() > 0 ||
                getFirst(FieldKey.GENRE).length() > 0 ||
                getFirst(FieldKey.YEAR).length() > 0 ||
                getFirstComment().length() > 0);
    
public java.util.Iteratoriterator()

return
an iterator to iterate through the fields of the tag

        return new ID3v1Iterator(this);
    
public voidread(java.nio.ByteBuffer byteBuffer)

param
byteBuffer
throws
TagNotFoundException

        if (!seek(byteBuffer))
        {
            throw new TagNotFoundException(getLoggingFilename() + ":" + "ID3v1 tag not found");
        }
        logger.finer(getLoggingFilename() + ":" + "Reading v1 tag");
        //Do single file read of data to cut down on file reads
        byte[] dataBuffer = new byte[TAG_LENGTH];
        byteBuffer.position(0);
        byteBuffer.get(dataBuffer, 0, TAG_LENGTH);
        title = Utils.getString(dataBuffer, FIELD_TITLE_POS, FIELD_TITLE_LENGTH, "ISO-8859-1").trim();
        Matcher m = AbstractID3v1Tag.endofStringPattern.matcher(title);
        if (m.find())
        {
            title = title.substring(0, m.start());
        }
        artist = Utils.getString(dataBuffer, FIELD_ARTIST_POS, FIELD_ARTIST_LENGTH, "ISO-8859-1").trim();
        m = AbstractID3v1Tag.endofStringPattern.matcher(artist);
        if (m.find())
        {
            artist = artist.substring(0, m.start());
        }
        album = Utils.getString(dataBuffer, FIELD_ALBUM_POS, FIELD_ALBUM_LENGTH, "ISO-8859-1").trim();
        m = AbstractID3v1Tag.endofStringPattern.matcher(album);
        logger.finest(getLoggingFilename() + ":" + "Orig Album is:" + comment + ":");
        if (m.find())
        {
            album = album.substring(0, m.start());
            logger.finest(getLoggingFilename() + ":" + "Album is:" + album + ":");
        }
        year = Utils.getString(dataBuffer, FIELD_YEAR_POS, FIELD_YEAR_LENGTH, "ISO-8859-1").trim();
        m = AbstractID3v1Tag.endofStringPattern.matcher(year);
        if (m.find())
        {
            year = year.substring(0, m.start());
        }
        comment = Utils.getString(dataBuffer, FIELD_COMMENT_POS, FIELD_COMMENT_LENGTH, "ISO-8859-1").trim();
        m = AbstractID3v1Tag.endofStringPattern.matcher(comment);
        logger.finest(getLoggingFilename() + ":" + "Orig Comment is:" + comment + ":");
        if (m.find())
        {
            comment = comment.substring(0, m.start());
            logger.finest(getLoggingFilename() + ":" + "Comment is:" + comment + ":");
        }
        genre = dataBuffer[FIELD_GENRE_POS];

    
protected java.util.ListreturnFieldToList(ID3v1TagField field)

        List<TagField> fields = new ArrayList<TagField>();
        fields.add(field);
        return fields;
    
public booleanseek(java.nio.ByteBuffer byteBuffer)
Does a tag of this version exist within the byteBuffer

return
whether tag exists within the byteBuffer

        byte[] buffer = new byte[FIELD_TAGID_LENGTH];
        // read the TAG value
        byteBuffer.get(buffer, 0, FIELD_TAGID_LENGTH);
        return (Arrays.equals(buffer, TAG_ID));
    
public voidsetAlbum(java.lang.String album)
Set Album

param
album

        if (album == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        this.album = ID3Tags.truncate(album, FIELD_ALBUM_LENGTH);
    
public voidsetArtist(java.lang.String artist)
Set Artist

param
artist

        if (artist == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        this.artist = ID3Tags.truncate(artist, FIELD_ARTIST_LENGTH);
    
public voidsetComment(java.lang.String comment)
Set Comment

param
comment
throws
IllegalArgumentException if comment null

        if (comment == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        this.comment = ID3Tags.truncate(comment, FIELD_COMMENT_LENGTH);
    
public booleansetEncoding(java.lang.String encoding)

param
encoding
return

        return true;
    
public voidsetField(FieldKey genericKey, java.lang.String value)

        TagField tagfield = createField(genericKey,value);
        setField(tagfield);
    
public voidsetField(TagField field)

        FieldKey genericKey = FieldKey.valueOf(field.getId());
        switch (genericKey)
        {
            case ARTIST:
                setArtist(field.toString());
                break;

            case ALBUM:
                setAlbum(field.toString());
                break;

            case TITLE:
                setTitle(field.toString());
                break;

            case GENRE:
                setGenre(field.toString());
                break;

            case YEAR:
                setYear(field.toString());
                break;

            case COMMENT:
                setComment(field.toString());
                break;
        }
    
public voidsetField(org.jaudiotagger.tag.images.Artwork artwork)

        throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
    
public voidsetGenre(java.lang.String genreVal)
Sets the genreID,

ID3v1 only supports genres defined in a predefined list so if unable to find value in list set 255, which seems to be the value winamp uses for undefined.

param
genreVal

        if (genreVal == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        Integer genreID = GenreTypes.getInstanceOf().getIdForValue(genreVal);
        if (genreID != null)
        {
            this.genre = genreID.byteValue();
        }
        else
        {
            this.genre = (byte) GENRE_UNDEFINED;
        }
    
public voidsetTitle(java.lang.String title)
Set Title

param
title

        if (title == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        this.title = ID3Tags.truncate(title, FIELD_TITLE_LENGTH);
    
public voidsetYear(java.lang.String year)
Set year

param
year

        this.year = ID3Tags.truncate(year, FIELD_YEAR_LENGTH);
    
public voidwrite(java.io.RandomAccessFile file)
Write this tag to the file, replacing any tag previously existing

param
file
throws
IOException

        logger.config("Saving ID3v1 tag to file");
        byte[] buffer = new byte[TAG_LENGTH];
        int i;
        String str;
        delete(file);
        file.seek(file.length());
        //Copy the TAGID into new buffer
        System.arraycopy(TAG_ID, FIELD_TAGID_POS, buffer, FIELD_TAGID_POS, TAG_ID.length);
        int offset = FIELD_TITLE_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveTitle())
        {
            str = ID3Tags.truncate(title, FIELD_TITLE_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = FIELD_ARTIST_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveArtist())
        {
            str = ID3Tags.truncate(artist, FIELD_ARTIST_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = FIELD_ALBUM_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveAlbum())
        {
            str = ID3Tags.truncate(album, FIELD_ALBUM_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = FIELD_YEAR_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveYear())
        {
            str = ID3Tags.truncate(year, AbstractID3v1Tag.FIELD_YEAR_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = FIELD_COMMENT_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveComment())
        {
            str = ID3Tags.truncate(comment, FIELD_COMMENT_LENGTH);
            for (i = 0; i < str.length(); i++)
            {
                buffer[i + offset] = (byte) str.charAt(i);
            }
        }
        offset = FIELD_GENRE_POS;
        if (TagOptionSingleton.getInstance().isId3v1SaveGenre())
        {
            buffer[offset] = genre;
        }
        file.write(buffer);
        logger.config("Saved ID3v1 tag to file");