Coverage Report - org.jaudiotagger.tag.id3.ID3v1Tag
 
Classes in this File Line Coverage Branch Coverage Complexity
ID3v1Tag
68%
221/323
58%
88/153
0
ID3v1Tag$1
100%
1/1
N/A
0
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: ID3v1Tag.java,v 1.27 2008/07/24 13:47:06 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.id3;
 25  
 
 26  
 import org.jaudiotagger.audio.generic.Utils;
 27  
 import org.jaudiotagger.audio.mp3.MP3File;
 28  
 import org.jaudiotagger.logging.ErrorMessage;
 29  
 import org.jaudiotagger.tag.*;
 30  
 import org.jaudiotagger.tag.id3.framebody.FrameBodyPIC;
 31  
 import org.jaudiotagger.tag.id3.valuepair.ImageFormats;
 32  
 import org.jaudiotagger.tag.datatype.Artwork;
 33  
 import org.jaudiotagger.tag.reference.GenreTypes;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.io.RandomAccessFile;
 37  
 import java.nio.ByteBuffer;
 38  
 import java.nio.channels.FileChannel;
 39  
 import java.util.*;
 40  
 import java.util.regex.Matcher;
 41  
 
 42  
 /**
 43  
  * Represents an ID3v1 tag.
 44  
  *
 45  
  * @author : Eric Farng
 46  
  * @author : Paul Taylor
 47  
  */
 48  
 public class ID3v1Tag extends AbstractID3v1Tag implements Tag
 49  
 {
 50  49
     static EnumMap<TagFieldKey, ID3v1FieldKey> tagFieldToID3v1Field = new EnumMap<TagFieldKey, ID3v1FieldKey>(TagFieldKey.class);
 51  
 
 52  
     static
 53  
     {
 54  49
         tagFieldToID3v1Field.put(TagFieldKey.ARTIST, ID3v1FieldKey.ARTIST);
 55  49
         tagFieldToID3v1Field.put(TagFieldKey.ALBUM, ID3v1FieldKey.ALBUM);
 56  49
         tagFieldToID3v1Field.put(TagFieldKey.TITLE, ID3v1FieldKey.TITLE);
 57  49
         tagFieldToID3v1Field.put(TagFieldKey.TRACK, ID3v1FieldKey.TRACK);
 58  49
         tagFieldToID3v1Field.put(TagFieldKey.YEAR, ID3v1FieldKey.YEAR);
 59  49
         tagFieldToID3v1Field.put(TagFieldKey.GENRE, ID3v1FieldKey.GENRE);
 60  49
         tagFieldToID3v1Field.put(TagFieldKey.COMMENT, ID3v1FieldKey.COMMENT);
 61  49
     }
 62  
 
 63  
     //For writing output
 64  
     protected static final String TYPE_COMMENT = "comment";
 65  
 
 66  
 
 67  
     protected static final int FIELD_COMMENT_LENGTH = 30;
 68  
     protected static final int FIELD_COMMENT_POS = 97;
 69  
     protected static final int BYTE_TO_UNSIGNED = 0xff;
 70  
 
 71  
     protected static final int GENRE_UNDEFINED = 0xff;
 72  
 
 73  
     /**
 74  
      *
 75  
      */
 76  1083
     protected String album = "";
 77  
 
 78  
     /**
 79  
      *
 80  
      */
 81  1083
     protected String artist = "";
 82  
 
 83  
     /**
 84  
      *
 85  
      */
 86  1083
     protected String comment = "";
 87  
 
 88  
     /**
 89  
      *
 90  
      */
 91  1083
     protected String title = "";
 92  
 
 93  
     /**
 94  
      *
 95  
      */
 96  1083
     protected String year = "";
 97  
 
 98  
     /**
 99  
      *
 100  
      */
 101  1083
     protected byte genre = (byte) -1;
 102  
 
 103  
 
 104  
     private static final byte RELEASE = 1;
 105  
     private static final byte MAJOR_VERSION = 0;
 106  
     private static final byte REVISION = 0;
 107  
 
 108  
     /**
 109  
      * Retrieve the Release
 110  
      */
 111  
     public byte getRelease()
 112  
     {
 113  4
         return RELEASE;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Retrieve the Major Version
 118  
      */
 119  
     public byte getMajorVersion()
 120  
     {
 121  4
         return MAJOR_VERSION;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Retrieve the Revision
 126  
      */
 127  
     public byte getRevision()
 128  
     {
 129  4
         return REVISION;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Creates a new ID3v1 datatype.
 134  
      */
 135  
     public ID3v1Tag()
 136  654
     {
 137  
 
 138  654
     }
 139  
 
 140  
     public ID3v1Tag(ID3v1Tag copyObject)
 141  
     {
 142  0
         super(copyObject);
 143  
 
 144  0
         this.album = new String(copyObject.album);
 145  0
         this.artist = new String(copyObject.artist);
 146  0
         this.comment = new String(copyObject.comment);
 147  0
         this.title = new String(copyObject.title);
 148  0
         this.year = new String(copyObject.year);
 149  0
         this.genre = copyObject.genre;
 150  0
     }
 151  
 
 152  
     public ID3v1Tag(AbstractTag mp3tag)
 153  3
     {
 154  
 
 155  3
         if (mp3tag != null)
 156  
         {
 157  
             ID3v11Tag convertedTag;
 158  3
             if (mp3tag instanceof ID3v1Tag)
 159  
             {
 160  0
                 throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument");
 161  
             }
 162  3
             if (mp3tag instanceof ID3v11Tag)
 163  
             {
 164  0
                 convertedTag = (ID3v11Tag) mp3tag;
 165  
             }
 166  
             else
 167  
             {
 168  3
                 convertedTag = new ID3v11Tag(mp3tag);
 169  
             }
 170  3
             this.album = new String(convertedTag.album);
 171  3
             this.artist = new String(convertedTag.artist);
 172  3
             this.comment = new String(convertedTag.comment);
 173  3
             this.title = new String(convertedTag.title);
 174  3
             this.year = new String(convertedTag.year);
 175  3
             this.genre = convertedTag.genre;
 176  
         }
 177  3
     }
 178  
 
 179  
     /**
 180  
      * Creates a new ID3v1 datatype.
 181  
      *
 182  
      * @param file
 183  
      * @param loggingFilename
 184  
      * @throws TagNotFoundException
 185  
      * @throws IOException
 186  
      */
 187  
     public ID3v1Tag(RandomAccessFile file, String loggingFilename) throws TagNotFoundException, IOException
 188  426
     {
 189  426
         setLoggingFilename(loggingFilename);
 190  
         FileChannel fc;
 191  
         ByteBuffer byteBuffer;
 192  
 
 193  426
         fc = file.getChannel();
 194  426
         fc.position(file.length() - TAG_LENGTH);
 195  426
         byteBuffer = ByteBuffer.allocate(TAG_LENGTH);
 196  426
         fc.read(byteBuffer);
 197  426
         byteBuffer.flip();
 198  426
         read(byteBuffer);
 199  67
     }
 200  
 
 201  
     /**
 202  
      * Creates a new ID3v1 datatype.
 203  
      *
 204  
      * @param file
 205  
      * @throws TagNotFoundException
 206  
      * @throws IOException
 207  
      * @deprecated use {@link #ID3v1Tag(RandomAccessFile,String)} instead
 208  
      */
 209  
     public ID3v1Tag(RandomAccessFile file) throws TagNotFoundException, IOException
 210  
     {
 211  0
         this(file, "");
 212  0
     }
 213  
 
 214  
     public void add(TagField field)
 215  
     {
 216  
         //TODO
 217  0
     }
 218  
 
 219  
     public List<TagField> get(String id)
 220  
     {
 221  
 
 222  2
         if (TagFieldKey.ARTIST.name().equals(id))
 223  
         {
 224  2
             return getArtist();
 225  
         }
 226  0
         else if (TagFieldKey.ALBUM.name().equals(id))
 227  
         {
 228  0
             return getAlbum();
 229  
         }
 230  0
         else if (TagFieldKey.TITLE.name().equals(id))
 231  
         {
 232  0
             return getTitle();
 233  
         }
 234  0
         else if (TagFieldKey.GENRE.name().equals(id))
 235  
         {
 236  0
             return getGenre();
 237  
         }
 238  0
         else if (TagFieldKey.YEAR.name().equals(id))
 239  
         {
 240  0
             return getYear();
 241  
         }
 242  0
         else if (TagFieldKey.COMMENT.name().equals(id))
 243  
         {
 244  0
             return getComment();
 245  
         }
 246  0
         return new ArrayList<TagField>();
 247  
     }
 248  
 
 249  
     public int getFieldCount()
 250  
     {
 251  0
         return 6;
 252  
     }
 253  
 
 254  
     protected List<TagField> returnFieldToList(ID3v1TagField field)
 255  
     {
 256  54
         List<TagField> fields = new ArrayList<TagField>();
 257  54
         fields.add(field);
 258  54
         return fields;
 259  
     }
 260  
 
 261  
     /**
 262  
      * Add Album
 263  
      * <p/>
 264  
      * <p>Only one album can be added so if one already exists it will be replaced.
 265  
      *
 266  
      * @param album
 267  
      */
 268  
     public void addAlbum(String album)
 269  
     {
 270  0
         setAlbum(album);
 271  0
     }
 272  
 
 273  
     /**
 274  
      * Set Album
 275  
      *
 276  
      * @param album
 277  
      */
 278  
     public void setAlbum(String album)
 279  
     {
 280  16
         if (album == null)
 281  
         {
 282  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 283  
         }
 284  16
         this.album = ID3Tags.truncate(album, this.FIELD_ALBUM_LENGTH);
 285  16
     }
 286  
 
 287  
     /**
 288  
      * Get Album
 289  
      *
 290  
      * @return album
 291  
      */
 292  
     public String getFirstAlbum()
 293  
     {
 294  31
         return album;
 295  
     }
 296  
 
 297  
     /**
 298  
      * @return album within list or empty if does not exist
 299  
      */
 300  
     public List<TagField> getAlbum()
 301  
     {
 302  8
         if (getFirstAlbum().length() > 0)
 303  
         {
 304  8
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ALBUM.name(), getFirstAlbum());
 305  8
             return returnFieldToList(field);
 306  
         }
 307  
         else
 308  
         {
 309  0
             return new ArrayList<TagField>();
 310  
         }
 311  
     }
 312  
 
 313  
 
 314  
     /**
 315  
      * Add Artist
 316  
      * <p/>
 317  
      * <p>Only one artist can be added so if one already exists it will be replaced.
 318  
      *
 319  
      * @param artist
 320  
      */
 321  
     public void addArtist(String artist)
 322  
     {
 323  0
         setArtist(album);
 324  0
     }
 325  
 
 326  
 
 327  
     /**
 328  
      * Set Artist
 329  
      *
 330  
      * @param artist
 331  
      */
 332  
     public void setArtist(String artist)
 333  
     {
 334  16
         if (artist == null)
 335  
         {
 336  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 337  
         }
 338  16
         this.artist = ID3Tags.truncate(artist, this.FIELD_ARTIST_LENGTH);
 339  16
     }
 340  
 
 341  
     /**
 342  
      * Get Artist
 343  
      *
 344  
      * @return artist
 345  
      */
 346  
     public String getFirstArtist()
 347  
     {
 348  50
         return artist;
 349  
     }
 350  
 
 351  
     /**
 352  
      * @return Artist within list or empty if does not exist
 353  
      */
 354  
     public List<TagField> getArtist()
 355  
     {
 356  10
         if (getFirstArtist().length() > 0)
 357  
         {
 358  10
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.ARTIST.name(), getFirstArtist());
 359  10
             return returnFieldToList(field);
 360  
         }
 361  
         else
 362  
         {
 363  0
             return new ArrayList<TagField>();
 364  
         }
 365  
     }
 366  
 
 367  
     /**
 368  
      * Add Comment
 369  
      * <p/>
 370  
      * <p>Only one comment can be added so if one already exists it will be replaced.
 371  
      *
 372  
      * @param comment
 373  
      */
 374  
     public void addComment(String comment)
 375  
     {
 376  0
         setComment(comment);
 377  0
     }
 378  
 
 379  
     /**
 380  
      * Set Comment
 381  
      *
 382  
      * @param comment
 383  
      * @throws IllegalArgumentException if comment null
 384  
      */
 385  
     public void setComment(String comment)
 386  
     {
 387  7
         if (comment == null)
 388  
         {
 389  1
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 390  
         }
 391  6
         this.comment = ID3Tags.truncate(comment, this.FIELD_COMMENT_LENGTH);
 392  6
     }
 393  
 
 394  
     /**
 395  
      * @return comment within list or empty if does not exist
 396  
      */
 397  
     public List<TagField> getComment()
 398  
     {
 399  8
         if (getFirstComment().length() > 0)
 400  
         {
 401  8
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.COMMENT.name(), getFirstComment());
 402  8
             return returnFieldToList(field);
 403  
         }
 404  
         else
 405  
         {
 406  0
             return new ArrayList<TagField>();
 407  
         }
 408  
     }
 409  
 
 410  
     /**
 411  
      * Get Comment
 412  
      *
 413  
      * @return comment
 414  
      */
 415  
     public String getFirstComment()
 416  
     {
 417  14
         return comment;
 418  
     }
 419  
 
 420  
     /**
 421  
      * Add Genre
 422  
      * <p/>
 423  
      * <p>Only one Genre can be added so if one already exists it will be replaced.
 424  
      *
 425  
      * @param genre
 426  
      */
 427  
     public void addGenre(String genre)
 428  
     {
 429  0
         setGenre(genre);
 430  0
     }
 431  
 
 432  
     /**
 433  
      * Sets the genreID,
 434  
      * <p/>
 435  
      * <p>ID3v1 only supports genres defined in a predefined list
 436  
      * so if unable to find value in list set 255, which seems to be the value
 437  
      * winamp uses for undefined.
 438  
      *
 439  
      * @param genreVal
 440  
      */
 441  
     public void setGenre(String genreVal)
 442  
     {
 443  13
         if (genreVal == null)
 444  
         {
 445  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 446  
         }
 447  13
         Integer genreID = GenreTypes.getInstanceOf().getIdForValue(genreVal);
 448  13
         if (genreID != null)
 449  
         {
 450  11
             this.genre = genreID.byteValue();
 451  
         }
 452  
         else
 453  
         {
 454  2
             this.genre = (byte) GENRE_UNDEFINED;
 455  
         }
 456  13
     }
 457  
 
 458  
     /**
 459  
      * Get Genre
 460  
      *
 461  
      * @return genre or empty string if not valid
 462  
      */
 463  
     public String getFirstGenre()
 464  
     {
 465  31
         Integer genreId = genre & this.BYTE_TO_UNSIGNED;
 466  31
         String genreValue = GenreTypes.getInstanceOf().getValueForId(genreId);
 467  31
         if (genreValue == null)
 468  
         {
 469  4
             return "";
 470  
         }
 471  
         else
 472  
         {
 473  27
             return genreValue;
 474  
         }
 475  
     }
 476  
 
 477  
     /**
 478  
      * Get Genre field
 479  
      * <p/>
 480  
      * <p>Only a single genre is available in ID3v1
 481  
      *
 482  
      * @return
 483  
      */
 484  
     public List<TagField> getGenre()
 485  
     {
 486  8
         if (getFirstGenre().length() > 0)
 487  
         {
 488  8
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.GENRE.name(), getFirstGenre());
 489  8
             return returnFieldToList(field);
 490  
         }
 491  
         else
 492  
         {
 493  0
             return new ArrayList<TagField>();
 494  
         }
 495  
     }
 496  
 
 497  
     /**
 498  
      * Add Title
 499  
      * <p/>
 500  
      * <p>Only one title can be added so if one already exists it will be replaced.
 501  
      *
 502  
      * @param title
 503  
      */
 504  
     public void addTitle(String title)
 505  
     {
 506  1
         setTitle(title);
 507  1
     }
 508  
 
 509  
     /**
 510  
      * Set Title
 511  
      *
 512  
      * @param title
 513  
      */
 514  
     public void setTitle(String title)
 515  
     {
 516  16
         if (title == null)
 517  
         {
 518  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 519  
         }
 520  16
         this.title = ID3Tags.truncate(title, this.FIELD_TITLE_LENGTH);
 521  16
     }
 522  
 
 523  
     /**
 524  
      * Get title
 525  
      *
 526  
      * @return Title
 527  
      */
 528  
     public String getFirstTitle()
 529  
     {
 530  37
         return title;
 531  
     }
 532  
 
 533  
     /**
 534  
      * Get title field
 535  
      * <p/>
 536  
      * <p>Only a single title is available in ID3v1
 537  
      *
 538  
      * @return
 539  
      */
 540  
     public List<TagField> getTitle()
 541  
     {
 542  8
         if (getFirstTitle().length() > 0)
 543  
         {
 544  8
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.TITLE.name(), getFirstTitle());
 545  8
             return returnFieldToList(field);
 546  
         }
 547  
         else
 548  
         {
 549  0
             return new ArrayList<TagField>();
 550  
         }
 551  
     }
 552  
 
 553  
     /**
 554  
      * Add Year
 555  
      * <p/>
 556  
      * <p>Only one year can be added so if one already exists it will be replaced.
 557  
      *
 558  
      * @param year
 559  
      */
 560  
     public void addYear(String year)
 561  
     {
 562  0
         setYear(year);
 563  0
     }
 564  
 
 565  
     /**
 566  
      * Set year
 567  
      *
 568  
      * @param year
 569  
      */
 570  
     public void setYear(String year)
 571  
     {
 572  12
         this.year = ID3Tags.truncate(year, this.FIELD_YEAR_LENGTH);
 573  12
     }
 574  
 
 575  
     /**
 576  
      * Get year
 577  
      *
 578  
      * @return year
 579  
      */
 580  
     public String getFirstYear()
 581  
     {
 582  30
         return year;
 583  
     }
 584  
 
 585  
     /**
 586  
      * Get year field
 587  
      * <p/>
 588  
      * <p>Only a single year is available in ID3v1
 589  
      *
 590  
      * @return
 591  
      */
 592  
     public List<TagField> getYear()
 593  
     {
 594  8
         if (getFirstYear().length() > 0)
 595  
         {
 596  8
             ID3v1TagField field = new ID3v1TagField(ID3v1FieldKey.YEAR.name(), getFirstYear());
 597  8
             return returnFieldToList(field);
 598  
         }
 599  
         else
 600  
         {
 601  0
             return new ArrayList<TagField>();
 602  
         }
 603  
     }
 604  
 
 605  
     public void addTrack(String track)
 606  
     {
 607  0
         throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
 608  
     }
 609  
 
 610  
     public String getFirstTrack()
 611  
     {
 612  0
         throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
 613  
     }
 614  
 
 615  
     public void setTrack(String track)
 616  
     {
 617  0
         throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
 618  
     }
 619  
 
 620  
     public List<TagField> getTrack()
 621  
     {
 622  0
         throw new UnsupportedOperationException("ID3v10 cannot store track numbers");
 623  
     }
 624  
 
 625  
     public TagField getFirstField(String id)
 626  
     {
 627  12
         List<TagField> results = null;
 628  
 
 629  12
         if (TagFieldKey.ARTIST.name().equals(id))
 630  
         {
 631  2
             results = getArtist();
 632  
         }
 633  10
         else if (TagFieldKey.ALBUM.name().equals(id))
 634  
         {
 635  2
             results = getAlbum();
 636  
         }
 637  8
         else if (TagFieldKey.TITLE.name().equals(id))
 638  
         {
 639  2
             results = getTitle();
 640  
         }
 641  6
         else if (TagFieldKey.GENRE.name().equals(id))
 642  
         {
 643  2
             results = getGenre();
 644  
         }
 645  4
         else if (TagFieldKey.YEAR.name().equals(id))
 646  
         {
 647  2
             results = getYear();
 648  
         }
 649  2
         else if (TagFieldKey.COMMENT.name().equals(id))
 650  
         {
 651  2
             results = getComment();
 652  
         }
 653  
 
 654  12
         if (results != null)
 655  
         {
 656  12
             if (results.size() > 0)
 657  
             {
 658  12
                 return results.get(0);
 659  
             }
 660  
         }
 661  0
         return null;
 662  
     }
 663  
 
 664  
     public Iterator<TagField> getFields()
 665  
     {
 666  0
         throw new UnsupportedOperationException("TODO:Not done yet");
 667  
     }
 668  
 
 669  
     public boolean hasCommonFields()
 670  
     {
 671  
         //TODO
 672  0
         return true;
 673  
     }
 674  
 
 675  
     public boolean hasField(String id)
 676  
     {
 677  
         //TODO
 678  0
         throw new UnsupportedOperationException("TODO:Not done yet");
 679  
     }
 680  
 
 681  
     public boolean isEmpty()
 682  
     {
 683  5
         if (getFirstTitle().length() > 0 || getFirstArtist().length() > 0 || getFirstAlbum().length() > 0 || getFirstGenre().length() > 0 || getFirstYear().length() > 0 || getFirstComment().length() > 0)
 684  
         {
 685  1
             return false;
 686  
         }
 687  4
         return true;
 688  
     }
 689  
 
 690  
 
 691  
     public void set(TagField field)
 692  
     {
 693  17
         TagFieldKey genericKey = TagFieldKey.valueOf(field.getId());
 694  17
         switch (genericKey)
 695  
         {
 696  
             case ARTIST:
 697  2
                 setArtist(field.toString());
 698  2
                 break;
 699  
 
 700  
             case ALBUM:
 701  2
                 setAlbum(field.toString());
 702  2
                 break;
 703  
 
 704  
             case TITLE:
 705  3
                 setTitle(field.toString());
 706  3
                 break;
 707  
 
 708  
             case GENRE:
 709  2
                 setGenre(field.toString());
 710  2
                 break;
 711  
 
 712  
             case YEAR:
 713  2
                 setYear(field.toString());
 714  2
                 break;
 715  
 
 716  
             case COMMENT:
 717  6
                 setComment(field.toString());
 718  
                 break;
 719  
         }
 720  15
     }
 721  
 
 722  
     /**
 723  
      * @param encoding
 724  
      * @return
 725  
      */
 726  
     public boolean setEncoding(String encoding)
 727  
     {
 728  0
         return true;
 729  
     }
 730  
 
 731  
     /**
 732  
      * Create Tag Field using generic key
 733  
      */
 734  
     public TagField createTagField(TagFieldKey genericKey, String value)
 735  
     {
 736  2
         return new ID3v1TagField(tagFieldToID3v1Field.get(genericKey).name(), value);
 737  
     }
 738  
 
 739  
     public String getEncoding()
 740  
     {
 741  0
         return "ISO-8859-1";
 742  
     }
 743  
 
 744  
     public TagField getFirstField(TagFieldKey genericKey)
 745  
     {
 746  0
         List<TagField> l = get(genericKey);
 747  0
         return (l.size() != 0) ? l.get(0) : null;
 748  
     }
 749  
 
 750  
     /**
 751  
      * Returns a {@linkplain List list} of {@link TagField} objects whose &quot;{@linkplain TagField#getId() id}&quot;
 752  
      * is the specified one.<br>
 753  
      *
 754  
      * @param genericKey The generic field key
 755  
      * @return A list of {@link TagField} objects with the given &quot;id&quot;.
 756  
      */
 757  
     public List<TagField> get(TagFieldKey genericKey)
 758  
     {
 759  12
         switch (genericKey)
 760  
         {
 761  
             case ARTIST:
 762  2
                 return getArtist();
 763  
 
 764  
             case ALBUM:
 765  2
                 return getAlbum();
 766  
 
 767  
             case TITLE:
 768  2
                 return getTitle();
 769  
 
 770  
             case GENRE:
 771  2
                 return getGenre();
 772  
 
 773  
             case YEAR:
 774  2
                 return getYear();
 775  
 
 776  
             case COMMENT:
 777  2
                 return getComment();
 778  
 
 779  
             default:
 780  0
                 return new ArrayList<TagField>();
 781  
         }
 782  
     }
 783  
 
 784  
 
 785  
     /**
 786  
      * Retrieve the first value that exists for this key id
 787  
      *
 788  
      * @param genericKey
 789  
      * @return
 790  
      */
 791  
     public String getFirst(String genericKey)
 792  
     {
 793  0
         TagFieldKey matchingKey = TagFieldKey.valueOf(genericKey);
 794  0
         if (matchingKey != null)
 795  
         {
 796  0
             return getFirst(matchingKey);
 797  
         }
 798  
         else
 799  
         {
 800  0
             return "";
 801  
         }
 802  
     }
 803  
 
 804  
 
 805  
     /**
 806  
      * Retrieve the first value that exists for this generic key
 807  
      *
 808  
      * @param genericKey
 809  
      * @return
 810  
      */
 811  
     public String getFirst(TagFieldKey genericKey)
 812  
     {
 813  15
         switch (genericKey)
 814  
         {
 815  
             case ARTIST:
 816  8
                 return getFirstArtist();
 817  
 
 818  
             case ALBUM:
 819  1
                 return getFirstAlbum();
 820  
 
 821  
             case TITLE:
 822  4
                 return getFirstTitle();
 823  
 
 824  
             case GENRE:
 825  1
                 return getFirstGenre();
 826  
 
 827  
             case YEAR:
 828  0
                 return getFirstYear();
 829  
 
 830  
             case TRACK:
 831  1
                 return getFirstTrack();
 832  
 
 833  
             case COMMENT:
 834  0
                 return getFirstComment();
 835  
 
 836  
             default:
 837  0
                 return "";
 838  
         }
 839  
     }
 840  
 
 841  
     /**
 842  
      * Delete any instance of tag fields with this key
 843  
      *
 844  
      * @param genericKey
 845  
      */
 846  
     public void deleteTagField(TagFieldKey genericKey)
 847  
     {
 848  13
         switch (genericKey)
 849  
         {
 850  
             case ARTIST:
 851  2
                 setArtist("");
 852  2
                 break;
 853  
 
 854  
             case ALBUM:
 855  2
                 setAlbum("");
 856  2
                 break;
 857  
 
 858  
             case TITLE:
 859  3
                 setTitle("");
 860  3
                 break;
 861  
 
 862  
             case GENRE:
 863  2
                 setGenre("");
 864  2
                 break;
 865  
 
 866  
             case YEAR:
 867  2
                 setYear("");
 868  2
                 break;
 869  
 
 870  
             case COMMENT:
 871  2
                 setComment("");
 872  
                 break;
 873  
         }
 874  13
     }
 875  
 
 876  
     /**
 877  
      * @param obj
 878  
      * @return true if this and obj are equivalent
 879  
      */
 880  
     public boolean equals(Object obj)
 881  
     {
 882  0
         if ((obj instanceof ID3v1Tag) == false)
 883  
         {
 884  0
             return false;
 885  
         }
 886  0
         ID3v1Tag object = (ID3v1Tag) obj;
 887  0
         if (this.album.equals(object.album) == false)
 888  
         {
 889  0
             return false;
 890  
         }
 891  0
         if (this.artist.equals(object.artist) == false)
 892  
         {
 893  0
             return false;
 894  
         }
 895  0
         if (this.comment.equals(object.comment) == false)
 896  
         {
 897  0
             return false;
 898  
         }
 899  0
         if (this.genre != object.genre)
 900  
         {
 901  0
             return false;
 902  
         }
 903  0
         if (this.title.equals(object.title) == false)
 904  
         {
 905  0
             return false;
 906  
         }
 907  0
         if (this.year.equals(object.year) == false)
 908  
         {
 909  0
             return false;
 910  
         }
 911  0
         return super.equals(obj);
 912  
     }
 913  
 
 914  
     /**
 915  
      * @return an iterator to iterate through the fields of the tag
 916  
      */
 917  
     public Iterator iterator()
 918  
     {
 919  0
         return new ID3v1Iterator(this);
 920  
     }
 921  
 
 922  
 
 923  
     /**
 924  
      * @param byteBuffer
 925  
      * @throws TagNotFoundException
 926  
      */
 927  
     public void read(ByteBuffer byteBuffer) throws TagNotFoundException
 928  
     {
 929  426
         if (seek(byteBuffer) == false)
 930  
         {
 931  359
             throw new TagNotFoundException(getLoggingFilename() + ":" + "ID3v1 tag not found");
 932  
         }
 933  67
         logger.finer(getLoggingFilename() + ":" + "Reading v1 tag");
 934  
         //Do single file read of data to cut down on file reads
 935  67
         byte[] dataBuffer = new byte[TAG_LENGTH];
 936  67
         byteBuffer.position(0);
 937  67
         byteBuffer.get(dataBuffer, 0, TAG_LENGTH);
 938  67
         title = Utils.getString(dataBuffer, FIELD_TITLE_POS, this.FIELD_TITLE_LENGTH, "ISO-8859-1").trim();
 939  67
         Matcher m = endofStringPattern.matcher(title);
 940  67
         if (m.find() == true)
 941  
         {
 942  0
             title = title.substring(0, m.start());
 943  
         }
 944  67
         artist = Utils.getString(dataBuffer, FIELD_ARTIST_POS, this.FIELD_ARTIST_LENGTH, "ISO-8859-1").trim();
 945  67
         m = endofStringPattern.matcher(artist);
 946  67
         if (m.find() == true)
 947  
         {
 948  0
             artist = artist.substring(0, m.start());
 949  
         }
 950  67
         album = Utils.getString(dataBuffer, FIELD_ALBUM_POS, this.FIELD_ALBUM_LENGTH, "ISO-8859-1").trim();
 951  67
         m = endofStringPattern.matcher(album);
 952  67
         logger.finest(getLoggingFilename() + ":" + "Orig Album is:" + comment + ":");
 953  67
         if (m.find() == true)
 954  
         {
 955  0
             album = album.substring(0, m.start());
 956  0
             logger.finest(getLoggingFilename() + ":" + "Album is:" + album + ":");
 957  
         }
 958  67
         year = Utils.getString(dataBuffer, FIELD_YEAR_POS, this.FIELD_YEAR_LENGTH, "ISO-8859-1").trim();
 959  67
         m = endofStringPattern.matcher(year);
 960  67
         if (m.find() == true)
 961  
         {
 962  0
             year = year.substring(0, m.start());
 963  
         }
 964  67
         comment = Utils.getString(dataBuffer, FIELD_COMMENT_POS, this.FIELD_COMMENT_LENGTH, "ISO-8859-1").trim();
 965  67
         m = endofStringPattern.matcher(comment);
 966  67
         logger.finest(getLoggingFilename() + ":" + "Orig Comment is:" + comment + ":");
 967  67
         if (m.find() == true)
 968  
         {
 969  0
             comment = comment.substring(0, m.start());
 970  0
             logger.finest(getLoggingFilename() + ":" + "Comment is:" + comment + ":");
 971  
         }
 972  67
         genre = dataBuffer[this.FIELD_GENRE_POS];
 973  
 
 974  67
     }
 975  
 
 976  
     /**
 977  
      * Does a tag of this version exist within the byteBuffer
 978  
      *
 979  
      * @return whether tag exists within the byteBuffer
 980  
      */
 981  
     public boolean seek(ByteBuffer byteBuffer)
 982  
     {
 983  678
         byte[] buffer = new byte[FIELD_TAGID_LENGTH];
 984  
         // read the TAG value
 985  678
         byteBuffer.get(buffer, 0, FIELD_TAGID_LENGTH);
 986  678
         return (Arrays.equals(buffer, TAG_ID));
 987  
     }
 988  
 
 989  
     /**
 990  
      * Write this tag to the file, replacing any tag previously existing
 991  
      *
 992  
      * @param file
 993  
      * @throws IOException
 994  
      */
 995  
     public void write(RandomAccessFile file) throws IOException
 996  
     {
 997  55
         logger.info("Saving ID3v1 tag to file");
 998  55
         byte[] buffer = new byte[TAG_LENGTH];
 999  
         int i;
 1000  
         String str;
 1001  55
         delete(file);
 1002  55
         file.seek(file.length());
 1003  
         //Copy the TAGID into new buffer
 1004  55
         System.arraycopy(TAG_ID, this.FIELD_TAGID_POS, buffer, this.FIELD_TAGID_POS, TAG_ID.length);
 1005  55
         int offset = this.FIELD_TITLE_POS;
 1006  55
         if (TagOptionSingleton.getInstance().isId3v1SaveTitle())
 1007  
         {
 1008  55
             str = ID3Tags.truncate(title, this.FIELD_TITLE_LENGTH);
 1009  89
             for (i = 0; i < str.length(); i++)
 1010  
             {
 1011  34
                 buffer[i + offset] = (byte) str.charAt(i);
 1012  
             }
 1013  
         }
 1014  55
         offset = this.FIELD_ARTIST_POS;
 1015  55
         if (TagOptionSingleton.getInstance().isId3v1SaveArtist())
 1016  
         {
 1017  55
             str = ID3Tags.truncate(artist, this.FIELD_ARTIST_LENGTH);
 1018  483
             for (i = 0; i < str.length(); i++)
 1019  
             {
 1020  428
                 buffer[i + offset] = (byte) str.charAt(i);
 1021  
             }
 1022  
         }
 1023  55
         offset = this.FIELD_ALBUM_POS;
 1024  55
         if (TagOptionSingleton.getInstance().isId3v1SaveAlbum())
 1025  
         {
 1026  55
             str = ID3Tags.truncate(album, this.FIELD_ALBUM_LENGTH);
 1027  432
             for (i = 0; i < str.length(); i++)
 1028  
             {
 1029  377
                 buffer[i + offset] = (byte) str.charAt(i);
 1030  
             }
 1031  
         }
 1032  55
         offset = this.FIELD_YEAR_POS;
 1033  55
         if (TagOptionSingleton.getInstance().isId3v1SaveYear())
 1034  
         {
 1035  55
             str = ID3Tags.truncate(year, AbstractID3v1Tag.FIELD_YEAR_LENGTH);
 1036  71
             for (i = 0; i < str.length(); i++)
 1037  
             {
 1038  16
                 buffer[i + offset] = (byte) str.charAt(i);
 1039  
             }
 1040  
         }
 1041  55
         offset = this.FIELD_COMMENT_POS;
 1042  55
         if (TagOptionSingleton.getInstance().isId3v1SaveComment())
 1043  
         {
 1044  55
             str = ID3Tags.truncate(comment, this.FIELD_COMMENT_LENGTH);
 1045  69
             for (i = 0; i < str.length(); i++)
 1046  
             {
 1047  14
                 buffer[i + offset] = (byte) str.charAt(i);
 1048  
             }
 1049  
         }
 1050  55
         offset = this.FIELD_GENRE_POS;
 1051  55
         if (TagOptionSingleton.getInstance().isId3v1SaveGenre())
 1052  
         {
 1053  55
             buffer[offset] = genre;
 1054  
         }
 1055  55
         file.write(buffer);
 1056  55
         logger.info("Saved ID3v1 tag to file");
 1057  55
     }
 1058  
 
 1059  
     /**
 1060  
      * Create strcutured representation of this item.
 1061  
      */
 1062  
     public void createStructure()
 1063  
     {
 1064  0
         MP3File.getStructureFormatter().openHeadingElement(TYPE_TAG, getIdentifier());
 1065  
         //Header
 1066  0
         MP3File.getStructureFormatter().addElement(TYPE_TITLE, this.title);
 1067  0
         MP3File.getStructureFormatter().addElement(TYPE_ARTIST, this.artist);
 1068  0
         MP3File.getStructureFormatter().addElement(TYPE_ALBUM, this.album);
 1069  0
         MP3File.getStructureFormatter().addElement(TYPE_YEAR, this.year);
 1070  0
         MP3File.getStructureFormatter().addElement(TYPE_COMMENT, this.comment);
 1071  0
         MP3File.getStructureFormatter().addElement(TYPE_GENRE, this.genre);
 1072  0
         MP3File.getStructureFormatter().closeHeadingElement(TYPE_TAG);
 1073  0
     }
 1074  
 
 1075  
     public List<Artwork> getArtworkList()
 1076  
     {
 1077  0
        return Collections.emptyList();
 1078  
     }
 1079  
 
 1080  
     public Artwork getFirstArtwork()
 1081  
     {           
 1082  0
         return null;
 1083  
     }
 1084  
 
 1085  
     public TagField  createArtworkField(Artwork artwork) throws FieldDataInvalidException
 1086  
     {
 1087  0
         throw new UnsupportedOperationException("Not implemented for this format");
 1088  
     }
 1089  
 
 1090  
     public void createAndSetArtworkField(Artwork artwork) throws FieldDataInvalidException
 1091  
     {
 1092  0
         throw new UnsupportedOperationException("Not implemented for this format");
 1093  
     }
 1094  
 }