Coverage Report - org.jaudiotagger.tag.vorbiscomment.VorbisCommentCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
VorbisCommentCreator
89%
17/19
100%
4/4
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.tag.vorbiscomment;
 20  
 
 21  
 import org.jaudiotagger.audio.generic.AbstractTagCreator;
 22  
 import org.jaudiotagger.audio.generic.Utils;
 23  
 import org.jaudiotagger.tag.Tag;
 24  
 import org.jaudiotagger.tag.TagField;
 25  
 
 26  
 import java.io.ByteArrayOutputStream;
 27  
 import java.io.IOException;
 28  
 import java.io.UnsupportedEncodingException;
 29  
 import java.nio.ByteBuffer;
 30  
 import java.util.Iterator;
 31  
 
 32  
 /**
 33  
  * Create the raw packet data for a Vorbis Comment Tag
 34  
  */
 35  84
 public class VorbisCommentCreator extends AbstractTagCreator
 36  
 {
 37  
     /**
 38  
      * Convert tagdata to rawdata ready for writing to file
 39  
      *
 40  
      * @param tag
 41  
      * @param padding
 42  
      * @return
 43  
      * @throws UnsupportedEncodingException
 44  
      */
 45  
     //TODO padding parameter currently ignored
 46  
     public ByteBuffer convert(Tag tag, int padding) throws UnsupportedEncodingException
 47  
     {
 48  
         try
 49  
         {
 50  38
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 51  
 
 52  
             //Vendor
 53  38
             String vendorString = ((VorbisCommentTag) tag).getVendor();
 54  38
             int vendorLength = Utils.getUTF8Bytes(vendorString).length;
 55  38
             baos.write(Utils.getSizeLEInt32(vendorLength));
 56  38
             baos.write(Utils.getUTF8Bytes(vendorString));
 57  
 
 58  
             //User Comment List
 59  38
             int listLength = tag.getFieldCount() - 1; //Remove Vendor from count         
 60  38
             baos.write(Utils.getSizeLEInt32(listLength));
 61  
 
 62  
             //Add metadata raw content
 63  38
             Iterator<TagField> it = tag.getFields();
 64  339
             while (it.hasNext())
 65  
             {
 66  301
                 TagField frame = it.next();
 67  301
                 if (frame.getId().equals(VorbisCommentFieldKey.VENDOR.name()))
 68  
                 {
 69  
                     //this is always stored above so ignore                    
 70  
                 }
 71  
                 else
 72  
                 {
 73  263
                     baos.write(frame.getRawContent());
 74  
                 }
 75  301
             }
 76  
 
 77  
             //Put into ByteBuffer
 78  38
             ByteBuffer buf = ByteBuffer.wrap(baos.toByteArray());
 79  38
             buf.rewind();
 80  38
             return buf;
 81  
         }
 82  0
         catch (IOException ioe)
 83  
         {
 84  
             //Should never happen as not writing to file at this point
 85  0
             throw new RuntimeException(ioe);
 86  
         }
 87  
     }
 88  
 }