Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4TrackField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4TrackField
50%
29/57
40%
2/5
2.111
 
 1  
 package org.jaudiotagger.tag.mp4.field;
 2  
 
 3  
 import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader;
 4  
 import org.jaudiotagger.tag.FieldDataInvalidException;
 5  
 import org.jaudiotagger.tag.mp4.Mp4FieldKey;
 6  
 import org.jaudiotagger.tag.mp4.atom.Mp4DataBox;
 7  
 
 8  
 import java.io.UnsupportedEncodingException;
 9  
 import java.nio.ByteBuffer;
 10  
 import java.util.ArrayList;
 11  
 
 12  
 /**
 13  
  * Represents the Track No field
 14  
  * <p/>
 15  
  * <p>There are a number of reseved fields makeing matters more complicated
 16  
  * Reserved:2 bytes
 17  
  * Track Number:2 bytes
 18  
  * No of Tracks:2 bytes (or zero if not known)
 19  
  * PlayListTitleReserved: 1 byte
 20  
  * playtitlenameReserved:0 bytes
 21  
  * </p>
 22  
  */
 23  
 public class Mp4TrackField extends Mp4TagTextNumberField
 24  
 {
 25  
     private static final int NONE_VALUE_INDEX = 0;
 26  
     private static final int TRACK_NO_INDEX = 1;
 27  
     private static final int TRACK_TOTAL_INDEX = 2;
 28  
     private static final int NONE_END_VALUE_INDEX = 3;
 29  
 
 30  
     /**
 31  
      * Create new Track Field parsing the String for the trackno/total
 32  
      *
 33  
      * @param trackValue
 34  
      * @throws org.jaudiotagger.tag.FieldDataInvalidException
 35  
      */
 36  
     public Mp4TrackField(String trackValue) throws FieldDataInvalidException
 37  
     {
 38  0
         super(Mp4FieldKey.TRACK.getFieldName(), trackValue);
 39  
 
 40  0
         numbers = new ArrayList<Short>();
 41  0
         numbers.add(new Short("0"));
 42  
 
 43  0
         String values[] = trackValue.split("/");
 44  0
         switch (values.length)
 45  
         {
 46  
             case 1:
 47  
                 try
 48  
                 {
 49  0
                     numbers.add(Short.parseShort(values[0]));
 50  
                 }
 51  0
                 catch (NumberFormatException nfe)
 52  
                 {
 53  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 54  0
                 }
 55  0
                 numbers.add(new Short("0"));
 56  0
                 numbers.add(new Short("0"));
 57  0
                 break;
 58  
 
 59  
             case 2:
 60  
                 try
 61  
                 {
 62  0
                     numbers.add(Short.parseShort(values[0]));
 63  
                 }
 64  0
                 catch (NumberFormatException nfe)
 65  
                 {
 66  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 67  0
                 }
 68  
                 try
 69  
                 {
 70  0
                     numbers.add(Short.parseShort(values[1]));
 71  
                 }
 72  0
                 catch (NumberFormatException nfe)
 73  
                 {
 74  0
                     throw new FieldDataInvalidException("Value of:" + values[1] + " is invalid for field:" + id);
 75  0
                 }
 76  0
                 numbers.add(new Short("0"));
 77  0
                 break;
 78  
 
 79  
             default:
 80  0
                 throw new FieldDataInvalidException("Value is invalid for field:" + id);
 81  
         }
 82  0
     }
 83  
 
 84  
 
 85  
     /**
 86  
      * Create new Track Field with only track No
 87  
      *
 88  
      * @param trackNo
 89  
      */
 90  
     public Mp4TrackField(int trackNo)
 91  
     {
 92  
 
 93  8
         super(Mp4FieldKey.TRACK.getFieldName(), String.valueOf(trackNo));
 94  8
         numbers = new ArrayList<Short>();
 95  8
         numbers.add(new Short("0"));
 96  8
         numbers.add((short) trackNo);
 97  8
         numbers.add(new Short("0"));
 98  8
         numbers.add(new Short("0"));
 99  8
         System.out.println("Created field"+trackNo);
 100  8
     }
 101  
 
 102  
     /**
 103  
      * Create new Track Field with track No and total tracks
 104  
      *
 105  
      * @param trackNo
 106  
      * @param total
 107  
      */
 108  
     public Mp4TrackField(int trackNo, int total)
 109  
     {
 110  28
         super(Mp4FieldKey.TRACK.getFieldName(), String.valueOf(trackNo));
 111  28
         numbers = new ArrayList<Short>();
 112  28
         numbers.add(new Short("0"));
 113  28
         numbers.add((short) trackNo);
 114  28
         numbers.add((short) total);
 115  28
         numbers.add(new Short("0"));
 116  28
     }
 117  
 
 118  
     /**
 119  
      * Construct from filedata
 120  
      *
 121  
      * @param id
 122  
      * @param data
 123  
      * @throws UnsupportedEncodingException
 124  
      */
 125  
     public Mp4TrackField(String id, ByteBuffer data) throws UnsupportedEncodingException
 126  
     {
 127  235
         super(id, data);
 128  235
     }
 129  
 
 130  
 
 131  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 132  
     {
 133  
         //Data actually contains a 'Data' Box so process data using this
 134  235
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 135  235
         Mp4DataBox databox = new Mp4DataBox(header, data);
 136  235
         dataSize = header.getDataLength();
 137  235
         numbers = databox.getNumbers();
 138  
 
 139  
         //Track number always hold three values, we can discard the first one, the second one is the track no
 140  
         //and the third is the total no of tracks so only use if not zero
 141  235
         StringBuffer sb = new StringBuffer();
 142  235
         sb.append(numbers.get(TRACK_NO_INDEX));
 143  235
         if (numbers.get(TRACK_TOTAL_INDEX) > 0)
 144  
         {
 145  217
             sb.append("/").append(numbers.get(TRACK_TOTAL_INDEX));
 146  
         }
 147  235
         content = sb.toString();
 148  235
     }
 149  
 
 150  
     /**
 151  
      * @return
 152  
      */
 153  
     public Short getTrackNo()
 154  
     {
 155  258
         return numbers.get(TRACK_NO_INDEX);
 156  
     }
 157  
 
 158  
     /**
 159  
      * @return
 160  
      */
 161  
     public Short getTrackTotal()
 162  
     {
 163  168
         return numbers.get(TRACK_TOTAL_INDEX);
 164  
     }
 165  
 
 166  
      /**
 167  
      * Set Track No
 168  
      *
 169  
      * @param trackNo
 170  
      */
 171  
     public void setTrackNo(int trackNo)
 172  
     {
 173  0
         numbers.set(TRACK_NO_INDEX, (short) trackNo);
 174  0
     }
 175  
 
 176  
 
 177  
     /**
 178  
      * Set total number of tracks
 179  
      *
 180  
      * @param trackTotal
 181  
      */
 182  
     public void setTrackTotal(int trackTotal)
 183  
     {
 184  0
        numbers.set(TRACK_TOTAL_INDEX, (short) trackTotal);
 185  0
     }
 186  
 }