Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4TrackField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4TrackField
50%
26/52
60%
3/5
0
 
 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>For some reason uses an array of four numbers, but only the middle two are of use for display purposes
 16  
  */
 17  
 public class Mp4TrackField extends Mp4TagTextNumberField
 18  
 {
 19  
     private static final int NONE_VALUE_INDEX = 0;
 20  
     private static final int TRACK_NO_INDEX = 1;
 21  
     private static final int TRACK_TOTAL_INDEX = 2;
 22  
     private static final int NONE_END_VALUE_INDEX = 3;
 23  
 
 24  
     /**
 25  
      * Create new Track Field parsing the String for the trackno/total
 26  
      *
 27  
      * @param trackValue
 28  
      */
 29  
     public Mp4TrackField(String trackValue) throws FieldDataInvalidException
 30  
     {
 31  2
         super(Mp4FieldKey.TRACK.getFieldName(), trackValue);
 32  
 
 33  2
         numbers = new ArrayList<Short>();
 34  2
         numbers.add(new Short("0"));
 35  
 
 36  2
         String values[] = trackValue.split("/");
 37  2
         switch (values.length)
 38  
         {
 39  
             case 1:
 40  
                 try
 41  
                 {
 42  0
                     numbers.add(Short.parseShort(values[0]));
 43  
                 }
 44  0
                 catch (NumberFormatException nfe)
 45  
                 {
 46  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 47  0
                 }
 48  0
                 numbers.add(new Short("0"));
 49  0
                 numbers.add(new Short("0"));
 50  0
                 break;
 51  
 
 52  
             case 2:
 53  
                 try
 54  
                 {
 55  2
                     numbers.add(Short.parseShort(values[0]));
 56  
                 }
 57  0
                 catch (NumberFormatException nfe)
 58  
                 {
 59  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 60  2
                 }
 61  
                 try
 62  
                 {
 63  2
                     numbers.add(Short.parseShort(values[1]));
 64  
                 }
 65  0
                 catch (NumberFormatException nfe)
 66  
                 {
 67  0
                     throw new FieldDataInvalidException("Value of:" + values[1] + " is invalid for field:" + id);
 68  2
                 }
 69  2
                 numbers.add(new Short("0"));
 70  2
                 break;
 71  
 
 72  
             default:
 73  0
                 throw new FieldDataInvalidException("Value is invalid for field:" + id);
 74  
         }
 75  2
     }
 76  
 
 77  
 
 78  
     /**
 79  
      * Create new Track Field with only track No
 80  
      *
 81  
      * @param trackNo
 82  
      */
 83  
     public Mp4TrackField(int trackNo)
 84  
     {
 85  0
         super(Mp4FieldKey.TRACK.getFieldName(), String.valueOf(trackNo));
 86  0
         numbers = new ArrayList<Short>();
 87  0
         numbers.add(new Short("0"));
 88  0
         numbers.add((short) trackNo);
 89  0
         numbers.add(new Short("0"));
 90  0
         numbers.add(new Short("0"));
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Create new Track Field with track No and total tracks
 95  
      *
 96  
      * @param trackNo
 97  
      * @param total
 98  
      */
 99  
     public Mp4TrackField(int trackNo, int total)
 100  
     {
 101  0
         super(Mp4FieldKey.TRACK.getFieldName(), String.valueOf(trackNo));
 102  0
         numbers = new ArrayList<Short>();
 103  0
         numbers.add(new Short("0"));
 104  0
         numbers.add((short) trackNo);
 105  0
         numbers.add((short) total);
 106  0
         numbers.add(new Short("0"));
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Construct from filedata
 111  
      *
 112  
      * @param id
 113  
      * @param data
 114  
      * @throws UnsupportedEncodingException
 115  
      */
 116  
     public Mp4TrackField(String id, ByteBuffer data) throws UnsupportedEncodingException
 117  
     {
 118  69
         super(id, data);
 119  69
     }
 120  
 
 121  
 
 122  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 123  
     {
 124  
         //Data actually contains a 'Data' Box so process data using this
 125  69
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 126  69
         Mp4DataBox databox = new Mp4DataBox(header, data);
 127  69
         dataSize = header.getDataLength();
 128  69
         numbers = databox.getNumbers();
 129  
 
 130  
         //Track number always hold three values, we can discard the first one, the second one is the track no
 131  
         //and the third is the total no of tracks so only use if not zero
 132  69
         StringBuffer sb = new StringBuffer();
 133  69
         sb.append(numbers.get(TRACK_NO_INDEX));
 134  69
         if (numbers.get(TRACK_TOTAL_INDEX) > 0)
 135  
         {
 136  57
             sb.append("/" + numbers.get(TRACK_TOTAL_INDEX));
 137  
         }
 138  69
         content = sb.toString();
 139  69
     }
 140  
 
 141  
     /**
 142  
      * @return
 143  
      */
 144  
     public Short getTrackNo()
 145  
     {
 146  3
         return numbers.get(TRACK_NO_INDEX);
 147  
     }
 148  
 
 149  
     /**
 150  
      * @return
 151  
      */
 152  
     public Short getTrackTotal()
 153  
     {
 154  3
         return numbers.get(TRACK_TOTAL_INDEX);
 155  
     }
 156  
 }