| 1 | |
package org.jaudiotagger.audio.flac.metadatablock; |
| 2 | |
|
| 3 | |
import org.jaudiotagger.audio.generic.Utils; |
| 4 | |
import org.jaudiotagger.tag.InvalidFrameException; |
| 5 | |
import org.jaudiotagger.tag.TagField; |
| 6 | |
import org.jaudiotagger.tag.TagFieldKey; |
| 7 | |
import org.jaudiotagger.tag.id3.valuepair.TextEncoding; |
| 8 | |
import org.jaudiotagger.tag.reference.PictureTypes; |
| 9 | |
|
| 10 | |
import java.io.ByteArrayOutputStream; |
| 11 | |
import java.io.IOException; |
| 12 | |
import java.io.RandomAccessFile; |
| 13 | |
import java.io.UnsupportedEncodingException; |
| 14 | |
import java.nio.ByteBuffer; |
| 15 | |
import java.util.logging.Logger; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class MetadataBlockDataPicture implements MetadataBlockData, TagField |
| 44 | |
{ |
| 45 | |
public static final String IMAGE_IS_URL = "-->"; |
| 46 | |
|
| 47 | |
private int pictureType; |
| 48 | |
private String mimeType; |
| 49 | |
private String description; |
| 50 | |
private int width; |
| 51 | |
private int height; |
| 52 | |
private int colourDepth; |
| 53 | |
private int indexedColouredCount; |
| 54 | |
private byte[] imageData; |
| 55 | |
|
| 56 | |
|
| 57 | 4 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac.MetadataBlockDataPicture"); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public MetadataBlockDataPicture(MetadataBlockHeader header, RandomAccessFile raf) throws IOException, InvalidFrameException |
| 64 | 17 | { |
| 65 | 17 | ByteBuffer rawdata = ByteBuffer.allocate(header.getDataLength()); |
| 66 | 17 | int bytesRead = raf.getChannel().read(rawdata); |
| 67 | 17 | if (bytesRead < header.getDataLength()) |
| 68 | |
{ |
| 69 | 0 | throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + header.getDataLength()); |
| 70 | |
} |
| 71 | 17 | rawdata.rewind(); |
| 72 | |
|
| 73 | |
|
| 74 | 17 | pictureType = rawdata.getInt(); |
| 75 | 17 | if (pictureType >= PictureTypes.getInstanceOf().getSize()) |
| 76 | |
{ |
| 77 | 0 | throw new InvalidFrameException("PictureType was:" + pictureType + "but the maximum allowed is " + (PictureTypes.getInstanceOf().getSize() - 1)); |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | 17 | int mimeTypeSize = rawdata.getInt(); |
| 82 | 17 | mimeType = getString(rawdata, mimeTypeSize, "ISO-8859-1"); |
| 83 | |
|
| 84 | |
|
| 85 | 17 | int descriptionSize = rawdata.getInt(); |
| 86 | 17 | description = getString(rawdata, descriptionSize, "UTF-8"); |
| 87 | |
|
| 88 | |
|
| 89 | 17 | width = rawdata.getInt(); |
| 90 | |
|
| 91 | |
|
| 92 | 17 | height = rawdata.getInt(); |
| 93 | |
|
| 94 | |
|
| 95 | 17 | colourDepth = rawdata.getInt(); |
| 96 | |
|
| 97 | |
|
| 98 | 17 | indexedColouredCount = rawdata.getInt(); |
| 99 | |
|
| 100 | |
|
| 101 | 17 | int rawdataSize = rawdata.getInt(); |
| 102 | 17 | imageData = new byte[rawdataSize]; |
| 103 | 17 | rawdata.get(imageData); |
| 104 | |
|
| 105 | 17 | logger.info("Read image:" + this.toString()); |
| 106 | |
|
| 107 | 17 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public MetadataBlockDataPicture(byte[] imageData, int pictureType, String mimeType, String description, int width, int height, int colourDepth, int indexedColouredCount) |
| 113 | 5 | { |
| 114 | |
|
| 115 | 5 | this.pictureType = pictureType; |
| 116 | |
|
| 117 | |
|
| 118 | 5 | this.mimeType = mimeType; |
| 119 | |
|
| 120 | |
|
| 121 | 5 | this.description = description; |
| 122 | |
|
| 123 | 5 | this.width = width; |
| 124 | |
|
| 125 | 5 | this.height = height; |
| 126 | |
|
| 127 | 5 | this.colourDepth = colourDepth; |
| 128 | |
|
| 129 | 5 | this.indexedColouredCount = indexedColouredCount; |
| 130 | |
|
| 131 | 5 | this.imageData = imageData; |
| 132 | 5 | } |
| 133 | |
|
| 134 | |
private String getString(ByteBuffer rawdata, int length, String charset) throws IOException |
| 135 | |
{ |
| 136 | 34 | byte[] tempbuffer = new byte[length]; |
| 137 | 34 | rawdata.get(tempbuffer); |
| 138 | 34 | return new String(tempbuffer, charset); |
| 139 | |
} |
| 140 | |
|
| 141 | |
public byte[] getBytes() |
| 142 | |
{ |
| 143 | |
try |
| 144 | |
{ |
| 145 | 66 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 146 | 66 | baos.write(Utils.getSizeBEInt32(pictureType)); |
| 147 | 66 | baos.write(Utils.getSizeBEInt32(mimeType.length())); |
| 148 | 66 | baos.write(mimeType.getBytes("ISO-8859-1")); |
| 149 | 66 | baos.write(Utils.getSizeBEInt32(description.length())); |
| 150 | 66 | baos.write(description.getBytes("UTF-8")); |
| 151 | 66 | baos.write(Utils.getSizeBEInt32(width)); |
| 152 | 66 | baos.write(Utils.getSizeBEInt32(height)); |
| 153 | 66 | baos.write(Utils.getSizeBEInt32(colourDepth)); |
| 154 | 66 | baos.write(Utils.getSizeBEInt32(indexedColouredCount)); |
| 155 | 66 | baos.write(Utils.getSizeBEInt32(imageData.length)); |
| 156 | 66 | baos.write(imageData); |
| 157 | 66 | return baos.toByteArray(); |
| 158 | |
|
| 159 | |
} |
| 160 | 0 | catch (IOException ioe) |
| 161 | |
{ |
| 162 | 0 | throw new RuntimeException(ioe.getMessage()); |
| 163 | |
} |
| 164 | |
} |
| 165 | |
|
| 166 | |
public int getLength() |
| 167 | |
{ |
| 168 | 22 | return getBytes().length; |
| 169 | |
} |
| 170 | |
|
| 171 | |
public int getPictureType() |
| 172 | |
{ |
| 173 | 17 | return pictureType; |
| 174 | |
} |
| 175 | |
|
| 176 | |
public String getMimeType() |
| 177 | |
{ |
| 178 | 48 | return mimeType; |
| 179 | |
} |
| 180 | |
|
| 181 | |
public String getDescription() |
| 182 | |
{ |
| 183 | 15 | return description; |
| 184 | |
} |
| 185 | |
|
| 186 | |
public int getWidth() |
| 187 | |
{ |
| 188 | 3 | return width; |
| 189 | |
} |
| 190 | |
|
| 191 | |
public int getHeight() |
| 192 | |
{ |
| 193 | 3 | return height; |
| 194 | |
} |
| 195 | |
|
| 196 | |
public int getColourDepth() |
| 197 | |
{ |
| 198 | 3 | return colourDepth; |
| 199 | |
} |
| 200 | |
|
| 201 | |
public int getIndexedColourCount() |
| 202 | |
{ |
| 203 | 3 | return indexedColouredCount; |
| 204 | |
} |
| 205 | |
|
| 206 | |
public byte[] getImageData() |
| 207 | |
{ |
| 208 | 31 | return imageData; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public boolean isImageUrl() |
| 215 | |
{ |
| 216 | 31 | return getMimeType().equals(IMAGE_IS_URL); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public String getImageUrl() |
| 223 | |
{ |
| 224 | 10 | if (isImageUrl()) |
| 225 | |
{ |
| 226 | 9 | return Utils.getString(getImageData(), 0, getImageData().length, TextEncoding.CHARSET_ISO_8859_1); |
| 227 | |
} |
| 228 | |
else |
| 229 | |
{ |
| 230 | 1 | return ""; |
| 231 | |
} |
| 232 | |
} |
| 233 | |
|
| 234 | |
public String toString() |
| 235 | |
{ |
| 236 | 17 | return PictureTypes.getInstanceOf().getValueForId(pictureType) + ":" + mimeType + ":" + description + ":" + "width:" + width + ":height:" + height + ":colourdepth:" + colourDepth + ":indexedColourCount:" + indexedColouredCount + ":image size in bytes:" + imageData.length; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void copyContent(TagField field) |
| 245 | |
{ |
| 246 | 0 | throw new UnsupportedOperationException(); |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public String getId() |
| 258 | |
{ |
| 259 | 0 | return TagFieldKey.COVER_ART.name(); |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
public byte[] getRawContent() throws UnsupportedEncodingException |
| 274 | |
{ |
| 275 | 0 | return getBytes(); |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public boolean isBinary() |
| 288 | |
{ |
| 289 | 0 | return true; |
| 290 | |
} |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public void isBinary(boolean b) |
| 304 | |
{ |
| 305 | |
|
| 306 | 0 | } |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
public boolean isCommon() |
| 319 | |
{ |
| 320 | 0 | return true; |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
public boolean isEmpty() |
| 329 | |
{ |
| 330 | 0 | return false; |
| 331 | |
} |
| 332 | |
|
| 333 | |
|
| 334 | |
} |