Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4TagByteField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4TagByteField
82%
28/34
57%
4/7
2.833
 
 1  
 package org.jaudiotagger.tag.mp4.field;
 2  
 
 3  
 import org.jaudiotagger.audio.generic.Utils;
 4  
 import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader;
 5  
 import org.jaudiotagger.tag.FieldDataInvalidException;
 6  
 import org.jaudiotagger.tag.mp4.Mp4FieldKey;
 7  
 import org.jaudiotagger.tag.mp4.atom.Mp4DataBox;
 8  
 
 9  
 import java.io.UnsupportedEncodingException;
 10  
 import java.nio.ByteBuffer;
 11  
 
 12  
 /**
 13  
  * Represents a single byte as a number
 14  
  * <p/>
 15  
  * <p>Usually single byte fields are used as a boolean field, but not always so we dont do this conversion
 16  
  */
 17  
 public class Mp4TagByteField extends Mp4TagTextField
 18  
 {
 19  4
     public static String TRUE_VALUE="1";  //when usimng this field to hold a boolean
 20  
 
 21  
     //Holds the actual size of the data content as held in the databoxitem, this is required when creating new
 22  
     //items because we cant accurately work out the size by looking at the content because sometimes field must be longer
 23  
     //than is actually required to hold the value
 24  
     //e.g byte data length seems to be 1 for pgap and cpil but 2 for tmpo, so we stored the dataSize
 25  
     //when we loaded the value so if greater than 1 we pad the value.
 26  
     private int realDataLength;
 27  
 
 28  
     //Preserved from data from file
 29  
     private byte[] bytedata;
 30  
 
 31  
     /**
 32  
      * Create new field
 33  
      * <p/>
 34  
      * Assume length of 1 which is correct for most but not all byte fields
 35  
      *
 36  
      * @param id
 37  
      * @param value is a String representation of a number
 38  
      * @throws org.jaudiotagger.tag.FieldDataInvalidException
 39  
      */
 40  
     public Mp4TagByteField(Mp4FieldKey id, String value) throws FieldDataInvalidException
 41  
     {
 42  0
         this(id, value, 1);
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Create new field with known length
 47  
      *
 48  
      * @param id
 49  
      * @param value is a String representation of a number
 50  
      * @param realDataLength
 51  
      * @throws org.jaudiotagger.tag.FieldDataInvalidException
 52  
      */
 53  
     public Mp4TagByteField(Mp4FieldKey id, String value, int realDataLength) throws FieldDataInvalidException
 54  
     {
 55  40
         super(id.getFieldName(), value);
 56  40
         this.realDataLength = realDataLength;
 57  
         //Check that can actually be stored numercially, otherwise will have big problems
 58  
         //when try and save the field
 59  
         try
 60  
         {
 61  40
             Long.parseLong(value);
 62  
         }
 63  1
         catch (NumberFormatException nfe)
 64  
         {
 65  1
             throw new FieldDataInvalidException("Value of:" + value + " is invalid for field:" + id);
 66  39
         }
 67  39
     }
 68  
 
 69  
     /**
 70  
      * Construct from rawdata from audio file
 71  
      *
 72  
      * @param id
 73  
      * @param raw
 74  
      * @throws UnsupportedEncodingException
 75  
      */
 76  
     public Mp4TagByteField(String id, ByteBuffer raw) throws UnsupportedEncodingException
 77  
     {
 78  1103
         super(id, raw);
 79  1103
     }
 80  
 
 81  
     public Mp4FieldType getFieldType()
 82  
     {
 83  519
         return Mp4FieldType.INTEGER;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Return raw data bytes
 88  
      * <p/>
 89  
      * TODO this code should be done better so generalised to any length
 90  
      *
 91  
      * @return
 92  
      * @throws UnsupportedEncodingException
 93  
      */
 94  
     protected byte[] getDataBytes() throws UnsupportedEncodingException
 95  
     {
 96  
 
 97  
         //Write original data
 98  519
         if (bytedata != null)
 99  
         {
 100  480
             return bytedata;
 101  
         }
 102  
 
 103  
         //new field, lets hope the realDataLength is correct
 104  39
         switch (realDataLength)
 105  
         {
 106  
             case 2:
 107  
             {
 108  
                 //Save as two bytes
 109  32
                 Short shortValue = new Short(content);
 110  32
                 byte rawData[] = Utils.getSizeBEInt16(shortValue);
 111  32
                 return rawData;
 112  
             }
 113  
             case 1:
 114  
             {
 115  
                 //Save as 1 bytes
 116  7
                 Short shortValue = new Short(content);
 117  7
                 byte rawData[] = new byte[1];
 118  7
                 rawData[0] = shortValue.byteValue();
 119  7
                 return rawData;
 120  
             }
 121  
             case 4:
 122  
             {
 123  
                 //Assume could be int
 124  0
                 Integer intValue = new Integer(content);
 125  0
                 byte rawData[] = Utils.getSizeBEInt32(intValue);
 126  0
                 return rawData;
 127  
             }
 128  
             default:
 129  
             {
 130  
                 //TODO
 131  0
                 throw new RuntimeException(id + ":" + realDataLength + ":" + "Dont know how to write byte fields of this length");
 132  
             }
 133  
         }
 134  
 
 135  
     }
 136  
 
 137  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 138  
     {
 139  
         //Data actually contains a 'Data' Box so process data using this
 140  1103
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 141  1103
         Mp4DataBox databox = new Mp4DataBox(header, data);
 142  1103
         dataSize = header.getDataLength();
 143  
         //Needed for subsequent write
 144  1103
         realDataLength = dataSize - Mp4DataBox.PRE_DATA_LENGTH;
 145  1103
         bytedata = databox.getByteData();
 146  1103
         content = databox.getContent();
 147  
 
 148  1103
     }
 149  
 }