FileDocCategorySizeDatePackage
SYLTID3V2Frame.javaAPI Docjid3 0.4618250Sun Feb 06 18:11:22 GMT 2005org.blinkenlights.jid3.v2

SYLTID3V2Frame

public class SYLTID3V2Frame extends ID3V2Frame
Frame containing synchronized lyrics/text.
author
paul

Fields Summary
private TextEncoding
m_oTextEncoding
private String
m_sLanguage
private TimestampFormat
m_oTimestampFormat
private ContentType
m_oContentType
private String
m_sContentDescriptor
private SortedMap
m_oSyncEntryMap
Constructors Summary
public SYLTID3V2Frame(String sLanguage, TimestampFormat oTimestampFormat, ContentType oContentType, String sContentDescriptor)
Creates a new instance of SYLTID3V2Frame.

param
sLanguage three letter language code for the content descriptor
param
oTimestampFormat the timestamp format used in this frame
param
oContentType the content for which synchronization is set
param
sContentDescriptor a unique text description for the synchronization details
throws
ID3Exception if language is not three characters, or if oTimestampFormat, oContentType or sContentDescriptor are null

    
                                                                      
            
         
    
        m_oTextEncoding = TextEncoding.getDefaultTextEncoding();
        if ((sLanguage == null) || (sLanguage.length() != 3))
        {
            throw new ID3Exception("A three character length language code is required in SYLT frame.");
        }
        m_sLanguage = sLanguage;
        if (oTimestampFormat == null)
        {
            throw new ID3Exception("Timestamp is required for SYLT frame.");
        }
        m_oTimestampFormat = oTimestampFormat;
        if (oContentType == null)
        {
            throw new ID3Exception("Content type is required for SYLT frame.");
        }
        m_oContentType = oContentType;
        if (sContentDescriptor == null)
        {
            throw new ID3Exception("Content descriptor is required for SYLT frame.");
        }
        m_sContentDescriptor = sContentDescriptor;
        // this is where sync entries go
        m_oSyncEntryMap = new TreeMap();
    
public SYLTID3V2Frame(InputStream oIS)

        try
        {
            ID3DataInputStream oFrameDataID3DIS = new ID3DataInputStream(oIS);

            // header
            m_oTextEncoding = TextEncoding.getTextEncoding(oFrameDataID3DIS.readUnsignedByte());
            byte[] abyLanguage = new byte[3];
            oFrameDataID3DIS.readFully(abyLanguage);
            m_sLanguage = new String(abyLanguage);
            m_oTimestampFormat = new SYLTID3V2Frame.TimestampFormat((byte)oFrameDataID3DIS.readUnsignedByte());
            m_oContentType = new SYLTID3V2Frame.ContentType((byte)oFrameDataID3DIS.readUnsignedByte());
            m_sContentDescriptor = oFrameDataID3DIS.readStringToNull(m_oTextEncoding);
            
            // sync entries
            m_oSyncEntryMap = new TreeMap();
            while (oFrameDataID3DIS.available() > 0)
            {
                String sText = oFrameDataID3DIS.readStringToNull(m_oTextEncoding);
                int iTimestamp = oFrameDataID3DIS.readBE32();
                
                m_oSyncEntryMap.put(new Integer(iTimestamp), sText);
            }
        }
        catch (Exception e)
        {
            throw new InvalidFrameID3Exception(e);
        }
    
Methods Summary
public voidaccept(ID3Visitor oID3Visitor)

        oID3Visitor.visitSYLTID3V2Frame(this);
    
public voidaddSyncEntry(org.blinkenlights.jid3.v2.SYLTID3V2Frame$SyncEntry oSyncEntry)
Add a sync entry to the frame.

param
oSyncEntry the sync entry to be added to this frame
throws
ID3Exception if oSyncEntry is null, or if a sync entry for this timestamp already exists in this frame

        if (oSyncEntry == null)
        {
            throw new ID3Exception("SyncEntry cannot be null.");
        }
        if (m_oSyncEntryMap.keySet().contains(new Integer(oSyncEntry.getTimestamp())))
        {
            throw new ID3Exception("SYLT frame already contains a sync entry for timestamp " + oSyncEntry.getTimestamp() + ".");
        }
        m_oSyncEntryMap.put(new Integer(oSyncEntry.getTimestamp()), oSyncEntry.getText());
    
public booleanequals(java.lang.Object oOther)

        if ((oOther == null) || (!(oOther instanceof SYLTID3V2Frame)))
        {
            return false;
        }
        
        SYLTID3V2Frame oOtherSYLT = (SYLTID3V2Frame)oOther;

        return (m_oTextEncoding.equals(oOtherSYLT.m_oTextEncoding) &&
                m_sLanguage.equals(oOtherSYLT.m_sLanguage) &&
                m_oTimestampFormat.equals(oOtherSYLT.m_oTimestampFormat) &&
                m_oContentType.equals(oOtherSYLT.m_oContentType) &&
                m_sContentDescriptor.equals(oOtherSYLT.m_sContentDescriptor) &&
                m_oSyncEntryMap.equals(oOtherSYLT.m_oSyncEntryMap));
    
public java.lang.StringgetContentDescriptor()
Get the content descriptor for this frame.

return
the content descriptor

        return m_sContentDescriptor;
    
public org.blinkenlights.jid3.v2.SYLTID3V2Frame$ContentTypegetContentType()
Get the content type.

return
the content type

        return m_oContentType;
    
protected byte[]getFrameId()

        return "SYLT".getBytes();
    
public java.lang.StringgetLanguage()
Get the language used in this frame.

return
a three letter language code

        return m_sLanguage;
    
public org.blinkenlights.jid3.v2.SYLTID3V2Frame$SyncEntrygetSyncEntry(int iTimestamp)
Get a sync entry from this frame.

param
iTimestamp the timestamp for which the sync entry should be returned
return
the sync entry object matching the specified timestamp, or null if no matching tiemstamp exists

        if (m_oSyncEntryMap.keySet().contains(new Integer(iTimestamp)))
        {
            try
            {
                return new SyncEntry((String)m_oSyncEntryMap.get(new Integer(iTimestamp)), iTimestamp);
            }
            catch (Exception e) { return null; }    // we've already created this object, so this can't happen
        }
        else
        {
            return null;
        }
    
public TextEncodinggetTextEncoding()
Get the text encoding used for the content descriptor and text in this frame.

return
the text encoding to be used for this frame

        return m_oTextEncoding;
    
public org.blinkenlights.jid3.v2.SYLTID3V2Frame$TimestampFormatgetTimestampFormat()
Get the timestamp format used.

return
the timestamp format

        return m_oTimestampFormat;
    
public int[]getTimestamps()
Get all timestamps for which entries have been set in this frame.

return
an array of ints, containing all of the timestamps

        int[] aiTimestamp = new int[m_oSyncEntryMap.keySet().size()];
        
        int i=0;
        Iterator oIter = m_oSyncEntryMap.keySet().iterator();
        while (oIter.hasNext())
        {
            aiTimestamp[i] = ((Integer)oIter.next()).intValue();
            i++;
        }
        
        return aiTimestamp;
    
public org.blinkenlights.jid3.v2.SYLTID3V2Frame$SyncEntryremoveSyncEntry(int iTimestamp)
Remove a sync entry from this frame.

param
iTimestamp the timestamp for which the sync entry is to be removed
return
the previously set sync entry for this timestamp, or null if no sync entry was set for this timestamp

        if (m_oSyncEntryMap.keySet().contains(new Integer(iTimestamp)))
        {
            try
            {
                return new SyncEntry((String)m_oSyncEntryMap.remove(new Integer(iTimestamp)), iTimestamp);
            }
            catch (Exception e) { return null; }    // we've already created this object, so this can't happen
        }
        else
        {
            return null;
        }
    
public voidsetContentDescriptor(java.lang.String sContentDescriptor)
Set the content descriptor for this frame.

param
sContentDescriptor the content descriptor for this frame
throws
ID3Exception if the content descriptor is null
throws
ID3Exception if this frame is in a tag with another SYLT frame which would have the same language and content descriptor

        String sOrigContentDescriptor = m_sContentDescriptor;
        
        if (sContentDescriptor == null)
        {
            throw new ID3Exception("Content descriptor is required for SYLT frame.");
        }

        m_sContentDescriptor = sContentDescriptor;
        
        // try this update, and reverse it if it generates and error
        try
        {
            notifyID3Observers();
        }
        catch (ID3Exception e)
        {
            m_sContentDescriptor = sOrigContentDescriptor;
            
            throw e;
        }
    
public voidsetContentType(org.blinkenlights.jid3.v2.SYLTID3V2Frame$ContentType oContentType)
Set the content type.

param
oContentType the content type for this frame

        m_oContentType = oContentType;
    
public voidsetLanguage(java.lang.String sLanguage)
Set the language used in this frame.

param
sLanguage a three letter language code
throws
ID3Exception if the language is null or not three characters in length or if this frame is in a tag which contains another SYLT frame with the same language and content descriptor
throws
ID3Exception if this frame is in a tag with another SYLT frame which would have the same language and content descriptor

        String sOrigLanguage = m_sLanguage;
        
        if ((sLanguage == null) || (sLanguage.length() != 3))
        {
            throw new ID3Exception("A three character length language code is required in SYLT frame.");
        }

        m_sLanguage = sLanguage;
        
        // try this update, and reverse it if it generates and error
        try
        {
            notifyID3Observers();
        }
        catch (ID3Exception e)
        {
            m_sLanguage = sOrigLanguage;
            
            throw e;
        }
    
public voidsetTextEncoding(TextEncoding oTextEncoding)
Set the text encoding to be used for the content descriptor and text in this frame.

param
oTextEncoding the text encoding to be used for this frame

        if (oTextEncoding == null)
        {
            throw new NullPointerException("Text encoding cannot be null.");
        }
        m_oTextEncoding = oTextEncoding;
    
public voidsetTimestampFormat(org.blinkenlights.jid3.v2.SYLTID3V2Frame$TimestampFormat oTimestampFormat)
Set the timestamp format used.

param
oTimestampFormat the timestamp format used in this frame

        m_oTimestampFormat = oTimestampFormat;
    
public java.lang.StringtoString()

        StringBuffer sbText = new StringBuffer();
        sbText.append("Synchronized lyrics/text: Language=[" + m_sLanguage + "], Timestamp format=[" + m_oTimestampFormat.getValue() +
               "], Content type=[" + m_oContentType.getValue() + "], Content descriptor=[" + m_sContentDescriptor + "]");
        Iterator oIter = m_oSyncEntryMap.keySet().iterator();
        while (oIter.hasNext())
        {
            Integer oTimestamp = (Integer)oIter.next();
            String sText = (String)m_oSyncEntryMap.get(oTimestamp);
            sbText.append(" SyncEntry(" + oTimestamp.intValue() + ", " + sText + ")");
        }
        
        return sbText.toString();
    
protected voidwriteBody(ID3DataOutputStream oIDOS)

        // header
        oIDOS.write(m_oTextEncoding.getEncodingValue()); // text encoding
        oIDOS.write(m_sLanguage.getBytes());    // language
        oIDOS.write(m_oTimestampFormat.getValue()); // timestamp format
        oIDOS.write(m_oContentType.getValue()); // content type
        oIDOS.write(m_sContentDescriptor.getBytes(m_oTextEncoding.getEncodingString()));   // content descriptor
        // null after content descriptor
        if (m_oTextEncoding.equals(TextEncoding.ISO_8859_1))
        {
            oIDOS.writeUnsignedByte(0);
        }
        else
        {
            oIDOS.writeUnsignedByte(0);
            oIDOS.writeUnsignedByte(0);
        }
        // sync entries
        Iterator oIter = m_oSyncEntryMap.keySet().iterator();
        while (oIter.hasNext())
        {
            Integer oTimestamp = (Integer)oIter.next();
            String sText = (String)m_oSyncEntryMap.get(oTimestamp);
            oIDOS.write(sText.getBytes(m_oTextEncoding.getEncodingString()));  // sync text
            // null after sync text
            if (m_oTextEncoding.equals(TextEncoding.ISO_8859_1))
            {
                oIDOS.writeUnsignedByte(0);
            }
            else
            {
                oIDOS.writeUnsignedByte(0);
                oIDOS.writeUnsignedByte(0);
            }
            oIDOS.writeBE32(oTimestamp.intValue()); // timestamp
        }