| 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 | |
|
| 88 | |
public FieldFrameBodyIMG(ByteBuffer byteBuffer) throws InvalidTagException |
| 89 | 0 | { |
| 90 | 0 | this.read(byteBuffer); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public String getIdentifier() |
| 97 | |
{ |
| 98 | 0 | return "IMG"; |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 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); |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | return size - 2; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 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 | |
|
| 144 | |
|
| 145 | |
public void setValue(String value) |
| 146 | |
{ |
| 147 | 0 | readString(value); |
| 148 | 0 | } |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public String getValue() |
| 154 | |
{ |
| 155 | 0 | return writeString(); |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public void addImage(Lyrics3Image image) |
| 162 | |
{ |
| 163 | 0 | images.add(image); |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 211 | 0 | byteBuffer.get(buffer); |
| 212 | 0 | imageString = new String(buffer); |
| 213 | 0 | readString(imageString); |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 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 | |
|
| 233 | |
|
| 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 | |
|
| 277 | |
|
| 278 | |
private void readString(String imageString) |
| 279 | |
{ |
| 280 | |
|
| 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 | |
|
| 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 | |
|
| 331 | |
|
| 332 | |
protected void setupObjectList() |
| 333 | |
{ |
| 334 | |
|
| 335 | 0 | } |
| 336 | |
} |