Coverage Report - org.jaudiotagger.audio.asf.data.ContentBranding
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentBranding
34%
18/52
14%
5/34
1.667
 
 1  
 package org.jaudiotagger.audio.asf.data;
 2  
 
 3  
 import org.jaudiotagger.audio.asf.util.Utils;
 4  
 
 5  
 import java.io.IOException;
 6  
 import java.io.OutputStream;
 7  
 import java.math.BigInteger;
 8  
 import java.util.HashSet;
 9  
 import java.util.Set;
 10  
 
 11  
 /**
 12  
  * This structure represents the value of the content branding object, which
 13  
  * stores the banner image, the banner image URL and the copyright URL.<br>
 14  
  * 
 15  
  * @author Christian Laireiter
 16  
  */
 17  4
 public final class ContentBranding extends MetadataContainer {
 18  
 
 19  
     /**
 20  
      * Stores the allowed {@linkplain MetadataDescriptor#getName() descriptor
 21  
      * keys}.
 22  
      */
 23  
     public final static Set<String> ALLOWED;
 24  
 
 25  
     /**
 26  
      * Descriptor key representing the banner image.
 27  
      */
 28  
     public final static String KEY_BANNER_IMAGE = "BANNER_IMAGE";
 29  
 
 30  
     /**
 31  
      * Descriptor key representing the banner image type.<br>
 32  
      * <br>
 33  
      * <b>Known/valid values are:</b>
 34  
      * <ol>
 35  
      * <li>0: there is no image present</li>
 36  
      * <li>1: there is a BMP image</li>
 37  
      * <li>2: there is a JPEG image</li>
 38  
      * <li>3: there is a GIF image</li>
 39  
      * </ol>
 40  
      */
 41  
     public final static String KEY_BANNER_TYPE = "BANNER_IMAGE_TYPE";
 42  
 
 43  
     /**
 44  
      * Descriptor key representing the banner image URL.
 45  
      */
 46  
     public final static String KEY_BANNER_URL = "BANNER_IMAGE_URL";
 47  
 
 48  
     /**
 49  
      * Descriptor key representing the copyright URL.
 50  
      */
 51  
     public final static String KEY_COPYRIGHT_URL = "COPYRIGHT_URL";
 52  
 
 53  
     static {
 54  4
         ALLOWED = new HashSet<String>();
 55  4
         ALLOWED.add(KEY_BANNER_IMAGE);
 56  4
         ALLOWED.add(KEY_BANNER_TYPE);
 57  4
         ALLOWED.add(KEY_BANNER_URL);
 58  4
         ALLOWED.add(KEY_COPYRIGHT_URL);
 59  4
     }
 60  
 
 61  
     /**
 62  
      * Creates an instance.
 63  
      */
 64  
     public ContentBranding() {
 65  0
         this(0, BigInteger.ZERO);
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Creates an instance.
 70  
      * 
 71  
      * @param pos
 72  
      *            Position of content description within file or stream
 73  
      * @param size
 74  
      *            Length of content description.
 75  
      */
 76  
     public ContentBranding(final long pos, final BigInteger size) {
 77  201
         super(ContainerType.CONTENT_BRANDING, pos, size);
 78  185
     }
 79  
 
 80  
     /**
 81  
      * Returns the banner image URL.
 82  
      * 
 83  
      * @return the banner image URL.
 84  
      */
 85  
     public String getBannerImageURL() {
 86  8
         return getValueFor(KEY_BANNER_URL);
 87  
     }
 88  
 
 89  
     /**
 90  
      * Returns the copyright URL.
 91  
      * 
 92  
      * @return the banner image URL.
 93  
      */
 94  
     public String getCopyRightURL() {
 95  8
         return getValueFor(KEY_COPYRIGHT_URL);
 96  
     }
 97  
 
 98  
     /**
 99  
      * {@inheritDoc}
 100  
      */
 101  
     @Override
 102  
     public long getCurrentAsfChunkSize() {
 103  
         // GUID, size, image type, image data size, image url data size,
 104  
         // copyright data size
 105  0
         long result = 40;
 106  0
         result += assertDescriptor(KEY_BANNER_IMAGE,
 107  
                 MetadataDescriptor.TYPE_BINARY).getRawDataSize();
 108  0
         result += getBannerImageURL().length();
 109  0
         result += getCopyRightURL().length();
 110  0
         return result;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Returns the binary image data.
 115  
      * 
 116  
      * @return binary image data.
 117  
      */
 118  
     public byte[] getImageData() {
 119  0
         return assertDescriptor(KEY_BANNER_IMAGE,
 120  
                 MetadataDescriptor.TYPE_BINARY).getRawData();
 121  
     }
 122  
 
 123  
     /**
 124  
      * Returns the image type.<br>
 125  
      * 
 126  
      * @see #KEY_BANNER_TYPE for known/valid values.
 127  
      * @return image type
 128  
      */
 129  
     public long getImageType() {
 130  0
         if (!hasDescriptor(KEY_BANNER_TYPE)) {
 131  0
             final MetadataDescriptor descriptor = new MetadataDescriptor(
 132  
                     ContainerType.CONTENT_BRANDING, KEY_BANNER_TYPE,
 133  
                     MetadataDescriptor.TYPE_DWORD);
 134  0
             descriptor.setDWordValue(0);
 135  0
             addDescriptor(descriptor);
 136  
         }
 137  0
         return assertDescriptor(KEY_BANNER_TYPE).getNumber();
 138  
     }
 139  
 
 140  
     /**
 141  
      * {@inheritDoc}
 142  
      */
 143  
     @Override
 144  
     public boolean isAddSupported(final MetadataDescriptor descriptor) {
 145  2341
         return ALLOWED.contains(descriptor.getName())
 146  
                 && super.isAddSupported(descriptor);
 147  
     }
 148  
 
 149  
     /**
 150  
      * This method sets the banner image URL, if <code>imageURL</code> is not
 151  
      * blank.<br>
 152  
      * 
 153  
      * @param imageURL
 154  
      *            image URL to set.
 155  
      */
 156  
     public void setBannerImageURL(final String imageURL) {
 157  4
         if (Utils.isBlank(imageURL)) {
 158  0
             removeDescriptorsByName(KEY_BANNER_URL);
 159  
         } else {
 160  4
             assertDescriptor(KEY_BANNER_URL).setStringValue(imageURL);
 161  
         }
 162  4
     }
 163  
 
 164  
     /**
 165  
      * This method sets the copyright URL, if <code>copyRight</code> is not
 166  
      * blank.<br>
 167  
      * 
 168  
      * @param copyRight
 169  
      *            copyright URL to set.
 170  
      */
 171  
     public void setCopyRightURL(final String copyRight) {
 172  4
         if (Utils.isBlank(copyRight)) {
 173  0
             removeDescriptorsByName(KEY_COPYRIGHT_URL);
 174  
         } else {
 175  4
             assertDescriptor(KEY_COPYRIGHT_URL).setStringValue(copyRight);
 176  
         }
 177  4
     }
 178  
 
 179  
     /**
 180  
      * @param imageType
 181  
      * @param imageData
 182  
      */
 183  
     public void setImage(final long imageType, final byte[] imageData) {
 184  0
         assert imageType >= 0 && imageType <= 3;
 185  0
         assert imageType > 0 || imageData.length == 0;
 186  0
         assertDescriptor(KEY_BANNER_TYPE, MetadataDescriptor.TYPE_DWORD)
 187  
                 .setDWordValue(imageType);
 188  0
         assertDescriptor(KEY_BANNER_IMAGE, MetadataDescriptor.TYPE_BINARY)
 189  
                 .setBinaryValue(imageData);
 190  0
     }
 191  
 
 192  
     /**
 193  
      * {@inheritDoc}
 194  
      */
 195  
     @Override
 196  
     public long writeInto(final OutputStream out) throws IOException {
 197  0
         final long chunkSize = getCurrentAsfChunkSize();
 198  0
         out.write(getGuid().getBytes());
 199  0
         Utils.writeUINT64(chunkSize, out);
 200  0
         Utils.writeUINT32(getImageType(), out);
 201  0
         assert getImageType() >= 0 && getImageType() <= 3;
 202  0
         final byte[] imageData = getImageData();
 203  0
         assert getImageType() > 0 || imageData.length == 0;
 204  0
         Utils.writeUINT32(imageData.length, out);
 205  0
         out.write(imageData);
 206  0
         Utils.writeUINT32(getBannerImageURL().length(), out);
 207  0
         out.write(getBannerImageURL().getBytes("ASCII"));
 208  0
         Utils.writeUINT32(getCopyRightURL().length(), out);
 209  0
         out.write(getCopyRightURL().getBytes("ASCII"));
 210  0
         return chunkSize;
 211  
     }
 212  
 
 213  
 }