📄 id3v1tag.java
字号:
/**
* @author : Paul Taylor
* @author : Eric Farng
*
* Version @version:$Id: ID3v1Tag.java,v 1.22 2007/11/27 17:03:31 paultaylor Exp $
*
* MusicTag Copyright (C)2003,2004
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not,
* you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Description:
*
*/
package com.hadeslee.audiotag.tag.id3;
import com.hadeslee.audiotag.audio.mp3.MP3File;
import com.hadeslee.audiotag.audio.generic.Utils;
import com.hadeslee.audiotag.tag.Tag;
import com.hadeslee.audiotag.tag.TagField;
import com.hadeslee.audiotag.tag.TagFieldKey;
import com.hadeslee.audiotag.tag.TagNotFoundException;
import com.hadeslee.audiotag.tag.TagOptionSingleton;
import com.hadeslee.audiotag.tag.id3.valuepair.GenreTypes;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.regex.Matcher;
/**
* Represents an ID3v1 tag.
*
* @author : Eric Farng
* @author : Paul Taylor
*/
public class ID3v1Tag extends AbstractID3v1Tag implements Tag
{
static EnumMap<TagFieldKey, ID3v1FieldKey> tagFieldToID3v1Field = new EnumMap<TagFieldKey, ID3v1FieldKey>(TagFieldKey.class);
static
{
tagFieldToID3v1Field.put(TagFieldKey.ARTIST, ID3v1FieldKey.ARTIST);
tagFieldToID3v1Field.put(TagFieldKey.ALBUM, ID3v1FieldKey.ALBUM);
tagFieldToID3v1Field.put(TagFieldKey.TITLE, ID3v1FieldKey.TITLE);
tagFieldToID3v1Field.put(TagFieldKey.TRACK, ID3v1FieldKey.TRACK);
tagFieldToID3v1Field.put(TagFieldKey.YEAR, ID3v1FieldKey.YEAR);
tagFieldToID3v1Field.put(TagFieldKey.GENRE, ID3v1FieldKey.GENRE);
tagFieldToID3v1Field.put(TagFieldKey.COMMENT, ID3v1FieldKey.COMMENT);
}
//For writing output
protected static final String TYPE_COMMENT = "comment";
protected static final int FIELD_COMMENT_LENGTH = 30;
protected static final int FIELD_COMMENT_POS = 97;
protected static final int BYTE_TO_UNSIGNED = 0xff;
protected static final int GENRE_UNDEFINED = 0xff;
protected String encoding="GBK";
/**
*
*/
protected String album = "";
/**
*
*/
protected String artist = "";
/**
*
*/
protected String comment = "";
/**
*
*/
protected String title = "";
/**
*
*/
protected String year = "";
/**
*
*/
protected byte genre = (byte) -1;
private static final byte RELEASE = 1;
private static final byte MAJOR_VERSION = 0;
private static final byte REVISION = 0;
/**
* Retrieve the Release
*/
public byte getRelease()
{
return RELEASE;
}
/**
* Retrieve the Major Version
*/
public byte getMajorVersion()
{
return MAJOR_VERSION;
}
/**
* Retrieve the Revision
*/
public byte getRevision()
{
return REVISION;
}
/**
* Creates a new ID3v1 datatype.
*/
public ID3v1Tag()
{
}
public ID3v1Tag(ID3v1Tag copyObject)
{
super(copyObject);
this.album = new String(copyObject.album);
this.artist = new String(copyObject.artist);
this.comment = new String(copyObject.comment);
this.title = new String(copyObject.title);
this.year = new String(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 = new String(convertedTag.album);
this.artist = new String(convertedTag.artist);
this.comment = new String(convertedTag.comment);
this.title = new String(convertedTag.title);
this.year = new String(convertedTag.year);
this.genre = convertedTag.genre;
}
}
/**
* Creates a new ID3v1 datatype.
*
* @param file
* @param loggingFilename
* @throws TagNotFoundException
* @throws IOException
*/
public ID3v1Tag(RandomAccessFile file, String loggingFilename)
throws TagNotFoundException, 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);
}
/**
* Creates a new ID3v1 datatype.
*
* @param file
* @throws TagNotFoundException
* @throws IOException
* @deprecated use {@link #ID3v1Tag(RandomAccessFile,String)} instead
*/
public ID3v1Tag(RandomAccessFile file)
throws TagNotFoundException, IOException
{
this(file, "");
}
public void add(TagField field)
{
//TODO
}
public List get(String id)
{
//TODO
return null;
}
public int getFieldCount()
{
return 7;
}
protected List returnFieldToList(ID3v1TagField field)
{
List fields = new ArrayList();
fields.add(field);
return fields;
}
/**
* Add Album
* <p/>
* <p>Only one album can be added so if one already exists it will be replaced.
*
* @param album
*/
public void addAlbum(String album)
{
setAlbum(album);
}
/**
* Set Album
*
* @param album
*/
public void setAlbum(String album)
{
this.album = ID3Tags.truncate(album, this.FIELD_ALBUM_LENGTH);
}
/**
* Get Album
*
* @return album
*/
public String getFirstAlbum()
{
return album;
}
/**
* @return album within list or empty if does not exist
*/
public List getAlbum()
{
if (getFirstAlbum().length() > 0)
{
ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ALBUM.name(), getFirstAlbum());
return returnFieldToList(field);
}
else
{
return new ArrayList();
}
}
/**
* Add Artist
* <p/>
* <p>Only one artist can be added so if one already exists it will be replaced.
*
* @param artist
*/
public void addArtist(String artist)
{
setArtist(album);
}
/**
* Set Artist
*
* @param artist
*/
public void setArtist(String artist)
{
this.artist = ID3Tags.truncate(artist, this.FIELD_ARTIST_LENGTH);
}
/**
* Get Artist
*
* @return artist
*/
public String getFirstArtist()
{
return artist;
}
/**
* @return Artist within list or empty if does not exist
*/
public List getArtist()
{
if (getFirstArtist().length() > 0)
{
ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ARTIST.name(), getFirstArtist());
return returnFieldToList(field);
}
else
{
return new ArrayList();
}
}
/**
* Add Comment
* <p/>
* <p>Only one comment can be added so if one already exists it will be replaced.
*
* @param comment
*/
public void addComment(String comment)
{
setComment(comment);
}
/**
* Set Comment
*
* @param comment
*/
public void setComment(String comment)
{
this.comment = ID3Tags.truncate(comment, this.FIELD_COMMENT_LENGTH);
}
/**
* @return comment within list or empty if does not exist
*/
public List getComment()
{
if (getFirstComment().length() > 0)
{
ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.COMMENT.name(), getFirstComment());
return returnFieldToList(field);
}
else
{
return new ArrayList();
}
}
/**
* Get Comment
*
* @return comment
*/
public String getFirstComment()
{
return comment;
}
/**
* Add Genre
* <p/>
* <p>Only one Genre can be added so if one already exists it will be replaced.
*
* @param genre
*/
public void addGenre(String genre)
{
setGenre(genre);
}
/**
* Sets the genreID,
* <p/>
* <p>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
*/
public void setGenre(String genreVal)
{
Integer genreID = GenreTypes.getInstanceOf().getIdForValue(genreVal);
if (genreID != null)
{
this.genre = genreID.byteValue();
}
else
{
this.genre = (byte) GENRE_UNDEFINED;
}
}
/**
* Get Genre
*
* @return genre or empty string if not valid
*/
public String getFirstGenre()
{
Integer genreId = genre & this.BYTE_TO_UNSIGNED;
String genreValue = GenreTypes.getInstanceOf().getValueForId(genreId);
if (genreValue == null)
{
return "";
}
else
{
return genreValue;
}
}
/**
* Get Genre field
* <p/>
* <p>Only a single genre is available in ID3v1
*
* @return
*/
public List getGenre()
{
if (getFirstGenre().length() > 0)
{
ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.GENRE.name(), getFirstGenre());
return returnFieldToList(field);
}
else
{
return new ArrayList();
}
}
/**
* Add Title
* <p/>
* <p>Only one title can be added so if one already exists it will be replaced.
*
* @param title
*/
public void addTitle(String title)
{
setTitle(title);
}
/**
* Set Title
*
* @param title
*/
public void setTitle(String title)
{
this.title = ID3Tags.truncate(title, this.FIELD_TITLE_LENGTH);
}
/**
* Get title
*
* @return Title
*/
public String getFirstTitle()
{
return title;
}
/**
* Get title field
* <p/>
* <p>Only a single title is available in ID3v1
*
* @return
*/
public List getTitle()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -