Coverage Report - org.jaudiotagger.audio.flac.FlacTagCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
FlacTagCreator
100%
38/38
100%
18/18
0
 
 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  42
 public class FlacTagCreator extends AbstractTagCreator
 39  
 {
 40  
     // Logger Object
 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  
      * @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  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  
         //If there are other metadata blocks
 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  
         //Images
 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  
         //Padding
 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  
 }