📄 id3v22tag.java
字号:
/*
* 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
*/
package com.hadeslee.audiotag.tag.id3;
import com.hadeslee.audiotag.FileConstants;
import com.hadeslee.audiotag.audio.mp3.MP3File;
import com.hadeslee.audiotag.tag.EmptyFrameException;
import com.hadeslee.audiotag.tag.FieldDataInvalidException;
import com.hadeslee.audiotag.tag.InvalidFrameException;
import com.hadeslee.audiotag.tag.InvalidFrameIdentifierException;
import com.hadeslee.audiotag.tag.KeyNotFoundException;
import com.hadeslee.audiotag.tag.TagException;
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 static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.ARTIST;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.ALBUM;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.TITLE;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.TRACK;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.DAY;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.COMMENT;
//import static com.hadeslee.jaudiotagger.tag.mp4.Mp4FieldKey.GENRE;
import com.hadeslee.audiotag.tag.id3.framebody.AbstractFrameBodyTextInfo;
import com.hadeslee.audiotag.tag.id3.framebody.FrameBodyTDRC;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.*;
import java.util.logging.Level;
/**
* Represents an ID3v2.2 tag.
*
* @author : Paul Taylor
* @author : Eric Farng
* @version $Id: ID3v22Tag.java,v 1.31 2007/11/13 14:24:30 paultaylor Exp $
*/
public class ID3v22Tag
extends AbstractID3v2Tag
{
protected static final String TYPE_COMPRESSION = "compression";
protected static final String TYPE_UNSYNCHRONISATION = "unsyncronisation";
/**
* ID3v2.2 Header bit mask
*/
public static final int MASK_V22_UNSYNCHRONIZATION = FileConstants.BIT7;
/**
* ID3v2.2 Header bit mask
*/
public static final int MASK_V22_COMPRESSION = FileConstants.BIT7;
/**
* The tag is compressed
*/
protected boolean compression = false;
/**
* All frames in the tag uses unsynchronisation
*/
protected boolean unsynchronization = false;
public static final byte RELEASE = 2;
public static final byte MAJOR_VERSION = 2;
public 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 empty ID3v2_2 tag.
*/
public ID3v22Tag()
{
frameMap = new LinkedHashMap();
}
/**
* Copy primitives applicable to v2.2
*/
protected void copyPrimitives(AbstractID3v2Tag copyObj)
{
logger.info("Copying primitives");
super.copyPrimitives(copyObj);
//Set the primitive types specific to v2_2.
if (copyObj instanceof ID3v22Tag)
{
ID3v22Tag copyObject = (ID3v22Tag) copyObj;
this.compression = copyObject.compression;
this.unsynchronization = copyObject.unsynchronization;
}
else if (copyObj instanceof ID3v23Tag)
{
ID3v23Tag copyObject = (ID3v23Tag) copyObj;
this.compression = copyObject.compression;
this.unsynchronization = copyObject.unsynchronization;
}
else if (copyObj instanceof ID3v24Tag)
{
ID3v24Tag copyObject = (ID3v24Tag) copyObj;
this.compression = false;
this.unsynchronization = copyObject.unsynchronization;
}
}
/**
* Copy frames from one tag into a v2.2 tag
*/
protected void copyFrames(AbstractID3v2Tag copyObject)
{
logger.info("Copying Frames,there are:" + copyObject.frameMap.keySet().size());
frameMap = new LinkedHashMap();
//Copy Frames that are a valid 2.2 type
Iterator iterator = copyObject.frameMap.keySet().iterator();
AbstractID3v2Frame frame;
ID3v22Frame newFrame = null;
while (iterator.hasNext())
{
String id = (String) iterator.next();
Object o = copyObject.frameMap.get(id);
if (o instanceof AbstractID3v2Frame)
{
frame = (AbstractID3v2Frame) o;
//Special case v24 TDRC (FRAME_ID_YEAR) may need converting to multiple frames
if (
(frame.getIdentifier().equals(ID3v24Frames.FRAME_ID_YEAR))
&&
(frame.getBody() instanceof FrameBodyTDRC)
)
{
translateFrame(frame);
}
else
{
try
{
newFrame = new ID3v22Frame(frame);
frameMap.put(newFrame.getIdentifier(), newFrame);
}
catch(InvalidFrameException ife)
{
logger.log(Level.SEVERE,"Unable to convert frame:"+frame.getIdentifier(),ife);
}
}
}
else if (o instanceof ArrayList)
{
ArrayList multiFrame = new ArrayList();
for (ListIterator li = ((ArrayList) o).listIterator(); li.hasNext();)
{
frame = (AbstractID3v2Frame) li.next();
try
{
newFrame = new ID3v22Frame(frame);
multiFrame.add(newFrame);
}
catch(InvalidFrameException ife)
{
logger.log(Level.SEVERE,"Unable to convert frame:"+frame.getIdentifier(),ife);
}
}
if (newFrame != null)
{
frameMap.put(newFrame.getIdentifier(), multiFrame);
}
}
}
}
/**
* Copy Constructor, creates a new ID3v2_2 Tag based on another ID3v2_2 Tag
*/
public ID3v22Tag(ID3v22Tag copyObject)
{
//This doesnt do anything.
super(copyObject);
logger.info("Creating tag from another tag of same type");
copyPrimitives(copyObject);
copyFrames(copyObject);
}
/**
* Constructs a new tag based upon another tag of different version/type
*/
public ID3v22Tag(AbstractTag mp3tag)
{
frameMap = new LinkedHashMap();
logger.info("Creating tag from a tag of a different version");
//Default Superclass constructor does nothing
if (mp3tag != null)
{
ID3v24Tag convertedTag;
//Should use the copy constructor instead
if ((mp3tag instanceof ID3v23Tag == false) && (mp3tag instanceof ID3v22Tag == true))
{
throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument");
}
//If v2.4 can get variables from this
else if (mp3tag instanceof ID3v24Tag)
{
convertedTag = (ID3v24Tag) mp3tag;
}
//Any tag (e.g lyrics3 and idv1.1,idv2.3 can be converted to id32.4 so do that
//to simplify things
else
{
convertedTag = new ID3v24Tag(mp3tag);
}
//Set the primitive types specific to v2_2.
copyPrimitives(convertedTag);
//Set v2.2 Frames
copyFrames(convertedTag);
logger.info("Created tag from a tag of a different version");
}
}
/**
* Creates a new ID3v2_2 datatype.
*
* @param buffer
* @param loggingFilename
* @throws TagException
*/
public ID3v22Tag(ByteBuffer buffer,String loggingFilename)
throws TagException
{
setLoggingFilename(loggingFilename);
this.read(buffer);
}
/**
* Creates a new ID3v2_2 datatype.
*
* @param buffer
* @throws TagException
*
* @deprecated use {@link #ID3v22Tag(ByteBuffer,String)} instead
*/
public ID3v22Tag(ByteBuffer buffer)
throws TagException
{
this(buffer,"");
}
/**
*
*
* @return an indentifier of the tag type
*/
public String getIdentifier()
{
return "ID3v2_2.20";
}
/**
* Return frame size based upon the sizes of the frames rather than the size
* including padding recorded in the tag header
*
* @return size
*/
public int getSize()
{
int size = TAG_HEADER_LENGTH;
size += super.getSize();
return size;
}
/**
*
*
* @param obj
* @return equality
*/
public boolean equals(Object obj)
{
if ((obj instanceof ID3v22Tag) == false)
{
return false;
}
ID3v22Tag object = (ID3v22Tag) obj;
if (this.compression != object.compression)
{
return false;
}
if (this.unsynchronization != object.unsynchronization)
{
return false;
}
return super.equals(obj);
}
/**
* Read tag from the ByteBuffer
*
* @param byteBuffer to read the tag from
* @throws TagException
* @throws TagNotFoundException
*/
public void read(ByteBuffer byteBuffer)
throws TagException
{
int size;
if (seek(byteBuffer) == false)
{
throw new TagNotFoundException("ID3v2.20 tag not found");
}
logger.info(getLoggingFilename()+":"+"Reading tag from file");
//Read the flags
byte flags = byteBuffer.get();
unsynchronization = (flags & MASK_V22_UNSYNCHRONIZATION) != 0;
compression = (flags & MASK_V22_COMPRESSION) != 0;
if(unsynchronization)
{
logger.warning(getLoggingFilename()+":"+"ID3v22 Tag is unsynchronized");
}
if(compression)
{
logger.warning(getLoggingFilename()+":"+"ID3v22 Tag is compressed");
}
//TODO if compression bit set should we ignore
// Read the size
size = ID3SyncSafeInteger.bufferToValue(byteBuffer);
//Slice Buffer, so position markers tally with size (i.e do not include tagheader)
ByteBuffer bufferWithoutHeader = byteBuffer.slice();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -