📄 id3v24frame.java
字号:
//Write Frame Size based on size of body buffer (if it has been unsynced then it size
//will have increased accordingly
int size = bodyBuffer.length;
logger.fine("Frame Size Is:" + size);
headerBuffer.put(ID3SyncSafeInteger.valueToBuffer(size));
//Write the Flags
//Status Flags:leave as they were when we read
headerBuffer.put(statusFlags.getWriteFlags());
//Enclosing Flags, first reset
encodingFlags.resetFlags();
//Encoding we only support unsynchrnization
if(unsynchronization)
{
((ID3v24Frame.EncodingFlags)encodingFlags).setUnsynchronised();
}
headerBuffer.put(encodingFlags.getFlags());
try
{
//Add header to the Byte Array Output Stream
tagBuffer.write(headerBuffer.array());
//Add bodybuffer to the Byte Array Output Stream
tagBuffer.write(bodyBuffer);
}
catch(IOException ioe)
{
//This could never happen coz not writing to file, so convert to RuntimeException
throw new RuntimeException(ioe);
}
}
/**
* Get Status Flags Object
*/
protected AbstractID3v2Frame.StatusFlags getStatusFlags()
{
return statusFlags;
}
/**
* Get Encoding Flags Object
*/
protected AbstractID3v2Frame.EncodingFlags getEncodingFlags()
{
return encodingFlags;
}
/**
* Member Class This represents a frame headers Status Flags
* Make adjustments if necessary based on frame type and specification.
*/
class StatusFlags
extends AbstractID3v2Frame.StatusFlags
{
public static final String TYPE_TAGALTERPRESERVATION = "typeTagAlterPreservation";
public static final String TYPE_FILEALTERPRESERVATION = "typeFileAlterPreservation";
public static final String TYPE_READONLY = "typeReadOnly";
/**
* Discard frame if tag altered
*/
public static final int MASK_TAG_ALTER_PRESERVATION = FileConstants.BIT6;
/**
* Discard frame if audio part of file altered
*/
public static final int MASK_FILE_ALTER_PRESERVATION = FileConstants.BIT5;
/**
* Frame tagged as read only
*/
public static final int MASK_READ_ONLY = FileConstants.BIT4;
/**
* Use this when creating a frame from scratch
*/
StatusFlags()
{
super();
}
/**
* Use this constructor when reading from file or from another v4 frame
*/
StatusFlags(byte flags)
{
originalFlags = flags;
writeFlags = flags;
modifyFlags();
}
/**
* Use this constructor when convert a v23 frame
*/
StatusFlags(ID3v23Frame.StatusFlags statusFlags)
{
originalFlags = convertV3ToV4Flags(statusFlags.getOriginalFlags());
writeFlags = originalFlags;
modifyFlags();
}
/**
* Convert V3 Flags to equivalent V4 Flags
*/
private byte convertV3ToV4Flags(byte v3Flag)
{
byte v4Flag = (byte) 0;
if ((v3Flag & ID3v23Frame.StatusFlags.MASK_FILE_ALTER_PRESERVATION) != 0)
{
v4Flag |= (byte) MASK_FILE_ALTER_PRESERVATION;
}
if ((v3Flag & ID3v23Frame.StatusFlags.MASK_TAG_ALTER_PRESERVATION) != 0)
{
v4Flag |= (byte) MASK_TAG_ALTER_PRESERVATION;
}
return v4Flag;
}
/**
* Makes modifications to flags based on specification and frameid
*/
protected void modifyFlags()
{
String str = getIdentifier();
if (ID3v24Frames.getInstanceOf().isDiscardIfFileAltered(str) == true)
{
writeFlags |= (byte) MASK_FILE_ALTER_PRESERVATION;
writeFlags &= (byte) ~MASK_TAG_ALTER_PRESERVATION;
}
else
{
writeFlags &= (byte) ~MASK_FILE_ALTER_PRESERVATION;
writeFlags &= (byte) ~MASK_TAG_ALTER_PRESERVATION;
}
}
public void createStructure()
{
MP3File.getStructureFormatter().openHeadingElement(TYPE_FLAGS, "");
MP3File.getStructureFormatter().addElement(TYPE_TAGALTERPRESERVATION, originalFlags & MASK_TAG_ALTER_PRESERVATION);
MP3File.getStructureFormatter().addElement(TYPE_FILEALTERPRESERVATION, originalFlags & MASK_FILE_ALTER_PRESERVATION);
MP3File.getStructureFormatter().addElement(TYPE_READONLY, originalFlags & MASK_READ_ONLY);
MP3File.getStructureFormatter().closeHeadingElement(TYPE_FLAGS);
}
}
/**
* This represents a frame headers Encoding Flags
*/
class EncodingFlags
extends AbstractID3v2Frame.EncodingFlags
{
public static final String TYPE_COMPRESSION = "compression";
public static final String TYPE_ENCRYPTION = "encryption";
public static final String TYPE_GROUPIDENTITY = "groupidentity";
public static final String TYPE_FRAMEUNSYNCHRONIZATION = "frameUnsynchronisation";
public static final String TYPE_DATALENGTHINDICATOR = "dataLengthIndicator";
/**
* Frame is part of a group
*/
public static final int MASK_GROUPING_IDENTITY = FileConstants.BIT6;
/**
* Frame is compressed
*/
public static final int MASK_COMPRESSION = FileConstants.BIT3;
/**
* Frame is encrypted
*/
public static final int MASK_ENCRYPTION = FileConstants.BIT2;
/**
* Unsynchronisation
*/
public static final int MASK_FRAME_UNSYNCHRONIZATION = FileConstants.BIT1;
/**
* Length
*/
public static final int MASK_DATA_LENGTH_INDICATOR = FileConstants.BIT0;
/**
* Use this when creating a frame from scratch
*/
EncodingFlags()
{
super();
}
/**
* Use this when creating a frame from existing flags in another v4 frame
*/
EncodingFlags(byte flags)
{
super(flags);
logEnabledFlags();
}
public void logEnabledFlags()
{
if (isCompression())
{
logger.warning(getLoggingFilename()+":"+identifier+" is compressed");
}
if (isEncryption())
{
logger.warning(getLoggingFilename()+":"+identifier+" is encrypted");
}
if (isGrouping())
{
logger.warning(getLoggingFilename()+":"+identifier+" is grouped");
}
if (isUnsynchronised())
{
logger.warning(getLoggingFilename()+":"+identifier+" is unsynchronised");
}
if (isDataLengthIndicator())
{
logger.warning(getLoggingFilename()+":"+identifier+" has a data length indicator");
}
}
public byte getFlags()
{
return flags;
}
public boolean isCompression()
{
return (flags & MASK_COMPRESSION) >0;
}
public boolean isEncryption()
{
return (flags & MASK_ENCRYPTION) >0;
}
public boolean isGrouping()
{
return (flags & MASK_GROUPING_IDENTITY) >0;
}
public boolean isUnsynchronised()
{
return (flags & MASK_FRAME_UNSYNCHRONIZATION) >0;
}
public boolean isDataLengthIndicator()
{
return (flags & MASK_DATA_LENGTH_INDICATOR) >0;
}
public void setUnsynchronised()
{
flags |= MASK_FRAME_UNSYNCHRONIZATION;
}
public void createStructure()
{
MP3File.getStructureFormatter().openHeadingElement(TYPE_FLAGS, "");
MP3File.getStructureFormatter().addElement(TYPE_COMPRESSION, flags & MASK_COMPRESSION);
MP3File.getStructureFormatter().addElement(TYPE_ENCRYPTION, flags & MASK_ENCRYPTION);
MP3File.getStructureFormatter().addElement(TYPE_GROUPIDENTITY, flags & MASK_GROUPING_IDENTITY);
MP3File.getStructureFormatter().addElement(TYPE_FRAMEUNSYNCHRONIZATION, flags & MASK_FRAME_UNSYNCHRONIZATION);
MP3File.getStructureFormatter().addElement(TYPE_DATALENGTHINDICATOR, flags & MASK_DATA_LENGTH_INDICATOR);
MP3File.getStructureFormatter().closeHeadingElement(TYPE_FLAGS);
}
}
/**
* Does the frame identifier meet the syntax for a idv3v2 frame identifier.
* must start with a capital letter and only contain capital letters and numbers
*
* @param identifier to be checked
* @return whether the identifier is valid
*/
public boolean isValidID3v2FrameIdentifier(String identifier)
{
Matcher m = validFrameIdentifier.matcher(identifier);
return m.matches();
}
/**
* Return String Representation of body
*/
public void createStructure()
{
MP3File.getStructureFormatter().openHeadingElement(TYPE_FRAME, getIdentifier());
MP3File.getStructureFormatter().addElement(TYPE_FRAME_SIZE, frameSize);
statusFlags.createStructure();
encodingFlags.createStructure();
frameBody.createStructure();
MP3File.getStructureFormatter().closeHeadingElement(TYPE_FRAME);
}
/**
*
* @return true if considered a common frame
*/
public boolean isCommon()
{
return ID3v24Frames.getInstanceOf().isCommon(getId());
}
/**
*
* @return true if considered a common frame
*/
public boolean isBinary()
{
return ID3v24Frames.getInstanceOf().isBinary(getId());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -