FileDocCategorySizeDatePackage
Mp4TagByteField.javaAPI DocJaudiotagger 2.0.44898Wed Mar 30 16:12:10 BST 2011org.jaudiotagger.tag.mp4.field

Mp4TagByteField

public class Mp4TagByteField extends Mp4TagTextField
Represents a single byte as a number

Usually single byte fields are used as a boolean field, but not always so we dont do this conversion

Fields Summary
public static String
TRUE_VALUE
private int
realDataLength
private byte[]
bytedata
Constructors Summary
public Mp4TagByteField(org.jaudiotagger.tag.mp4.Mp4FieldKey id, String value)
Create new field

Assume length of 1 which is correct for most but not all byte fields

param
id
param
value is a String representation of a number
throws
org.jaudiotagger.tag.FieldDataInvalidException


                                        
          
    
        this(id, value, 1);
    
public Mp4TagByteField(org.jaudiotagger.tag.mp4.Mp4FieldKey id, String value, int realDataLength)
Create new field with known length

param
id
param
value is a String representation of a number
param
realDataLength
throws
org.jaudiotagger.tag.FieldDataInvalidException

        super(id.getFieldName(), value);
        this.realDataLength = realDataLength;
        //Check that can actually be stored numercially, otherwise will have big problems
        //when try and save the field
        try
        {
            Long.parseLong(value);
        }
        catch (NumberFormatException nfe)
        {
            throw new FieldDataInvalidException("Value of:" + value + " is invalid for field:" + id);
        }
    
public Mp4TagByteField(String id, ByteBuffer raw)
Construct from rawdata from audio file

param
id
param
raw
throws
UnsupportedEncodingException

        super(id, raw);
    
Methods Summary
protected voidbuild(java.nio.ByteBuffer data)

        //Data actually contains a 'Data' Box so process data using this
        Mp4BoxHeader header = new Mp4BoxHeader(data);
        Mp4DataBox databox = new Mp4DataBox(header, data);
        dataSize = header.getDataLength();
        //Needed for subsequent write
        realDataLength = dataSize - Mp4DataBox.PRE_DATA_LENGTH;
        bytedata = databox.getByteData();
        content = databox.getContent();

    
protected byte[]getDataBytes()
Return raw data bytes

TODO this code should be done better so generalised to any length

return
throws
UnsupportedEncodingException


        //Write original data
        if (bytedata != null)
        {
            return bytedata;
        }

        //new field, lets hope the realDataLength is correct
        switch (realDataLength)
        {
            case 2:
            {
                //Save as two bytes
                Short shortValue = new Short(content);
                byte rawData[] = Utils.getSizeBEInt16(shortValue);
                return rawData;
            }
            case 1:
            {
                //Save as 1 bytes
                Short shortValue = new Short(content);
                byte rawData[] = new byte[1];
                rawData[0] = shortValue.byteValue();
                return rawData;
            }
            case 4:
            {
                //Assume could be int
                Integer intValue = new Integer(content);
                byte rawData[] = Utils.getSizeBEInt32(intValue);
                return rawData;
            }
            default:
            {
                //TODO
                throw new RuntimeException(id + ":" + realDataLength + ":" + "Dont know how to write byte fields of this length");
            }
        }

    
public Mp4FieldTypegetFieldType()

        return Mp4FieldType.INTEGER;