Coverage Report - org.jaudiotagger.tag.vorbiscomment.VorbisCommentReader
 
Classes in this File Line Coverage Branch Coverage Complexity
VorbisCommentReader
100%
38/38
100%
8/8
2.667
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 4  
  * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
 5  
  * 
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation; either
 9  
  * version 2.1 of the License, or (at your option) any later version.
 10  
  *  
 11  
  * This library is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  
  * Lesser General Public License for more details.
 15  
  * 
 16  
  * You should have received a copy of the GNU Lesser General Public
 17  
  * License along with this library; if not, write to the Free Software
 18  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 19  
  */
 20  
 package org.jaudiotagger.tag.vorbiscomment;
 21  
 
 22  
 import org.jaudiotagger.audio.exceptions.CannotReadException;
 23  
 import org.jaudiotagger.audio.generic.Utils;
 24  
 import org.jaudiotagger.audio.ogg.util.VorbisHeader;
 25  
 import org.jaudiotagger.fix.Fix;
 26  
 import org.jaudiotagger.logging.ErrorMessage;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.util.logging.Logger;
 30  
 
 31  
 /**
 32  
  * Create the VorbisCommentTag by reading from the raw packet data
 33  
  * <p/>
 34  
  * <p>This is in the same format whether encoded with Ogg or Flac
 35  
  * except the framing bit is only present when used within Ogg Vorbis
 36  
  * <p/>
 37  
  * <pre>
 38  
  * From the http://xiph.org/vorbis/doc/Vorbis_I_spec.html#vorbis-spec-comment
 39  
  * Read decodes the packet data using the following algorithm:
 40  
  *  [vendor_length] = read an unsigned integer of 32 bits
 41  
  *  [vendor_string] = read a UTF-8 vector as [vendor_length] octets
 42  
  *  [user_comment_list_length] = read an unsigned integer of 32 bits
 43  
  *  iterate [user_comment_list_length] times {
 44  
  *      5) [length] = read an unsigned integer of 32 bits
 45  
  *      6) this iteration's user comment = read a UTF-8 vector as [length] octets
 46  
  *    }
 47  
  *  [framing_bit] = read a single bit as boolean
 48  
  *  if ( [framing_bit] unset or end-of-packet ) then ERROR
 49  
  *  done.
 50  
  * </pre>
 51  
  */
 52  
 public class VorbisCommentReader
 53  
 {
 54  
     // Logger Object
 55  42
     public static Logger logger = Logger.getLogger("org.jaudiotagger.tag.vorbiscomment.VorbisCommentReader");
 56  
 
 57  
     public static final int FIELD_VENDOR_LENGTH_POS = 0;
 58  
     public static final int FIELD_VENDOR_STRING_POS = 4;
 59  
 
 60  
     public static final int FIELD_VENDOR_LENGTH_LENGTH = 4;
 61  
     public static final int FIELD_USER_COMMENT_LIST_LENGTH = 4;
 62  
     public static final int FIELD_COMMENT_LENGTH_LENGTH = 4;
 63  
 
 64  
     private Fix fix;
 65  
 
 66  
     public VorbisCommentReader()
 67  181
     {
 68  
 
 69  181
     }
 70  
 
 71  
     public VorbisCommentReader(Fix fix)
 72  1
     {
 73  1
         this.fix = fix;
 74  1
     }
 75  
 
 76  
     /**
 77  
      * @param rawdata
 78  
      * @return logical representation of VorbisCommentTag
 79  
      * @throws IOException
 80  
      * @throws CannotReadException
 81  
      */
 82  
     public VorbisCommentTag read(byte[] rawdata, boolean isFramingBit) throws IOException, CannotReadException
 83  
     {
 84  
 
 85  66
         VorbisCommentTag tag = new VorbisCommentTag();
 86  
 
 87  66
         byte[] b = new byte[FIELD_VENDOR_LENGTH_LENGTH];
 88  66
         System.arraycopy(rawdata, FIELD_VENDOR_LENGTH_POS, b, FIELD_VENDOR_LENGTH_POS, FIELD_VENDOR_LENGTH_LENGTH);
 89  66
         int pos = FIELD_VENDOR_LENGTH_LENGTH;
 90  66
         int vendorStringLength = Utils.getIntLE(b);
 91  
 
 92  66
         b = new byte[vendorStringLength];
 93  66
         System.arraycopy(rawdata, pos, b, 0, vendorStringLength);
 94  66
         pos += vendorStringLength;
 95  66
         tag.setVendor(new String(b, VorbisHeader.CHARSET_UTF_8));
 96  
 
 97  66
         b = new byte[FIELD_USER_COMMENT_LIST_LENGTH];
 98  66
         System.arraycopy(rawdata, pos, b, 0, FIELD_USER_COMMENT_LIST_LENGTH);
 99  66
         pos += FIELD_USER_COMMENT_LIST_LENGTH;
 100  
 
 101  66
         int userComments = Utils.getIntLE(b);
 102  66
         logger.info("Number of user comments:" + userComments);
 103  66
         if (fix == Fix.FIX_OGG_VORBIS_COMMENT_NOT_COUNTING_EMPTY_COLUMNS)
 104  
         {
 105  1
             userComments++;
 106  
         }
 107  396
         for (int i = 0; i < userComments; i++)
 108  
         {
 109  330
             b = new byte[FIELD_COMMENT_LENGTH_LENGTH];
 110  330
             System.arraycopy(rawdata, pos, b, 0, FIELD_COMMENT_LENGTH_LENGTH);
 111  330
             pos += FIELD_COMMENT_LENGTH_LENGTH;
 112  
 
 113  330
             int commentLength = Utils.getIntLE(b);
 114  330
             logger.info("Next Comment Length:" + commentLength);
 115  330
             b = new byte[commentLength];
 116  330
             System.arraycopy(rawdata, pos, b, 0, commentLength);
 117  330
             pos += commentLength;
 118  
 
 119  330
             VorbisCommentTagField fieldComment = new VorbisCommentTagField(b);
 120  330
             logger.info("Adding:" + fieldComment.getId());
 121  330
             tag.add(fieldComment);
 122  
         }
 123  
 
 124  
         //Check framing bit, only exists when vorbisComment used within OggVorbis       
 125  66
         if (isFramingBit)
 126  
         {
 127  45
             if ((rawdata[pos] & 0x01) != 1)
 128  
             {
 129  1
                 throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_FRAMING_BIT.getMsg((rawdata[pos] & 0x01)));
 130  
             }
 131  
         }
 132  65
         return tag;
 133  
     }
 134  
 }
 135