FileDocCategorySizeDatePackage
APICID3V2Frame.javaAPI Docjid3 0.4614600Sun Feb 06 18:11:17 GMT 2005org.blinkenlights.jid3.v2

APICID3V2Frame

public class APICID3V2Frame extends ID3V2Frame
author
paul Frame containing an attached picture.

Fields Summary
private TextEncoding
m_oTextEncoding
private String
m_sMimeType
private PictureType
m_oPictureType
private String
m_sDescription
private byte[]
m_abyPictureData
Constructors Summary
public APICID3V2Frame(String sMimeType, PictureType oPictureType, String sDescription, byte[] abyPictureData)
Constructor. Note: It is valid to set the MIME type to "-->", and set the picture data to an URL pointing to the image, although this is discouraged.

param
sMimeType the valid MIME type (ie. image/png) describing the format of the contained image (the default if null is specified is "image/")
param
oPictureType the classification of the picture attached
param
sDescription an optional description of the image, or null if no description required
param
abyPictureData the data content of the image
throws
ID3Exception if the description is longer than 64 characters
throws
ID3Exception if the picture data is null, or zero length


                                                                                                                                      
      
                           
                           
                           
         
    
        m_oTextEncoding = TextEncoding.getDefaultTextEncoding();
        m_sMimeType = sMimeType;
        if (m_sMimeType == null)
        {
            m_sMimeType = "image/";
        }
        m_oPictureType = oPictureType;
        if (sDescription.length() > 64)
        {
            // I have no idea why...
            throw new ID3Exception("Description in APIC frame cannot exceed 64 characters.");
        }
        m_sDescription = sDescription;
        if ((abyPictureData == null) || (abyPictureData.length == 0))
        {
            throw new ID3Exception("APIC frame requires picture data.");
        }
        m_abyPictureData = abyPictureData;
    
public APICID3V2Frame(InputStream oIS)

        // Parse out the text encoding and text string from the raw data
        try
        {
            ID3DataInputStream oFrameDataID3DIS = new ID3DataInputStream(oIS);
            
            // text encoding
            m_oTextEncoding = TextEncoding.getTextEncoding(oFrameDataID3DIS.readUnsignedByte());
            
            // mime type (read to null)
            ByteArrayOutputStream oMimeTypeBAOS = new ByteArrayOutputStream();
            int iMimeTypeByte;
            do
            {
                iMimeTypeByte = oFrameDataID3DIS.readUnsignedByte();
                if (iMimeTypeByte != 0)
                {
                    oMimeTypeBAOS.write(iMimeTypeByte);
                }
            }
            while (iMimeTypeByte != 0);
            if (oMimeTypeBAOS.size() > 0)
            {
                byte[] abyMimeType = oMimeTypeBAOS.toByteArray();
                m_sMimeType = new String(abyMimeType);
            }
            
            // picture type
            m_oPictureType = new APICID3V2Frame.PictureType((byte)oFrameDataID3DIS.readUnsignedByte());
            
            // description (read to null)
            m_sDescription = oFrameDataID3DIS.readStringToNull(m_oTextEncoding);
            
            // picture data
            m_abyPictureData = new byte[oFrameDataID3DIS.available()];
            oFrameDataID3DIS.readFully(m_abyPictureData);
        }
        catch (Exception e)
        {
            throw new InvalidFrameID3Exception(e);
        }
    
Methods Summary
public voidaccept(ID3Visitor oID3Visitor)

        oID3Visitor.visitAPICID3V2Frame(this);
    
public booleanequals(java.lang.Object oOther)

        if ((oOther == null) || (!(oOther instanceof APICID3V2Frame)))
        {
            return false;
        }
        
        APICID3V2Frame oOtherAPIC = (APICID3V2Frame)oOther;
        
        return (m_oTextEncoding.equals(oOtherAPIC.m_oTextEncoding) &&
                m_sMimeType.equals(oOtherAPIC.m_sMimeType) &&
                m_oPictureType.equals(oOtherAPIC.m_oPictureType) &&
                m_sDescription.equals(oOtherAPIC.m_sDescription) &&
                Arrays.equals(m_abyPictureData, oOtherAPIC.m_abyPictureData));
    
public java.lang.StringgetDescription()
Get the description for the picture in this frame.

return
the set description, or null if no description has been defined

        return m_sDescription;
    
protected byte[]getFrameId()

        return "APIC".getBytes();
    
public java.lang.StringgetMimeType()
Get the MIME type of the image contained in this frame. A MIME type of "-->" implies that the image data contains an URL reference to the actual image data.

return
the specified MIME type

        return m_sMimeType;
    
public byte[]getPictureData()
Get the picture data for the image in this frame.

return
the picture data for the image

        return m_abyPictureData;
    
public org.blinkenlights.jid3.v2.APICID3V2Frame$PictureTypegetPictureType()
Get the classification of the picture in this frame.

return
the type of the picture in this frame

        return m_oPictureType;
    
public TextEncodinggetTextEncoding()
Get the text encoding used for the description in this frame.

return
the text encoding to be used for this frame

        return m_oTextEncoding;
    
public voidsetDescription(java.lang.String sDescription)
Set the description for the picture in this frame.

param
sDescription an optional description of the image, or null if no description required
throws
ID3Exception if the description is longer than 64 characters
throws
ID3Exception if this frame is in a tag with another APIC frame which would have the same description

        String sOrigDescription = m_sDescription;
        TextEncoding oOrigTextEncoding = m_oTextEncoding;
        
        if (sDescription.length() > 64)
        {
            // I have no idea why...
            throw new ID3Exception("Description in APIC frame cannot exceed 64 characters.");
        }
        m_oTextEncoding = TextEncoding.getDefaultTextEncoding();
        m_sDescription = sDescription;
        
        // try this update, and reverse it if it generates and error
        try
        {
            notifyID3Observers();
        }
        catch (ID3Exception e)
        {
            m_sDescription = sOrigDescription;
            m_oTextEncoding = oOrigTextEncoding;
            
            throw e;
        }
    
public voidsetMimeType(java.lang.String sMimeType)
Set the MIME type for the image contained in this frame. Note: It is valid to set the MIME type to "-->", and set the picture data to an URL pointing to the image, although this is discouraged.

param
sMimeType the valid MIME type (ie. image/png) describing the format of the contained image (the default if null is specified is "image/")

        m_sMimeType = sMimeType;
        if (m_sMimeType == null)
        {
            m_sMimeType = "image/";
        }
    
public voidsetPictureData(byte[] abyPictureData)
Set the picture data for the image in this frame.

param
abyPictureData the data content of the image
throws
ID3Exception if the picture data is null, or zero length

        if ((abyPictureData == null) || (abyPictureData.length == 0))
        {
            throw new ID3Exception("APIC frame requires picture data.");
        }
        m_abyPictureData = abyPictureData;
    
public voidsetPictureType(org.blinkenlights.jid3.v2.APICID3V2Frame$PictureType oPictureType)
Set the classification of the picture in this frame.

param
oPictureType the type of the picture in this frame.

        m_oPictureType = oPictureType;
    
public voidsetTextEncoding(TextEncoding oTextEncoding)
Set the text encoding to be used for the description 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 java.lang.StringtoString()

        return "Attached picure: Mime type=[" + m_sMimeType + "], Picture type = " +
               m_oPictureType.getValue() + ", Description=[" + m_sDescription + "], Picture data length = " +
               m_abyPictureData.length;
    
protected voidwriteBody(ID3DataOutputStream oIDOS)

        // text encoding
        oIDOS.writeUnsignedByte(m_oTextEncoding.getEncodingValue());
        // mime type (and trailing null)
        oIDOS.write(m_sMimeType.getBytes());
        oIDOS.writeUnsignedByte(0);
        // picture type
        oIDOS.writeUnsignedByte(m_oPictureType.getValue());
        // description
        if (m_sDescription != null)
        {
            oIDOS.write(m_sDescription.getBytes(m_oTextEncoding.getEncodingString()));
        }
        // null separating description from picture data
        if (m_oTextEncoding.equals(TextEncoding.ISO_8859_1))
        {
            oIDOS.writeUnsignedByte(0);
        }
        else
        {
            oIDOS.writeUnsignedByte(0);
            oIDOS.writeUnsignedByte(0);
        }
        // actual picture data
        oIDOS.write(m_abyPictureData);