| 1 | |
package org.jaudiotagger.tag.asf; |
| 2 | |
|
| 3 | |
import org.jaudiotagger.audio.asf.data.AsfHeader; |
| 4 | |
import org.jaudiotagger.audio.asf.data.MetadataDescriptor; |
| 5 | |
import org.jaudiotagger.logging.ErrorMessage; |
| 6 | |
import org.jaudiotagger.tag.id3.valuepair.ImageFormats; |
| 7 | |
import org.jaudiotagger.tag.asf.AbstractAsfTagImageField; |
| 8 | |
import org.jaudiotagger.tag.asf.AsfFieldKey; |
| 9 | |
|
| 10 | |
import java.io.ByteArrayOutputStream; |
| 11 | |
import java.io.UnsupportedEncodingException; |
| 12 | |
import java.util.logging.Logger; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
public class AsfTagCoverField extends AbstractAsfTagImageField |
| 25 | |
{ |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 4 | public final static Logger LOGGER = Logger |
| 30 | |
.getLogger("org.jaudiotagger.audio.asf.tag"); |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
private String description; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 94 | private int endOfName = 0; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private int imageDataSize; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private String mimeType; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private int pictureType; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public AsfTagCoverField(final byte[] imageData, final int pictureType, |
| 66 | |
final String description, final String mimeType) { |
| 67 | 12 | super(new MetadataDescriptor(AsfFieldKey.COVER_ART.getFieldName(), |
| 68 | |
MetadataDescriptor.TYPE_BINARY)); |
| 69 | 12 | this.getDescriptor() |
| 70 | |
.setBinaryValue( |
| 71 | |
createRawContent(imageData, pictureType, description, |
| 72 | |
mimeType)); |
| 73 | 12 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public AsfTagCoverField(final MetadataDescriptor source) { |
| 82 | 82 | super(source); |
| 83 | |
|
| 84 | 82 | if (!source.getName().equals(AsfFieldKey.COVER_ART.getFieldName())) { |
| 85 | 0 | throw new IllegalArgumentException( |
| 86 | |
"Descriptor description must be WM/Picture"); |
| 87 | |
} |
| 88 | 82 | if (source.getType() != MetadataDescriptor.TYPE_BINARY) { |
| 89 | 0 | throw new IllegalArgumentException("Descriptor type must be binary"); |
| 90 | |
} |
| 91 | |
|
| 92 | |
try { |
| 93 | 82 | processRawContent(); |
| 94 | 0 | } catch (final UnsupportedEncodingException uee) { |
| 95 | |
|
| 96 | 0 | throw new RuntimeException(uee); |
| 97 | 82 | } |
| 98 | 82 | } |
| 99 | |
|
| 100 | |
private byte[] createRawContent(final byte[] data, final int pictureType, |
| 101 | |
final String description, String mimeType) { |
| 102 | 12 | this.description = description; |
| 103 | |
|
| 104 | |
|
| 105 | 12 | if (mimeType == null) { |
| 106 | 4 | mimeType = ImageFormats.getMimeTypeForBinarySignature(data); |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | 4 | if (mimeType == null) { |
| 111 | 0 | LOGGER.warning(ErrorMessage.GENERAL_UNIDENITIFED_IMAGE_FORMAT |
| 112 | |
.getMsg()); |
| 113 | 0 | mimeType = ImageFormats.MIME_TYPE_PNG; |
| 114 | |
} |
| 115 | |
} |
| 116 | |
|
| 117 | 12 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 118 | |
|
| 119 | |
|
| 120 | 12 | baos.write(pictureType); |
| 121 | |
|
| 122 | |
|
| 123 | 12 | baos.write(org.jaudiotagger.audio.generic.Utils |
| 124 | |
.getSizeLEInt32(data.length), 0, 4); |
| 125 | |
|
| 126 | |
|
| 127 | |
byte[] mimeTypeData; |
| 128 | |
try { |
| 129 | 12 | mimeTypeData = mimeType.getBytes(AsfHeader.ASF_CHARSET.name()); |
| 130 | 0 | } catch (final UnsupportedEncodingException uee) { |
| 131 | |
|
| 132 | 0 | throw new RuntimeException("Unable to find encoding:" |
| 133 | |
+ AsfHeader.ASF_CHARSET.name()); |
| 134 | 12 | } |
| 135 | 12 | baos.write(mimeTypeData, 0, mimeTypeData.length); |
| 136 | |
|
| 137 | |
|
| 138 | 12 | baos.write(0x00); |
| 139 | 12 | baos.write(0x00); |
| 140 | |
|
| 141 | |
|
| 142 | 12 | if (description != null && description.length() > 0) { |
| 143 | |
byte[] descriptionData; |
| 144 | |
try { |
| 145 | 4 | descriptionData = description.getBytes(AsfHeader.ASF_CHARSET |
| 146 | |
.name()); |
| 147 | 0 | } catch (final UnsupportedEncodingException uee) { |
| 148 | |
|
| 149 | 0 | throw new RuntimeException("Unable to find encoding:" |
| 150 | |
+ AsfHeader.ASF_CHARSET.name()); |
| 151 | 4 | } |
| 152 | 4 | baos.write(descriptionData, 0, descriptionData.length); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | 12 | baos.write(0x00); |
| 157 | 12 | baos.write(0x00); |
| 158 | |
|
| 159 | |
|
| 160 | 12 | baos.write(data, 0, data.length); |
| 161 | |
|
| 162 | 12 | return baos.toByteArray(); |
| 163 | |
} |
| 164 | |
|
| 165 | |
public String getDescription() { |
| 166 | 40 | return this.description; |
| 167 | |
} |
| 168 | |
|
| 169 | |
@Override |
| 170 | |
public int getImageDataSize() { |
| 171 | 12 | return this.imageDataSize; |
| 172 | |
} |
| 173 | |
|
| 174 | |
public String getMimeType() { |
| 175 | 40 | return this.mimeType; |
| 176 | |
} |
| 177 | |
|
| 178 | |
public int getPictureType() { |
| 179 | 44 | return this.pictureType; |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
@Override |
| 186 | |
public byte[] getRawImageData() { |
| 187 | 100 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 188 | 100 | baos.write(getRawContent(), this.endOfName, this.toWrap |
| 189 | |
.getRawDataSize() |
| 190 | |
- this.endOfName); |
| 191 | 100 | return baos.toByteArray(); |
| 192 | |
} |
| 193 | |
|
| 194 | |
private void processRawContent() throws UnsupportedEncodingException { |
| 195 | |
|
| 196 | 82 | this.pictureType = this.getRawContent()[0]; |
| 197 | |
|
| 198 | |
|
| 199 | 82 | this.imageDataSize = org.jaudiotagger.audio.generic.Utils.getIntLE(this |
| 200 | |
.getRawContent(), 1, 2); |
| 201 | |
|
| 202 | |
|
| 203 | 82 | int count = 5; |
| 204 | 82 | this.mimeType = null; |
| 205 | 82 | this.description = null; |
| 206 | 82 | int endOfMimeType = 0; |
| 207 | |
|
| 208 | 1508 | while (count < this.getRawContent().length - 1) { |
| 209 | 1508 | if (getRawContent()[count] == 0 && getRawContent()[count + 1] == 0) { |
| 210 | 164 | if (this.mimeType == null) { |
| 211 | 82 | this.mimeType = new String(getRawContent(), 5, (count) - 5, |
| 212 | |
"UTF-16LE"); |
| 213 | 82 | endOfMimeType = count + 2; |
| 214 | 82 | } else if (this.description == null) { |
| 215 | 82 | this.description = new String(getRawContent(), |
| 216 | |
endOfMimeType, count - endOfMimeType, "UTF-16LE"); |
| 217 | 82 | this.endOfName = count + 2; |
| 218 | 82 | break; |
| 219 | |
} |
| 220 | |
} |
| 221 | 1426 | count += 2; |
| 222 | |
} |
| 223 | 82 | } |
| 224 | |
|
| 225 | |
} |