| 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 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | 4 | public final class ContentBranding extends MetadataContainer { |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public final static Set<String> ALLOWED; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
public final static String KEY_BANNER_IMAGE = "BANNER_IMAGE"; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public final static String KEY_BANNER_TYPE = "BANNER_IMAGE_TYPE"; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public final static String KEY_BANNER_URL = "BANNER_IMAGE_URL"; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
|
| 63 | |
|
| 64 | |
public ContentBranding() { |
| 65 | 0 | this(0, BigInteger.ZERO); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public ContentBranding(final long pos, final BigInteger size) { |
| 77 | 201 | super(ContainerType.CONTENT_BRANDING, pos, size); |
| 78 | 185 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public String getBannerImageURL() { |
| 86 | 8 | return getValueFor(KEY_BANNER_URL); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public String getCopyRightURL() { |
| 95 | 8 | return getValueFor(KEY_COPYRIGHT_URL); |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public long getCurrentAsfChunkSize() { |
| 103 | |
|
| 104 | |
|
| 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 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public byte[] getImageData() { |
| 119 | 0 | return assertDescriptor(KEY_BANNER_IMAGE, |
| 120 | |
MetadataDescriptor.TYPE_BINARY).getRawData(); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 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 | |
|
| 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 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 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 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 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 | |
|
| 181 | |
|
| 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 | |
|
| 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 | |
} |