Coverage Report - org.jaudiotagger.audio.asf.tag.AsfTagCoverField
 
Classes in this File Line Coverage Branch Coverage Complexity
AsfTagCoverField
86%
56/65
65%
13/20
2.7
 
 1  
 package org.jaudiotagger.audio.asf.tag;
 2  
 
 3  
 import org.jaudiotagger.audio.asf.data.ContentDescriptor;
 4  
 import org.jaudiotagger.audio.asf.data.AsfHeader;
 5  
 import org.jaudiotagger.tag.id3.valuepair.ImageFormats;
 6  
 import org.jaudiotagger.logging.ErrorMessage;
 7  
 
 8  
 import javax.imageio.ImageIO;
 9  
 import java.io.UnsupportedEncodingException;
 10  
 import java.io.ByteArrayInputStream;
 11  
 import java.io.IOException;
 12  
 import java.io.ByteArrayOutputStream;
 13  
 import java.awt.image.BufferedImage;
 14  
 import java.util.logging.Logger;
 15  
 
 16  
 /**
 17  
  * Enscapulates the WM/Pictures provides some convenience methods for decoding the binary data it contains
 18  
  *
 19  
  * The value of a Wm/Pictures content descriptor is as follows:
 20  
  *
 21  
  * byte0    Picture Type
 22  
  * byte1-4  Length of the image data
 23  
  * mimetype encoded as UTF-16LE
 24  
  * null byte
 25  
  * null byte
 26  
  * description encoded as UTF-16LE (optional)
 27  
  * null byte
 28  
  * null byte
 29  
  * image data
 30  
  *
 31  
  */
 32  
 public class AsfTagCoverField extends AsfTagField
 33  
 {
 34  
     /** Logger Object  */
 35  5
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.asf.tag");
 36  
 
 37  
     /**
 38  
      * Picture Type
 39  
      */
 40  
     private int pictureType;
 41  
 
 42  
     /** Mimetype of binary
 43  
      *
 44  
      */
 45  
     private String mimeType;
 46  
 
 47  
     /** Description
 48  
      *
 49  
      */
 50  
     private String description;
 51  
 
 52  
     /**
 53  
      * Image Data Size as read
 54  
      */
 55  
     private int imageDataSize;
 56  
 
 57  
     /**
 58  
      * We need this to retrive the buffered image, if required
 59  
      */
 60  41
     private int endOfName=0;
 61  
 
 62  
      /**
 63  
      * Creates an instance from a content descriptor
 64  
      *
 65  
      * @param source The content descriptor, whose content is published.<br>
 66  
 
 67  
      */
 68  
     public AsfTagCoverField(ContentDescriptor source)
 69  
     {
 70  39
         super(source);
 71  
 
 72  39
         if(!source.getName().equals(AsfFieldKey.COVER_ART.getFieldName()))
 73  
         {
 74  0
             throw new IllegalArgumentException("Descriptor description must be WM/Picture");
 75  
         }
 76  39
         if (source.getType() != ContentDescriptor.TYPE_BINARY)
 77  
         {
 78  0
             throw new IllegalArgumentException("Descriptor type must be binary");
 79  
         }
 80  
 
 81  
         try
 82  
         {
 83  39
             processRawContent();
 84  
         }
 85  0
         catch(UnsupportedEncodingException uee)
 86  
         {
 87  
             //Should never happen
 88  0
             throw new RuntimeException(uee);
 89  39
         }
 90  39
     }
 91  
 
 92  
     /**
 93  
      * Create New Image Field
 94  
      *
 95  
      * @param imageData
 96  
      * @param description
 97  
      */
 98  
     public AsfTagCoverField(byte[] imageData,int pictureType,String description)
 99  
     {
 100  2
         super(new ContentDescriptor(AsfFieldKey.COVER_ART.getFieldName(), ContentDescriptor.TYPE_BINARY));
 101  2
         this.getDescriptor().setBinaryValue(createRawContent(imageData,pictureType,description));
 102  2
     }
 103  
 
 104  
     private byte[] createRawContent(byte[] data,int pictureType,String description)
 105  
     {
 106  
         //Get Mimetype
 107  2
         mimeType=ImageFormats.getMimeTypeForBinarySignature(data);
 108  2
         description=description;
 109  2
         if(mimeType==null)
 110  
         {
 111  0
             throw new RuntimeException(ErrorMessage.GENERAL_UNIDENITIFED_IMAGE_FORMAT.getMsg());
 112  
         }
 113  
 
 114  2
         ByteArrayOutputStream baos = new  ByteArrayOutputStream();
 115  
 
 116  
         //PictureType
 117  2
         baos.write(pictureType);
 118  
 
 119  
         //ImageDataSize
 120  2
         baos.write(org.jaudiotagger.audio.generic.Utils.getSizeLEInt32(data.length),0,4);
 121  
 
 122  
         //mimetype
 123  
         byte[] mimeTypeData;
 124  
         try
 125  
         {
 126  2
             mimeTypeData=mimeType.getBytes(AsfHeader.ASF_CHARSET.name());
 127  
         }
 128  0
         catch(UnsupportedEncodingException  uee)
 129  
         {
 130  
             //Should never happen
 131  0
             throw new RuntimeException("Unable to find encoding:"+AsfHeader.ASF_CHARSET.name());
 132  2
         }
 133  2
         baos.write(mimeTypeData,0,mimeTypeData.length);
 134  
 
 135  
         //Seperator
 136  2
         baos.write(0x00);
 137  2
         baos.write(0x00);
 138  
 
 139  
         //description
 140  2
         if(description!=null && description.length()>0)
 141  
         {
 142  
             byte[] descriptionData;
 143  
             try
 144  
             {
 145  1
                 descriptionData=description.getBytes(AsfHeader.ASF_CHARSET.name());
 146  
             }
 147  0
             catch(UnsupportedEncodingException  uee)
 148  
             {
 149  
                 //Should never happen
 150  0
                 throw new RuntimeException("Unable to find encoding:"+AsfHeader.ASF_CHARSET.name());
 151  1
             }
 152  1
             baos.write(descriptionData,0,descriptionData.length);
 153  
         }
 154  
 
 155  
         //Seperator (always write whther or not we have descriptor field)
 156  2
         baos.write(0x00);
 157  2
         baos.write(0x00);
 158  
 
 159  
         //Image data
 160  2
         baos.write(data,0,data.length);
 161  
 
 162  2
         return baos.toByteArray();
 163  
     }
 164  
 
 165  
 
 166  
     private void processRawContent() throws UnsupportedEncodingException
 167  
     {
 168  
         //PictureType
 169  39
         pictureType=this.getRawContent()[0];
 170  
 
 171  
         //ImageDataSize
 172  39
         imageDataSize=org.jaudiotagger.audio.generic.Utils.getIntLE(this.getRawContent(), 1, 2);
 173  
 
 174  
         //Set Count to after picture type,datasize and two byte nulls
 175  39
         int count=5;
 176  39
         mimeType=null;
 177  39
         description =null; //Optional
 178  39
         int endOfMimeType=0;
 179  
        
 180  693
         while(count<this.getRawContent().length - 1)
 181  
         {
 182  693
             if(getRawContent()[count]==0 && getRawContent()[count+1]==0 )
 183  
             {
 184  78
                 if(mimeType==null)
 185  
                 {
 186  39
                     mimeType = new String(getRawContent(),5,(count)-5,"UTF-16LE");
 187  39
                     endOfMimeType=count+2;
 188  
                 }
 189  39
                 else if(description ==null)
 190  
                 {
 191  39
                     description = new String(getRawContent(),endOfMimeType,count - endOfMimeType,"UTF-16LE");
 192  39
                     endOfName=count+2;
 193  39
                     break;
 194  
                 }
 195  
             }
 196  654
             count+=2;  //keep on two byte word boundary
 197  
         }
 198  39
     }
 199  
 
 200  
     public String getMimeType()
 201  
     {
 202  10
         return mimeType;
 203  
     }
 204  
 
 205  
     public String getDescription()
 206  
     {
 207  10
         return description;
 208  
     }
 209  
 
 210  
     public int getPictureType()
 211  
     {
 212  11
         return pictureType;
 213  
     }
 214  
 
 215  
     public int getImageDataSize()
 216  
     {
 217  3
         return imageDataSize;
 218  
     }
 219  
 
 220  
     /**
 221  
      *
 222  
      * @return the raw image data only
 223  
      */
 224  
     public byte[] getRawImageData()
 225  
     {
 226  25
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 227  25
         baos.write(getRawContent(),endOfName,getRawContent().length - endOfName);
 228  25
         return baos.toByteArray();
 229  
     }
 230  
 
 231  
     /**
 232  
      *
 233  
      * @return the image
 234  
      * @throws IOException
 235  
      */
 236  
     public BufferedImage getImage() throws IOException
 237  
     {
 238  15
         BufferedImage bi = ImageIO.read(ImageIO
 239  
                     .createImageInputStream(new ByteArrayInputStream(getRawImageData())));
 240  15
         return bi;
 241  
     }
 242  
 }