📄 id3v11tag.java
字号:
}
/**
* Set the track, v11 stores track numbers in a single byte value so can only
* handle a simple number in the range 0-255.
*
* @param trackValue
*/
@Override
public void setTrack(String trackValue)
{
int trackAsInt;
//Try and convert String representation of track into an integer
try
{
trackAsInt = Integer.parseInt(trackValue);
}
catch (NumberFormatException e)
{
trackAsInt = 0;
}
//This value cannot be held in v1_1
if ((trackAsInt > TRACK_MAX_VALUE) || (trackAsInt < TRACK_MIN_VALUE))
{
this.track = (byte) TRACK_UNDEFINED;
}
else
{
this.track = (byte) Integer.parseInt(trackValue);
}
}
/**
* Return the track number as a String.
*
* @return track
*/
@Override
public String getFirstTrack()
{
return String.valueOf(track & BYTE_TO_UNSIGNED);
}
@Override
public void addTrack(String track)
{
setTrack(track);
}
@Override
public List getTrack()
{
if(getFirstTrack().length()>0)
{
ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.TRACK.name(),getFirstTrack());
return returnFieldToList(field);
}
else
{
return new ArrayList();
}
}
public void set(TagField field)
{
TagFieldKey genericKey = TagFieldKey.valueOf(field.getId());
switch(genericKey)
{
case ARTIST:
setArtist(field.toString());
case ALBUM:
setAlbum(field.toString());
case TITLE:
setTitle(field.toString());
case GENRE:
setGenre(field.toString());
case YEAR:
setYear(field.toString());
case COMMENT:
setComment(field.toString());
case TRACK:
setTrack(field.toString());
}
}
/**
* Compares Object with this only returns true if both v1_1 tags with all
* fields set to same value
*
* @param obj Comparing Object
* @return
*/
public boolean equals(Object obj)
{
if ((obj instanceof ID3v11Tag) == false)
{
return false;
}
ID3v11Tag object = (ID3v11Tag) obj;
if (this.track != object.track)
{
return false;
}
return super.equals(obj);
}
/**
* Find identifer within byteBuffer to indicate that a v11 tag exists within the buffer
* @param byteBuffer
* @return true if find header for v11 tag within buffer
*/
public boolean seek(ByteBuffer byteBuffer)
{
byte[] buffer = new byte[FIELD_TAGID_LENGTH];
// read the TAG value
byteBuffer.get(buffer, 0, FIELD_TAGID_LENGTH);
if (!(Arrays.equals(buffer, TAG_ID)))
{
return false;
}
// Check for the empty byte before the TRACK
byteBuffer.position(this.FIELD_TRACK_INDICATOR_POS);
if (byteBuffer.get() != END_OF_FIELD)
{
return false;
}
//Now check for TRACK if the next byte is also null byte then not v1.1
//tag, however this means cannot have v1_1 tag with track set to zero/undefined
//because on next read will be v1 tag.
if (byteBuffer.get() == END_OF_FIELD)
{
return false;
}
return true;
}
/**
* Read in a tag from the ByteBuffer
*
* @param byteBuffer from where to read in a tag
* @throws TagNotFoundException if unable to read a tag in the byteBuffer
*/
public void read(ByteBuffer byteBuffer)
throws TagNotFoundException
{
if (seek(byteBuffer) == false)
{
throw new TagNotFoundException("ID3v1 tag not found");
}
logger.finer("Reading v1.1 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, this.FIELD_TITLE_LENGTH,"ISO-8859-1").trim();
Matcher m = endofStringPattern.matcher(title);
if (m.find() == true)
{
title = title.substring(0, m.start());
}
artist = Utils.getString(dataBuffer, FIELD_ARTIST_POS, this.FIELD_ARTIST_LENGTH,"ISO-8859-1").trim();
m = endofStringPattern.matcher(artist);
if (m.find() == true)
{
artist = artist.substring(0, m.start());
}
album = Utils.getString(dataBuffer, FIELD_ALBUM_POS, this.FIELD_ALBUM_LENGTH,"ISO-8859-1").trim();
m = endofStringPattern.matcher(album);
if (m.find() == true)
{
album = album.substring(0, m.start());
}
year = Utils.getString(dataBuffer, FIELD_YEAR_POS, this.FIELD_YEAR_LENGTH,"ISO-8859-1").trim();
m = endofStringPattern.matcher(year);
if (m.find() == true)
{
year = year.substring(0, m.start());
}
comment = Utils.getString(dataBuffer, FIELD_COMMENT_POS, this.FIELD_COMMENT_LENGTH,"ISO-8859-1").trim();
m = endofStringPattern.matcher(comment);
if (m.find() == true)
{
comment = comment.substring(0, m.start());
}
track = dataBuffer[FIELD_TRACK_POS];
genre = dataBuffer[FIELD_GENRE_POS];
}
/**
* Write this representation of tag to the file indicated
*
* @param file that this tag should be written to
* @throws IOException thrown if there were problems writing to the file
*/
public void write(RandomAccessFile file)
throws IOException
{
logger.info("Saving file");
byte[] buffer = new byte[TAG_LENGTH];
int i;
String str;
delete(file);
file.seek(file.length());
System.arraycopy(TAG_ID, this.FIELD_TAGID_POS, buffer, this.FIELD_TAGID_POS, TAG_ID.length);
int offset = this.FIELD_TITLE_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveTitle())
{
str = ID3Tags.truncate(title, this.FIELD_TITLE_LENGTH);
for (i = 0; i < str.length(); i++)
{
buffer[i + offset] = (byte) str.charAt(i);
}
}
offset = this.FIELD_ARTIST_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveArtist())
{
str = ID3Tags.truncate(artist, this.FIELD_ARTIST_LENGTH);
for (i = 0; i < str.length(); i++)
{
buffer[i + offset] = (byte) str.charAt(i);
}
}
offset = this.FIELD_ALBUM_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveAlbum())
{
str = ID3Tags.truncate(album, this.FIELD_ALBUM_LENGTH);
for (i = 0; i < str.length(); i++)
{
buffer[i + offset] = (byte) str.charAt(i);
}
}
offset = this.FIELD_YEAR_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveYear())
{
str = ID3Tags.truncate(year, this.FIELD_YEAR_LENGTH);
for (i = 0; i < str.length(); i++)
{
buffer[i + offset] = (byte) str.charAt(i);
}
}
offset = this.FIELD_COMMENT_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveComment())
{
str = ID3Tags.truncate(comment, this.FIELD_COMMENT_LENGTH);
for (i = 0; i < str.length(); i++)
{
buffer[i + offset] = (byte) str.charAt(i);
}
}
offset = this.FIELD_TRACK_POS;
buffer[offset] = track; // skip one byte extra blank for 1.1 definition
offset = this.FIELD_GENRE_POS;
if (TagOptionSingleton.getInstance().isId3v1SaveGenre())
{
buffer[offset] = genre;
}
file.write(buffer);
}
public void createStructure()
{
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_TRACK, (int) this.track);
MP3File.getStructureFormatter().addElement(TYPE_GENRE, (int) this.genre);
MP3File.getStructureFormatter().closeHeadingElement(TYPE_TAG);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -