| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 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 | |
|
| 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 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public FieldFrameBodyIMG(String imageString) |
| 68 | 0 | { |
| 69 | 0 | readString(imageString); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public FieldFrameBodyIMG(Lyrics3Image image) |
| 78 | 0 | { |
| 79 | 0 | images.add(image); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public FieldFrameBodyIMG(ByteBuffer byteBuffer) throws InvalidTagException |
| 88 | 0 | { |
| 89 | 0 | this.read(byteBuffer); |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public String getIdentifier() |
| 96 | |
{ |
| 97 | 0 | return "IMG"; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 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); |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | return size - 2; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 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 | |
|
| 143 | |
|
| 144 | |
public void setValue(String value) |
| 145 | |
{ |
| 146 | 0 | readString(value); |
| 147 | 0 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public String getValue() |
| 153 | |
{ |
| 154 | 0 | return writeString(); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public void addImage(Lyrics3Image image) |
| 161 | |
{ |
| 162 | 0 | images.add(image); |
| 163 | 0 | } |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 214 | 0 | byteBuffer.get(buffer); |
| 215 | 0 | imageString = new String(buffer); |
| 216 | 0 | readString(imageString); |
| 217 | 0 | } |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 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 | |
|
| 236 | |
|
| 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 | |
|
| 280 | |
|
| 281 | |
private void readString(String imageString) |
| 282 | |
{ |
| 283 | |
|
| 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 | |
|
| 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 | |
|
| 334 | |
|
| 335 | |
protected void setupObjectList() |
| 336 | |
{ |
| 337 | |
|
| 338 | 0 | } |
| 339 | |
} |