Coverage Report - org.jaudiotagger.tag.lyrics3.FieldFrameBodyIMG
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldFrameBodyIMG
0%
0/100
0%
0/36
0
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: FieldFrameBodyIMG.java,v 1.11 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.InvalidTagException;
 27  
 import org.jaudiotagger.tag.TagOptionSingleton;
 28  
 import org.jaudiotagger.tag.datatype.Lyrics3Image;
 29  
 
 30  
 import java.io.RandomAccessFile;
 31  
 import java.nio.ByteBuffer;
 32  
 import java.util.ArrayList;
 33  
 import java.util.Iterator;
 34  
 
 35  
 public class FieldFrameBodyIMG extends AbstractLyrics3v2FieldFrameBody
 36  
 {
 37  
     /**
 38  
      *
 39  
      */
 40  0
     private ArrayList<Lyrics3Image> images = new ArrayList<Lyrics3Image>();
 41  
 
 42  
     /**
 43  
      * Creates a new FieldBodyIMG datatype.
 44  
      */
 45  
     public FieldFrameBodyIMG()
 46  0
     {
 47  0
     }
 48  
 
 49  
     public FieldFrameBodyIMG(FieldFrameBodyIMG copyObject)
 50  
     {
 51  0
         super(copyObject);
 52  
 
 53  
         Lyrics3Image old;
 54  
 
 55  0
         for (int i = 0; i < copyObject.images.size(); i++)
 56  
         {
 57  0
             old = copyObject.images.get(i);
 58  0
             this.images.add(new Lyrics3Image(old));
 59  
         }
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Creates a new FieldBodyIMG datatype.
 64  
      *
 65  
      * @param imageString
 66  
      */
 67  
     public FieldFrameBodyIMG(String imageString)
 68  0
     {
 69  0
         readString(imageString);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Creates a new FieldBodyIMG datatype.
 74  
      *
 75  
      * @param image
 76  
      */
 77  
     public FieldFrameBodyIMG(Lyrics3Image image)
 78  0
     {
 79  0
         images.add(image);
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Creates a new FieldBodyIMG datatype.
 84  
      *
 85  
      * @throws InvalidTagException
 86  
      */
 87  
     public FieldFrameBodyIMG(ByteBuffer byteBuffer) throws InvalidTagException
 88  0
     {
 89  0
         this.read(byteBuffer);
 90  0
     }
 91  
 
 92  
     /**
 93  
      * @return
 94  
      */
 95  
     public String getIdentifier()
 96  
     {
 97  0
         return "IMG";
 98  
     }
 99  
 
 100  
     /**
 101  
      * @return
 102  
      */
 103  
     public int getSize()
 104  
     {
 105  0
         int size = 0;
 106  
         Lyrics3Image image;
 107  
 
 108  0
         for (Object image1 : images)
 109  
         {
 110  0
             image = (Lyrics3Image) image1;
 111  0
             size += (image.getSize() + 2); // add CRLF pair
 112  
         }
 113  
 
 114  0
         return size - 2; // cut off trailing crlf pair
 115  
     }
 116  
 
 117  
     /**
 118  
      * @param obj
 119  
      * @return
 120  
      */
 121  
     public boolean isSubsetOf(Object obj)
 122  
     {
 123  0
         if ((obj instanceof FieldFrameBodyIMG) == false)
 124  
         {
 125  0
             return false;
 126  
         }
 127  
 
 128  0
         ArrayList<Lyrics3Image> superset = ((FieldFrameBodyIMG) obj).images;
 129  
 
 130  0
         for (Object image : images)
 131  
         {
 132  0
             if (superset.contains(image) == false)
 133  
             {
 134  0
                 return false;
 135  
             }
 136  
         }
 137  
 
 138  0
         return super.isSubsetOf(obj);
 139  
     }
 140  
 
 141  
     /**
 142  
      * @param value
 143  
      */
 144  
     public void setValue(String value)
 145  
     {
 146  0
         readString(value);
 147  0
     }
 148  
 
 149  
     /**
 150  
      * @return
 151  
      */
 152  
     public String getValue()
 153  
     {
 154  0
         return writeString();
 155  
     }
 156  
 
 157  
     /**
 158  
      * @param image
 159  
      */
 160  
     public void addImage(Lyrics3Image image)
 161  
     {
 162  0
         images.add(image);
 163  0
     }
 164  
 
 165  
     /**
 166  
      * @param obj
 167  
      * @return
 168  
      */
 169  
     public boolean equals(Object obj)
 170  
     {
 171  0
         if ((obj instanceof FieldFrameBodyIMG) == false)
 172  
         {
 173  0
             return false;
 174  
         }
 175  
 
 176  0
         FieldFrameBodyIMG object = (FieldFrameBodyIMG) obj;
 177  
 
 178  0
         if (this.images.equals(object.images) == false)
 179  
         {
 180  0
             return false;
 181  
         }
 182  
 
 183  0
         return super.equals(obj);
 184  
     }
 185  
 
 186  
     /**
 187  
      * @return
 188  
      */
 189  
     public Iterator<Lyrics3Image> iterator()
 190  
     {
 191  0
         return images.iterator();
 192  
     }
 193  
 
 194  
 
 195  
     public void read(ByteBuffer byteBuffer) throws InvalidTagException
 196  
     {
 197  
         String imageString;
 198  
 
 199  0
         byte[] buffer = new byte[5];
 200  
 
 201  
         // read the 5 character size
 202  0
         byteBuffer.get(buffer, 0, 5);
 203  
 
 204  0
         int size = Integer.parseInt(new String(buffer, 0, 5));
 205  
 
 206  0
         if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false))
 207  
         {
 208  0
             throw new InvalidTagException("Lyircs3v2 Field has size of zero.");
 209  
         }
 210  
 
 211  0
         buffer = new byte[size];
 212  
 
 213  
         // read the SIZE length description
 214  0
         byteBuffer.get(buffer);
 215  0
         imageString = new String(buffer);
 216  0
         readString(imageString);
 217  0
     }
 218  
 
 219  
     /**
 220  
      * @return
 221  
      */
 222  
     public String toString()
 223  
     {
 224  0
         String str = getIdentifier() + " : ";
 225  
 
 226  0
         for (Object image : images)
 227  
         {
 228  0
             str += (image.toString() + " ; ");
 229  
         }
 230  
 
 231  0
         return str;
 232  
     }
 233  
 
 234  
     /**
 235  
      * @param file
 236  
      * @throws java.io.IOException
 237  
      */
 238  
     public void write(RandomAccessFile file) throws java.io.IOException
 239  
     {
 240  0
         int size = 0;
 241  0
         int offset = 0;
 242  0
         byte[] buffer = new byte[5];
 243  
         String str;
 244  
 
 245  0
         size = getSize();
 246  0
         str = Integer.toString(size);
 247  
 
 248  0
         for (int i = 0; i < (5 - str.length()); i++)
 249  
         {
 250  0
             buffer[i] = (byte) '0';
 251  
         }
 252  
 
 253  0
         offset += (5 - str.length());
 254  
 
 255  0
         for (int i = 0; i < str.length(); i++)
 256  
         {
 257  0
             buffer[i + offset] = (byte) str.charAt(i);
 258  
         }
 259  
 
 260  0
         offset += str.length();
 261  
 
 262  0
         file.write(buffer, 0, 5);
 263  
 
 264  0
         if (size > 0)
 265  
         {
 266  0
             str = writeString();
 267  0
             buffer = new byte[str.length()];
 268  
 
 269  0
             for (int i = 0; i < str.length(); i++)
 270  
             {
 271  0
                 buffer[i] = (byte) str.charAt(i);
 272  
             }
 273  
 
 274  0
             file.write(buffer);
 275  
         }
 276  0
     }
 277  
 
 278  
     /**
 279  
      * @param imageString
 280  
      */
 281  
     private void readString(String imageString)
 282  
     {
 283  
         // now read each picture and put in the vector;
 284  
         Lyrics3Image image;
 285  0
         String token = "";
 286  0
         int offset = 0;
 287  0
         int delim = imageString.indexOf(Lyrics3v2Fields.CRLF);
 288  0
         images = new ArrayList<Lyrics3Image>();
 289  
 
 290  0
         while (delim >= 0)
 291  
         {
 292  0
             token = imageString.substring(offset, delim);
 293  0
             image = new Lyrics3Image("Image", this);
 294  0
             image.setFilename(token);
 295  0
             images.add(image);
 296  0
             offset = delim + Lyrics3v2Fields.CRLF.length();
 297  0
             delim = imageString.indexOf(Lyrics3v2Fields.CRLF, offset);
 298  
         }
 299  
 
 300  0
         if (offset < imageString.length())
 301  
         {
 302  0
             token = imageString.substring(offset);
 303  0
             image = new Lyrics3Image("Image", this);
 304  0
             image.setFilename(token);
 305  0
             images.add(image);
 306  
         }
 307  0
     }
 308  
 
 309  
     /**
 310  
      * @return
 311  
      */
 312  
     private String writeString()
 313  
     {
 314  0
         String str = "";
 315  
         Lyrics3Image image;
 316  
 
 317  0
         for (Object image1 : images)
 318  
         {
 319  0
             image = (Lyrics3Image) image1;
 320  0
             str += (image.writeString() + Lyrics3v2Fields.CRLF);
 321  
         }
 322  
 
 323  0
         if (str.length() > 2)
 324  
         {
 325  0
             return str.substring(0, str.length() - 2);
 326  
         }
 327  
 
 328  0
         return str;
 329  
     }
 330  
 
 331  
 
 332  
     /**
 333  
      * TODO
 334  
      */
 335  
     protected void setupObjectList()
 336  
     {
 337  
 
 338  0
     }
 339  
 }