Coverage Report - org.jaudiotagger.tag.lyrics3.Lyrics3v1
 
Classes in this File Line Coverage Branch Coverage Complexity
Lyrics3v1
0%
0/94
0%
0/28
2.438
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: Lyrics3v1.java,v 1.9 2008/07/21 10:45:49 paultaylor Exp $
 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 = new String(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 = new String(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  
      */
 89  
     public Lyrics3v1(ByteBuffer byteBuffer) throws TagNotFoundException, java.io.IOException
 90  0
     {
 91  
         try
 92  
         {
 93  0
             this.read(byteBuffer);
 94  
         }
 95  0
         catch (TagException e)
 96  
         {
 97  0
             e.printStackTrace();
 98  0
         }
 99  0
     }
 100  
 
 101  
     /**
 102  
      * @return
 103  
      */
 104  
     public String getIdentifier()
 105  
     {
 106  0
         return "Lyrics3v1.00";
 107  
     }
 108  
 
 109  
     /**
 110  
      * @param lyric
 111  
      */
 112  
     public void setLyric(String lyric)
 113  
     {
 114  0
         this.lyric = ID3Tags.truncate(lyric, 5100);
 115  0
     }
 116  
 
 117  
     /**
 118  
      * @return
 119  
      */
 120  
     public String getLyric()
 121  
     {
 122  0
         return lyric;
 123  
     }
 124  
 
 125  
     /**
 126  
      * @return
 127  
      */
 128  
     public int getSize()
 129  
     {
 130  0
         return "LYRICSBEGIN".length() + lyric.length() + "LYRICSEND".length();
 131  
     }
 132  
 
 133  
     /**
 134  
      * @param obj
 135  
      * @return
 136  
      */
 137  
     public boolean isSubsetOf(Object obj)
 138  
     {
 139  0
         if ((obj instanceof Lyrics3v1) == false)
 140  
         {
 141  0
             return false;
 142  
         }
 143  
 
 144  0
         return (((Lyrics3v1) obj).lyric.contains(this.lyric));
 145  
     }
 146  
 
 147  
     /**
 148  
      * @param obj
 149  
      * @return
 150  
      */
 151  
     public boolean equals(Object obj)
 152  
     {
 153  0
         if ((obj instanceof Lyrics3v1) == false)
 154  
         {
 155  0
             return false;
 156  
         }
 157  
 
 158  0
         Lyrics3v1 object = (Lyrics3v1) obj;
 159  
 
 160  0
         if (this.lyric.equals(object.lyric) == false)
 161  
         {
 162  0
             return false;
 163  
         }
 164  
 
 165  0
         return super.equals(obj);
 166  
     }
 167  
 
 168  
     /**
 169  
      * @return
 170  
      * @throws java.lang.UnsupportedOperationException
 171  
      *
 172  
      */
 173  
     public Iterator iterator()
 174  
     {
 175  
         /**
 176  
          * @todo Implement this org.jaudiotagger.tag.AbstractMP3Tag abstract method
 177  
          */
 178  0
         throw new java.lang.UnsupportedOperationException("Method iterator() not yet implemented.");
 179  
     }
 180  
 
 181  
     /**
 182  
      * TODO implement
 183  
      *
 184  
      * @param byteBuffer
 185  
      * @return
 186  
      * @throws IOException
 187  
      */
 188  
     public boolean seek(ByteBuffer byteBuffer)
 189  
     {
 190  0
         return false;
 191  
     }
 192  
 
 193  
     /**
 194  
      * @param byteBuffer
 195  
      * @throws TagNotFoundException
 196  
      * @throws IOException
 197  
      */
 198  
     public void read(ByteBuffer byteBuffer) throws TagException
 199  
     {
 200  0
         byte[] buffer = new byte[5100 + 9 + 11];
 201  
         String lyricBuffer;
 202  
 
 203  0
         if (seek(byteBuffer) == false)
 204  
         {
 205  0
             throw new TagNotFoundException("ID3v1 tag not found");
 206  
         }
 207  
 
 208  0
         byteBuffer.get(buffer);
 209  0
         lyricBuffer = new String(buffer);
 210  
 
 211  0
         lyric = lyricBuffer.substring(0, lyricBuffer.indexOf("LYRICSEND"));
 212  0
     }
 213  
 
 214  
     /**
 215  
      * @param file
 216  
      * @return
 217  
      * @throws IOException
 218  
      */
 219  
     public boolean seek(RandomAccessFile file) throws IOException
 220  
     {
 221  0
         byte[] buffer = new byte[5100 + 9 + 11];
 222  0
         String lyricsEnd = "";
 223  0
         String lyricsStart = "";
 224  0
         long offset = 0;
 225  
 
 226  
         // check right before the ID3 1.0 tag for the lyrics tag
 227  0
         file.seek(file.length() - 128 - 9);
 228  0
         file.read(buffer, 0, 9);
 229  0
         lyricsEnd = new String(buffer, 0, 9);
 230  
 
 231  0
         if (lyricsEnd.equals("LYRICSEND"))
 232  
         {
 233  0
             offset = file.getFilePointer();
 234  
         }
 235  
         else
 236  
         {
 237  
             // check the end of the file for a lyrics tag incase an ID3
 238  
             // tag wasn't placed after it.
 239  0
             file.seek(file.length() - 9);
 240  0
             file.read(buffer, 0, 9);
 241  0
             lyricsEnd = new String(buffer, 0, 9);
 242  
 
 243  0
             if (lyricsEnd.equals("LYRICSEND"))
 244  
             {
 245  0
                 offset = file.getFilePointer();
 246  
             }
 247  
             else
 248  
             {
 249  0
                 return false;
 250  
             }
 251  
         }
 252  
 
 253  
         // the tag can at most only be 5100 bytes
 254  0
         offset -= (5100 + 9 + 11);
 255  0
         file.seek(offset);
 256  0
         file.read(buffer);
 257  0
         lyricsStart = new String(buffer);
 258  
 
 259  
         // search for the tag
 260  0
         int i = lyricsStart.indexOf("LYRICSBEGIN");
 261  
 
 262  0
         if (i == -1)
 263  
         {
 264  0
             return false;
 265  
         }
 266  
 
 267  0
         file.seek(offset + i + 11);
 268  
 
 269  0
         return true;
 270  
     }
 271  
 
 272  
     /**
 273  
      * @return
 274  
      */
 275  
     public String toString()
 276  
     {
 277  0
         String str = getIdentifier() + " " + this.getSize() + "\n";
 278  
 
 279  0
         return str + lyric;
 280  
     }
 281  
 
 282  
     /**
 283  
      * @param file
 284  
      * @throws IOException
 285  
      */
 286  
     public void write(RandomAccessFile file) throws IOException
 287  
     {
 288  0
         String str = "";
 289  0
         int offset = 0;
 290  
         byte[] buffer;
 291  0
         ID3v1Tag id3v1tag = null;
 292  
 
 293  0
         id3v1tag = null;
 294  
 
 295  0
         delete(file);
 296  0
         file.seek(file.length());
 297  
 
 298  0
         buffer = new byte[lyric.length() + 11 + 9];
 299  
 
 300  0
         str = "LYRICSBEGIN";
 301  
 
 302  0
         for (int i = 0; i < str.length(); i++)
 303  
         {
 304  0
             buffer[i] = (byte) str.charAt(i);
 305  
         }
 306  
 
 307  0
         offset = str.length();
 308  
 
 309  0
         str = ID3Tags.truncate(lyric, 5100);
 310  
 
 311  0
         for (int i = 0; i < str.length(); i++)
 312  
         {
 313  0
             buffer[i + offset] = (byte) str.charAt(i);
 314  
         }
 315  
 
 316  0
         offset += str.length();
 317  
 
 318  0
         str = "LYRICSEND";
 319  
 
 320  0
         for (int i = 0; i < str.length(); i++)
 321  
         {
 322  0
             buffer[i + offset] = (byte) str.charAt(i);
 323  
         }
 324  
 
 325  0
         offset += str.length();
 326  
 
 327  0
         file.write(buffer, 0, offset);
 328  
 
 329  0
         if (id3v1tag != null)
 330  
         {
 331  0
             id3v1tag.write(file);
 332  
         }
 333  0
     }
 334  
 
 335  
 }