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