Coverage Report - org.jaudiotagger.tag.lyrics3.Lyrics3v1
 
Classes in this File Line Coverage Branch Coverage Complexity
Lyrics3v1
0%
0/84
0%
0/32
2.312
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: Lyrics3v1.java 836 2009-11-12 15:44:07Z paultaylor $
 6  
  *
 7  
  *  MusicTag Copyright (C)2003,2004
 8  
  *
 9  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 10  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 11  
  *  or (at your option) any later version.
 12  
  *
 13  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 14  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
  *  See the GNU Lesser General Public License for more details.
 16  
  *
 17  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 18  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 19  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 20  
  *
 21  
  * Description:
 22  
  */
 23  
 
 24  
 package org.jaudiotagger.tag.lyrics3;
 25  
 
 26  
 import org.jaudiotagger.tag.TagException;
 27  
 import org.jaudiotagger.tag.TagNotFoundException;
 28  
 import org.jaudiotagger.tag.id3.AbstractTag;
 29  
 import org.jaudiotagger.tag.id3.ID3Tags;
 30  
 import org.jaudiotagger.tag.id3.ID3v1Tag;
 31  
 
 32  
 import java.io.IOException;
 33  
 import java.io.RandomAccessFile;
 34  
 import java.nio.ByteBuffer;
 35  
 import java.util.Iterator;
 36  
 
 37  
 public class Lyrics3v1 extends AbstractLyrics3
 38  
 {
 39  
     /**
 40  
      *
 41  
      */
 42  0
     private String lyric = "";
 43  
 
 44  
     /**
 45  
      * Creates a new Lyrics3v1 datatype.
 46  
      */
 47  
     public Lyrics3v1()
 48  0
     {
 49  0
     }
 50  
 
 51  
     public Lyrics3v1(Lyrics3v1 copyObject)
 52  
     {
 53  0
         super(copyObject);
 54  0
         this.lyric = copyObject.lyric;
 55  0
     }
 56  
 
 57  
     public Lyrics3v1(AbstractTag mp3Tag)
 58  0
     {
 59  0
         if (mp3Tag != null)
 60  
         {
 61  
             Lyrics3v2 lyricTag;
 62  
 
 63  0
             if (mp3Tag instanceof Lyrics3v1)
 64  
             {
 65  0
                 throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument");
 66  
             }
 67  0
             else if (mp3Tag instanceof Lyrics3v2)
 68  
             {
 69  0
                 lyricTag = (Lyrics3v2) mp3Tag;
 70  
             }
 71  
             else
 72  
             {
 73  0
                 lyricTag = new Lyrics3v2(mp3Tag);
 74  
             }
 75  
 
 76  
             FieldFrameBodyLYR lyricField;
 77  0
             lyricField = (FieldFrameBodyLYR) lyricTag.getField("LYR").getBody();
 78  0
             this.lyric = lyricField.getLyric();
 79  
         }
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Creates a new Lyrics3v1 datatype.
 84  
      *
 85  
      * @param file
 86  
      * @throws TagNotFoundException
 87  
      * @throws java.io.IOException
 88  
      * @param byteBuffer
 89  
      */
 90  
     public Lyrics3v1(ByteBuffer byteBuffer) throws TagNotFoundException, java.io.IOException
 91  0
     {
 92  
         try
 93  
         {
 94  0
             this.read(byteBuffer);
 95  
         }
 96  0
         catch (TagException e)
 97  
         {
 98  0
             e.printStackTrace();
 99  0
         }
 100  0
     }
 101  
 
 102  
     /**
 103  
      * @return
 104  
      */
 105  
     public String getIdentifier()
 106  
     {
 107  0
         return "Lyrics3v1.00";
 108  
     }
 109  
 
 110  
     /**
 111  
      * @param lyric
 112  
      */
 113  
     public void setLyric(String lyric)
 114  
     {
 115  0
         this.lyric = ID3Tags.truncate(lyric, 5100);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * @return
 120  
      */
 121  
     public String getLyric()
 122  
     {
 123  0
         return lyric;
 124  
     }
 125  
 
 126  
     /**
 127  
      * @return
 128  
      */
 129  
     public int getSize()
 130  
     {
 131  0
         return "LYRICSBEGIN".length() + lyric.length() + "LYRICSEND".length();
 132  
     }
 133  
 
 134  
     /**
 135  
      * @param obj
 136  
      * @return
 137  
      */
 138  
     public boolean isSubsetOf(Object obj)
 139  
     {
 140  0
         return (obj instanceof Lyrics3v1) && (((Lyrics3v1) obj).lyric.contains(this.lyric));
 141  
 
 142  
     }
 143  
 
 144  
     /**
 145  
      * @param obj
 146  
      * @return
 147  
      */
 148  
     public boolean equals(Object obj)
 149  
     {
 150  0
         if (!(obj instanceof Lyrics3v1))
 151  
         {
 152  0
             return false;
 153  
         }
 154  
 
 155  0
         Lyrics3v1 object = (Lyrics3v1) obj;
 156  
 
 157  0
         return this.lyric.equals(object.lyric) && super.equals(obj);
 158  
 
 159  
     }
 160  
 
 161  
     /**
 162  
      * @return
 163  
      * @throws java.lang.UnsupportedOperationException
 164  
      *
 165  
      */
 166  
     public Iterator iterator()
 167  
     {
 168  
         /**
 169  
          * @todo Implement this org.jaudiotagger.tag.AbstractMP3Tag abstract method
 170  
          */
 171  0
         throw new java.lang.UnsupportedOperationException("Method iterator() not yet implemented.");
 172  
     }
 173  
 
 174  
     /**
 175  
      * TODO implement
 176  
      *
 177  
      * @param byteBuffer
 178  
      * @return
 179  
      * @throws IOException
 180  
      */
 181  
     public boolean seek(ByteBuffer byteBuffer)
 182  
     {
 183  0
         return false;
 184  
     }
 185  
 
 186  
     /**
 187  
      * @param byteBuffer
 188  
      * @throws TagNotFoundException
 189  
      * @throws IOException
 190  
      */
 191  
     public void read(ByteBuffer byteBuffer) throws TagException
 192  
     {
 193  0
         byte[] buffer = new byte[5100 + 9 + 11];
 194  
         String lyricBuffer;
 195  
 
 196  0
         if (!seek(byteBuffer))
 197  
         {
 198  0
             throw new TagNotFoundException("ID3v1 tag not found");
 199  
         }
 200  
 
 201  0
         byteBuffer.get(buffer);
 202  0
         lyricBuffer = new String(buffer);
 203  
 
 204  0
         lyric = lyricBuffer.substring(0, lyricBuffer.indexOf("LYRICSEND"));
 205  0
     }
 206  
 
 207  
     /**
 208  
      * @param file
 209  
      * @return
 210  
      * @throws IOException
 211  
      */
 212  
     public boolean seek(RandomAccessFile file) throws IOException
 213  
     {
 214  0
         byte[] buffer = new byte[5100 + 9 + 11];
 215  
         String lyricsEnd;
 216  
         String lyricsStart;
 217  
         long offset;
 218  
 
 219  
         // check right before the ID3 1.0 tag for the lyrics tag
 220  0
         file.seek(file.length() - 128 - 9);
 221  0
         file.read(buffer, 0, 9);
 222  0
         lyricsEnd = new String(buffer, 0, 9);
 223  
 
 224  0
         if (lyricsEnd.equals("LYRICSEND"))
 225  
         {
 226  0
             offset = file.getFilePointer();
 227  
         }
 228  
         else
 229  
         {
 230  
             // check the end of the file for a lyrics tag incase an ID3
 231  
             // tag wasn't placed after it.
 232  0
             file.seek(file.length() - 9);
 233  0
             file.read(buffer, 0, 9);
 234  0
             lyricsEnd = new String(buffer, 0, 9);
 235  
 
 236  0
             if (lyricsEnd.equals("LYRICSEND"))
 237  
             {
 238  0
                 offset = file.getFilePointer();
 239  
             }
 240  
             else
 241  
             {
 242  0
                 return false;
 243  
             }
 244  
         }
 245  
 
 246  
         // the tag can at most only be 5100 bytes
 247  0
         offset -= (5100 + 9 + 11);
 248  0
         file.seek(offset);
 249  0
         file.read(buffer);
 250  0
         lyricsStart = new String(buffer);
 251  
 
 252  
         // search for the tag
 253  0
         int i = lyricsStart.indexOf("LYRICSBEGIN");
 254  
 
 255  0
         if (i == -1)
 256  
         {
 257  0
             return false;
 258  
         }
 259  
 
 260  0
         file.seek(offset + i + 11);
 261  
 
 262  0
         return true;
 263  
     }
 264  
 
 265  
     /**
 266  
      * @return
 267  
      */
 268  
     public String toString()
 269  
     {
 270  0
         String str = getIdentifier() + " " + this.getSize() + "\n";
 271  
 
 272  0
         return str + lyric;
 273  
     }
 274  
 
 275  
     /**
 276  
      * @param file
 277  
      * @throws IOException
 278  
      */
 279  
     public void write(RandomAccessFile file) throws IOException
 280  
     {
 281  
         String str;
 282  
         int offset;
 283  
         byte[] buffer;
 284  
         ID3v1Tag id3v1tag;
 285  
 
 286  0
         id3v1tag = null;
 287  
 
 288  0
         delete(file);
 289  0
         file.seek(file.length());
 290  
 
 291  0
         buffer = new byte[lyric.length() + 11 + 9];
 292  
 
 293  0
         str = "LYRICSBEGIN";
 294  
 
 295  0
         for (int i = 0; i < str.length(); i++)
 296  
         {
 297  0
             buffer[i] = (byte) str.charAt(i);
 298  
         }
 299  
 
 300  0
         offset = str.length();
 301  
 
 302  0
         str = ID3Tags.truncate(lyric, 5100);
 303  
 
 304  0
         for (int i = 0; i < str.length(); i++)
 305  
         {
 306  0
             buffer[i + offset] = (byte) str.charAt(i);
 307  
         }
 308  
 
 309  0
         offset += str.length();
 310  
 
 311  0
         str = "LYRICSEND";
 312  
 
 313  0
         for (int i = 0; i < str.length(); i++)
 314  
         {
 315  0
             buffer[i + offset] = (byte) str.charAt(i);
 316  
         }
 317  
 
 318  0
         offset += str.length();
 319  
 
 320  0
         file.write(buffer, 0, offset);
 321  
 
 322  0
         if (id3v1tag != null)
 323  
         {
 324  0
             id3v1tag.write(file);
 325  
         }
 326  0
     }
 327  
 
 328  
 }