| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.flac; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.flac.metadatablock.BlockType; |
| 22 | |
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPadding; |
| 23 | |
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture; |
| 24 | |
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader; |
| 25 | |
import org.jaudiotagger.audio.generic.AbstractTagCreator; |
| 26 | |
import org.jaudiotagger.tag.Tag; |
| 27 | |
import org.jaudiotagger.tag.flac.FlacTag; |
| 28 | |
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentCreator; |
| 29 | |
|
| 30 | |
import java.io.UnsupportedEncodingException; |
| 31 | |
import java.nio.ByteBuffer; |
| 32 | |
import java.util.ListIterator; |
| 33 | |
import java.util.logging.Logger; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | 42 | public class FlacTagCreator extends AbstractTagCreator |
| 39 | |
{ |
| 40 | |
|
| 41 | 42 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac"); |
| 42 | |
|
| 43 | |
public static final int DEFAULT_PADDING = 4000; |
| 44 | 42 | private static final VorbisCommentCreator creator = new VorbisCommentCreator(); |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public ByteBuffer convert(Tag tag, int paddingSize) throws UnsupportedEncodingException |
| 53 | |
{ |
| 54 | 20 | logger.info("Convert flac tag:padding:" + paddingSize); |
| 55 | 20 | FlacTag flacTag = (FlacTag) tag; |
| 56 | |
|
| 57 | 20 | int tagLength = 0; |
| 58 | 20 | ByteBuffer vorbiscomment = null; |
| 59 | 20 | if (flacTag.getVorbisCommentTag() != null) |
| 60 | |
{ |
| 61 | 16 | vorbiscomment = creator.convert(flacTag.getVorbisCommentTag()); |
| 62 | 16 | tagLength = vorbiscomment.capacity() + MetadataBlockHeader.HEADER_LENGTH; |
| 63 | |
} |
| 64 | 20 | for (MetadataBlockDataPicture image : flacTag.getImages()) |
| 65 | |
{ |
| 66 | 22 | tagLength += image.getBytes().length + MetadataBlockHeader.HEADER_LENGTH; |
| 67 | |
} |
| 68 | |
|
| 69 | 20 | logger.info("Convert flac tag:taglength:" + tagLength); |
| 70 | 20 | ByteBuffer buf = ByteBuffer.allocate(tagLength + paddingSize); |
| 71 | |
|
| 72 | |
MetadataBlockHeader vorbisHeader; |
| 73 | |
|
| 74 | 20 | if (flacTag.getVorbisCommentTag() != null) |
| 75 | |
{ |
| 76 | 16 | if ((paddingSize > 0) || (flacTag.getImages().size() > 0)) |
| 77 | |
{ |
| 78 | 14 | vorbisHeader = new MetadataBlockHeader(false, BlockType.VORBIS_COMMENT, vorbiscomment.capacity()); |
| 79 | |
} |
| 80 | |
else |
| 81 | |
{ |
| 82 | 2 | vorbisHeader = new MetadataBlockHeader(true, BlockType.VORBIS_COMMENT, vorbiscomment.capacity()); |
| 83 | |
} |
| 84 | 16 | buf.put(vorbisHeader.getBytes()); |
| 85 | 16 | buf.put(vorbiscomment); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | 20 | ListIterator<MetadataBlockDataPicture> li = flacTag.getImages().listIterator(); |
| 90 | 42 | while (li.hasNext()) |
| 91 | |
{ |
| 92 | 22 | MetadataBlockDataPicture imageField = li.next(); |
| 93 | |
MetadataBlockHeader imageHeader; |
| 94 | |
|
| 95 | 22 | if (paddingSize > 0 || li.hasNext()) |
| 96 | |
{ |
| 97 | 16 | imageHeader = new MetadataBlockHeader(false, BlockType.PICTURE, imageField.getLength()); |
| 98 | |
} |
| 99 | |
else |
| 100 | |
{ |
| 101 | 6 | imageHeader = new MetadataBlockHeader(true, BlockType.PICTURE, imageField.getLength()); |
| 102 | |
} |
| 103 | 22 | buf.put(imageHeader.getBytes()); |
| 104 | 22 | buf.put(imageField.getBytes()); |
| 105 | 22 | } |
| 106 | |
|
| 107 | |
|
| 108 | 20 | logger.info("Convert flac tag at" + buf.position()); |
| 109 | 20 | if (paddingSize > 0) |
| 110 | |
{ |
| 111 | 10 | int paddingDataSize = paddingSize - MetadataBlockHeader.HEADER_LENGTH; |
| 112 | 10 | MetadataBlockHeader paddingHeader = new MetadataBlockHeader(true, BlockType.PADDING, paddingDataSize); |
| 113 | 10 | MetadataBlockDataPadding padding = new MetadataBlockDataPadding(paddingDataSize); |
| 114 | 10 | buf.put(paddingHeader.getBytes()); |
| 115 | 10 | buf.put(padding.getBytes()); |
| 116 | |
} |
| 117 | 20 | buf.rewind(); |
| 118 | 20 | return buf; |
| 119 | |
} |
| 120 | |
} |