Coverage Report - org.jaudiotagger.audio.flac.FlacTagCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
FlacTagCreator
100%
38/38
100%
18/18
10
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 RaphaĆ«l Slinckx <raphael@slinckx.net>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 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  
  * Create the tag data ready for writing to flac file
 37  
  */
 38  4
 public class FlacTagCreator extends AbstractTagCreator
 39  
 {
 40  
     // Logger Object
 41  4
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac");
 42  
 
 43  
     public static final int DEFAULT_PADDING = 4000;
 44  4
     private static final VorbisCommentCreator creator = new VorbisCommentCreator();
 45  
 
 46  
     /**
 47  
      * @param tag
 48  
      * @param paddingSize extra padding to be added
 49  
      * @return
 50  
      * @throws UnsupportedEncodingException
 51  
      */
 52  
     public ByteBuffer convert(Tag tag, int paddingSize) throws UnsupportedEncodingException
 53  
     {
 54  76
         logger.info("Convert flac tag:padding:" + paddingSize);
 55  76
         FlacTag flacTag = (FlacTag) tag;
 56  
 
 57  76
         int tagLength = 0;
 58  76
         ByteBuffer vorbiscomment = null;
 59  76
         if (flacTag.getVorbisCommentTag() != null)
 60  
         {
 61  60
             vorbiscomment = creator.convert(flacTag.getVorbisCommentTag());
 62  60
             tagLength = vorbiscomment.capacity() + MetadataBlockHeader.HEADER_LENGTH;
 63  
         }
 64  76
         for (MetadataBlockDataPicture image : flacTag.getImages())
 65  
         {
 66  82
             tagLength += image.getBytes().length + MetadataBlockHeader.HEADER_LENGTH;
 67  
         }
 68  
 
 69  76
         logger.info("Convert flac tag:taglength:" + tagLength);
 70  76
         ByteBuffer buf = ByteBuffer.allocate(tagLength + paddingSize);
 71  
 
 72  
         MetadataBlockHeader vorbisHeader;
 73  
         //If there are other metadata blocks
 74  76
         if (flacTag.getVorbisCommentTag() != null)
 75  
         {
 76  60
             if ((paddingSize > 0) || (flacTag.getImages().size() > 0))
 77  
             {
 78  51
                 vorbisHeader = new MetadataBlockHeader(false, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
 79  
             }
 80  
             else
 81  
             {
 82  9
                 vorbisHeader = new MetadataBlockHeader(true, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
 83  
             }
 84  60
             buf.put(vorbisHeader.getBytes());
 85  60
             buf.put(vorbiscomment);
 86  
         }
 87  
 
 88  
         //Images
 89  76
         ListIterator<MetadataBlockDataPicture> li = flacTag.getImages().listIterator();
 90  158
         while (li.hasNext())
 91  
         {
 92  82
             MetadataBlockDataPicture imageField = li.next();
 93  
             MetadataBlockHeader imageHeader;
 94  
 
 95  82
             if (paddingSize > 0 || li.hasNext())
 96  
             {
 97  61
                 imageHeader = new MetadataBlockHeader(false, BlockType.PICTURE, imageField.getLength());
 98  
             }
 99  
             else
 100  
             {
 101  21
                 imageHeader = new MetadataBlockHeader(true, BlockType.PICTURE, imageField.getLength());
 102  
             }
 103  82
             buf.put(imageHeader.getBytes());
 104  82
             buf.put(imageField.getBytes());
 105  82
         }
 106  
 
 107  
         //Padding
 108  76
         logger.info("Convert flac tag at" + buf.position());
 109  76
         if (paddingSize > 0)
 110  
         {
 111  38
             int paddingDataSize = paddingSize - MetadataBlockHeader.HEADER_LENGTH;
 112  38
             MetadataBlockHeader paddingHeader = new MetadataBlockHeader(true, BlockType.PADDING, paddingDataSize);
 113  38
             MetadataBlockDataPadding padding = new MetadataBlockDataPadding(paddingDataSize);
 114  38
             buf.put(paddingHeader.getBytes());
 115  38
             buf.put(padding.getBytes());
 116  
         }
 117  76
         buf.rewind();
 118  76
         return buf;
 119  
     }
 120  
 }