Coverage Report - org.jaudiotagger.tag.id3.ID3v1TagField
 
Classes in this File Line Coverage Branch Coverage Complexity
ID3v1TagField
18%
10/53
65%
13/20
1.562
 
 1  
 package org.jaudiotagger.tag.id3;
 2  
 
 3  
 import org.jaudiotagger.audio.generic.Utils;
 4  
 import org.jaudiotagger.tag.TagField;
 5  
 import org.jaudiotagger.tag.TagTextField;
 6  
 
 7  
 import java.io.UnsupportedEncodingException;
 8  
 
 9  
 /**
 10  
  * This class encapsulates the name and content of a tag entry in id3 fields
 11  
  * <br>
 12  
  *
 13  
  * @author @author Raphael Slinckx (KiKiDonK)
 14  
  * @author Christian Laireiter (liree)
 15  
  */
 16  
 public class ID3v1TagField implements TagTextField
 17  
 {
 18  
 
 19  
     /**
 20  
      * If <code>true</code>, the id of the current encapsulated tag field is
 21  
      * specified as a common field. <br>
 22  
      * Example is "ARTIST" which should be interpreted by any application as the
 23  
      * artist of the media content. <br>
 24  
      * Will be set during construction with {@link #checkCommon()}.
 25  
      */
 26  
     private boolean common;
 27  
 
 28  
     /**
 29  
      * Stores the content of the tag field. <br>
 30  
      */
 31  
     private String content;
 32  
 
 33  
     /**
 34  
      * Stores the id (name) of the tag field. <br>
 35  
      */
 36  
     private String id;
 37  
 
 38  
     /**
 39  
      * Creates an instance.
 40  
      *
 41  
      * @param raw Raw byte data of the tagfield.
 42  
      * @throws UnsupportedEncodingException If the data doesn't conform "UTF-8" specification.
 43  
      */
 44  
     public ID3v1TagField(byte[] raw) throws UnsupportedEncodingException
 45  0
     {
 46  0
         String field = new String(raw, "ISO-8859-1");
 47  
 
 48  0
         int i = field.indexOf("=");
 49  0
         if (i == -1)
 50  
         {
 51  
             //Beware that ogg ID, must be capitalized and contain no space..
 52  0
             this.id = "ERRONEOUS";
 53  0
             this.content = field;
 54  
         }
 55  
         else
 56  
         {
 57  0
             this.id = field.substring(0, i).toUpperCase();
 58  0
             if (field.length() > i)
 59  
             {
 60  0
                 this.content = field.substring(i + 1);
 61  
             }
 62  
             else
 63  
             {
 64  
                 //We have "XXXXXX=" with nothing after the "="
 65  0
                 this.content = "";
 66  
             }
 67  
         }
 68  0
         checkCommon();
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Creates an instance.
 73  
      *
 74  
      * @param fieldId      ID (name) of the field.
 75  
      * @param fieldContent Content of the field.
 76  
      */
 77  
     public ID3v1TagField(String fieldId, String fieldContent)
 78  336
     {
 79  336
         this.id = fieldId.toUpperCase();
 80  336
         this.content = fieldContent;
 81  336
         checkCommon();
 82  336
     }
 83  
 
 84  
     /**
 85  
      * This method examines the ID of the current field and modifies
 86  
      * {@link #common}in order to reflect if the tag id is a commonly used one.
 87  
      * <br>
 88  
      */
 89  
     private void checkCommon()
 90  
     {
 91  336
         this.common = id.equals(ID3v1FieldKey.TITLE.name()) || id.equals(ID3v1FieldKey.ALBUM.name()) || id.equals(ID3v1FieldKey.ARTIST.name()) || id.equals(ID3v1FieldKey.GENRE.name()) || id.equals(ID3v1FieldKey.YEAR.name()) || id.equals(ID3v1FieldKey.COMMENT.name()) || id.equals(ID3v1FieldKey.TRACK.name());
 92  336
     }
 93  
 
 94  
     /**
 95  
      * This method will copy all bytes of <code>src</code> to <code>dst</code>
 96  
      * at the specified location.
 97  
      *
 98  
      * @param src       bytes to copy.
 99  
      * @param dst       where to copy to.
 100  
      * @param dstOffset at which position of <code>dst</code> the data should be
 101  
      *                  copied.
 102  
      */
 103  
     protected void copy(byte[] src, byte[] dst, int dstOffset)
 104  
     {
 105  
         //        for (int i = 0; i < src.length; i++)
 106  
         //            dst[i + dstOffset] = src[i];
 107  
         /*
 108  
          * Heared that this method is optimized and does its job very near of
 109  
          * the system.
 110  
          */
 111  0
         System.arraycopy(src, 0, dst, dstOffset, src.length);
 112  0
     }
 113  
 
 114  
     /**
 115  
      * @see TagField#copyContent(TagField)
 116  
      */
 117  
     public void copyContent(TagField field)
 118  
     {
 119  0
         if (field instanceof TagTextField)
 120  
         {
 121  0
             this.content = ((TagTextField) field).getContent();
 122  
         }
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @see TagTextField#getContent()
 127  
      */
 128  
     public String getContent()
 129  
     {
 130  336
         return content;
 131  
     }
 132  
 
 133  
     /**
 134  
      * @see TagTextField#getEncoding()
 135  
      */
 136  
     public String getEncoding()
 137  
     {
 138  0
         return "ISO-8859-1";
 139  
     }
 140  
 
 141  
     /**
 142  
      * @see TagField#getId()
 143  
      */
 144  
     public String getId()
 145  
     {
 146  224
         return this.id;
 147  
     }
 148  
 
 149  
     /**
 150  
      * @see TagField#getRawContent()
 151  
      */
 152  
     public byte[] getRawContent() throws UnsupportedEncodingException
 153  
     {
 154  0
         byte[] size = new byte[4];
 155  0
         byte[] idBytes = this.id.getBytes("ISO-8859-1");
 156  0
         byte[] contentBytes = Utils.getDefaultBytes(this.content, "ISO-8859-1");
 157  0
         byte[] b = new byte[4 + idBytes.length + 1 + contentBytes.length];
 158  
 
 159  0
         int length = idBytes.length + 1 + contentBytes.length;
 160  0
         size[3] = (byte) ((length & 0xFF000000) >> 24);
 161  0
         size[2] = (byte) ((length & 0x00FF0000) >> 16);
 162  0
         size[1] = (byte) ((length & 0x0000FF00) >> 8);
 163  0
         size[0] = (byte) (length & 0x000000FF);
 164  
 
 165  0
         int offset = 0;
 166  0
         copy(size, b, offset);
 167  0
         offset += 4;
 168  0
         copy(idBytes, b, offset);
 169  0
         offset += idBytes.length;
 170  0
         b[offset] = (byte) 0x3D;
 171  0
         offset++;// "="
 172  0
         copy(contentBytes, b, offset);
 173  
 
 174  0
         return b;
 175  
     }
 176  
 
 177  
     /**
 178  
      * @see TagField#isBinary()
 179  
      */
 180  
     public boolean isBinary()
 181  
     {
 182  0
         return false;
 183  
     }
 184  
 
 185  
     /**
 186  
      * @see TagField#isBinary(boolean)
 187  
      */
 188  
     public void isBinary(boolean b)
 189  
     {
 190  
         //Do nothing, always false
 191  0
     }
 192  
 
 193  
     /**
 194  
      * @see TagField#isCommon()
 195  
      */
 196  
     public boolean isCommon()
 197  
     {
 198  0
         return common;
 199  
     }
 200  
 
 201  
     /**
 202  
      * @see TagField#isEmpty()
 203  
      */
 204  
     public boolean isEmpty()
 205  
     {
 206  0
         return this.content.equals("");
 207  
     }
 208  
 
 209  
     /**
 210  
      * @see TagTextField#setContent(String)
 211  
      */
 212  
     public void setContent(String s)
 213  
     {
 214  0
         this.content = s;
 215  0
     }
 216  
 
 217  
     /**
 218  
      * @see TagTextField#setEncoding(String)
 219  
      */
 220  
     public void setEncoding(String s)
 221  
     {
 222  
         //Do nothing, encoding is always ISO-8859-1 for this tag
 223  0
     }
 224  
 
 225  
     public String toString()
 226  
     {
 227  144
         return getContent();
 228  
     }
 229  
 }