| 1 | package org.farng.mp3.lyrics3; |
| 2 | |
| 3 | import org.farng.mp3.InvalidTagException; |
| 4 | import org.farng.mp3.TagConstant; |
| 5 | import org.farng.mp3.TagOptionSingleton; |
| 6 | import org.farng.mp3.object.ObjectLyrics3Image; |
| 7 | |
| 8 | import java.io.RandomAccessFile; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.Iterator; |
| 11 | |
| 12 | /** |
| 13 | * Link to an image files (BMP or JPG format). Image lines include filename, description and timestamp separated by |
| 14 | * delimiter - two ASCII chars 124 ("||"). Description and timestamp are optional, but if timestamp is used, and there |
| 15 | * is no description, two delimiters ("||||") should be used between the filename and the timestamp. Multiple images are |
| 16 | * allowed by using a [CR][LF] delimiter between each image line. No [CR][LF] is needed after the last image line. |
| 17 | * Number of images is not limited (except by the field size).<BR><B>Filename</B> can be in one of these formats: <UL> |
| 18 | * <LI>Filename only - when the image is located in the same path as the MP3 file (preferred, since if you move the mp3 |
| 19 | * file this will still be correct) <LI>Relative Path + Filename - when the image is located in a subdirectory below the |
| 20 | * MP3 file (i.e. images\cover.jpg) <LI>Full path + Filename - when the image is located in a totally different path or |
| 21 | * drive. This will not work if the image is moved or drive letters has changed, and so should be avoided if possible |
| 22 | * (i.e. c:\images\artist.jpg)</LI></UL><B>Description</B> can be up to 250 chars long.<BR><B>Timestamp</B> must be |
| 23 | * formatted like the lyrics timestamp which is "[mm:ss]". If an image has a timestamp, then the visible image will |
| 24 | * automatically switch to that image on the timestamp play time, just the same as the selected lyrics line is switched |
| 25 | * based on timestamps. |
| 26 | * |
| 27 | * @author Eric Farng |
| 28 | * @version $Revision: 1.4 $ |
| 29 | */ |
| 30 | public class FieldBodyIMG extends AbstractLyrics3v2FieldBody { |
| 31 | |
| 32 | private ArrayList images = new ArrayList(); |
| 33 | |
| 34 | /** |
| 35 | * Creates a new FieldBodyIMG object. |
| 36 | */ |
| 37 | public FieldBodyIMG() { |
| 38 | super(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Creates a new FieldBodyIMG object. |
| 43 | */ |
| 44 | public FieldBodyIMG(final FieldBodyIMG copyObject) { |
| 45 | super(copyObject); |
| 46 | ObjectLyrics3Image oldObject; |
| 47 | for (int i = 0; i < copyObject.images.size(); i++) { |
| 48 | oldObject = (ObjectLyrics3Image) copyObject.images.get(i); |
| 49 | this.images.add(new ObjectLyrics3Image(oldObject)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a new FieldBodyIMG object. |
| 55 | */ |
| 56 | public FieldBodyIMG(final String imageString) { |
| 57 | readString(imageString); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Creates a new FieldBodyIMG object. |
| 62 | */ |
| 63 | public FieldBodyIMG(final ObjectLyrics3Image image) { |
| 64 | this.images.add(image); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a new FieldBodyIMG object. |
| 69 | */ |
| 70 | public FieldBodyIMG(final RandomAccessFile file) throws InvalidTagException, java.io.IOException { |
| 71 | this.read(file); |
| 72 | } |
| 73 | |
| 74 | public String getIdentifier() { |
| 75 | return "IMG"; |
| 76 | } |
| 77 | |
| 78 | public int getSize() { |
| 79 | int size = 0; |
| 80 | ObjectLyrics3Image image; |
| 81 | for (int i = 0; i < this.images.size(); i++) { |
| 82 | image = (ObjectLyrics3Image) this.images.get(i); |
| 83 | size += (image.getSize() + 2); // add CRLF pair |
| 84 | } |
| 85 | return size - 2; // cut off trailing crlf pair |
| 86 | } |
| 87 | |
| 88 | public boolean isSubsetOf(final Object object) { |
| 89 | if ((object instanceof FieldBodyIMG) == false) { |
| 90 | return false; |
| 91 | } |
| 92 | final ArrayList superset = ((FieldBodyIMG) object).images; |
| 93 | for (int i = 0; i < this.images.size(); i++) { |
| 94 | if (superset.contains(this.images.get(i)) == false) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | return super.isSubsetOf(object); |
| 99 | } |
| 100 | |
| 101 | public void setValue(final String value) { |
| 102 | readString(value); |
| 103 | } |
| 104 | |
| 105 | public String getValue() { |
| 106 | return writeString(); |
| 107 | } |
| 108 | |
| 109 | public void addImage(final ObjectLyrics3Image image) { |
| 110 | this.images.add(image); |
| 111 | } |
| 112 | |
| 113 | public boolean equals(final Object obj) { |
| 114 | if ((obj instanceof FieldBodyIMG) == false) { |
| 115 | return false; |
| 116 | } |
| 117 | final FieldBodyIMG fieldBodyIMG = (FieldBodyIMG) obj; |
| 118 | if (this.images.equals(fieldBodyIMG.images) == false) { |
| 119 | return false; |
| 120 | } |
| 121 | return super.equals(obj); |
| 122 | } |
| 123 | |
| 124 | public Iterator iterator() { |
| 125 | return this.images.iterator(); |
| 126 | } |
| 127 | |
| 128 | protected void setupObjectList() { |
| 129 | // throw new UnsupportedOperationException(); |
| 130 | } |
| 131 | |
| 132 | public void read(final RandomAccessFile file) throws InvalidTagException, java.io.IOException { |
| 133 | final String imageString; |
| 134 | byte[] buffer = new byte[5]; |
| 135 | |
| 136 | // read the 5 character size |
| 137 | file.read(buffer, 0, 5); |
| 138 | final int size = Integer.parseInt(new String(buffer, 0, 5)); |
| 139 | if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false)) { |
| 140 | throw new InvalidTagException("Lyircs3v2 Field has size of zero."); |
| 141 | } |
| 142 | buffer = new byte[size]; |
| 143 | |
| 144 | // read the SIZE length description |
| 145 | file.read(buffer); |
| 146 | imageString = new String(buffer); |
| 147 | readString(imageString); |
| 148 | } |
| 149 | |
| 150 | public String toString() { |
| 151 | String str = getIdentifier() + " : "; |
| 152 | for (int i = 0; i < this.images.size(); i++) { |
| 153 | str += (this.images.get(i).toString() + " ; "); |
| 154 | } |
| 155 | return str; |
| 156 | } |
| 157 | |
| 158 | public void write(final RandomAccessFile file) throws java.io.IOException { |
| 159 | final int size; |
| 160 | int offset = 0; |
| 161 | byte[] buffer = new byte[5]; |
| 162 | String str; |
| 163 | size = getSize(); |
| 164 | str = Integer.toString(size); |
| 165 | for (int i = 0; i < (5 - str.length()); i++) { |
| 166 | buffer[i] = (byte) '0'; |
| 167 | } |
| 168 | offset += (5 - str.length()); |
| 169 | for (int i = 0; i < str.length(); i++) { |
| 170 | buffer[i + offset] = (byte) str.charAt(i); |
| 171 | } |
| 172 | offset += str.length(); |
| 173 | file.write(buffer, 0, 5); |
| 174 | if (size > 0) { |
| 175 | str = writeString(); |
| 176 | buffer = new byte[str.length()]; |
| 177 | for (int i = 0; i < str.length(); i++) { |
| 178 | buffer[i] = (byte) str.charAt(i); |
| 179 | } |
| 180 | file.write(buffer); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param imageString |
| 186 | */ |
| 187 | private void readString(final String imageString) { |
| 188 | // now read each picture and put in the vector; |
| 189 | ObjectLyrics3Image image; |
| 190 | String token; |
| 191 | int offset = 0; |
| 192 | int delim = imageString.indexOf(TagConstant.SEPERATOR_LINE); |
| 193 | this.images = new ArrayList(); |
| 194 | while (delim >= 0) { |
| 195 | token = imageString.substring(offset, delim); |
| 196 | image = new ObjectLyrics3Image("Image"); |
| 197 | image.setFilename(token); |
| 198 | this.images.add(image); |
| 199 | offset = delim + TagConstant.SEPERATOR_LINE.length(); |
| 200 | delim = imageString.indexOf(TagConstant.SEPERATOR_LINE, offset); |
| 201 | } |
| 202 | if (offset < imageString.length()) { |
| 203 | token = imageString.substring(offset); |
| 204 | image = new ObjectLyrics3Image("Image"); |
| 205 | image.setFilename(token); |
| 206 | this.images.add(image); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private String writeString() { |
| 211 | String str = ""; |
| 212 | ObjectLyrics3Image image; |
| 213 | for (int i = 0; i < this.images.size(); i++) { |
| 214 | image = (ObjectLyrics3Image) this.images.get(i); |
| 215 | str += (image.writeString() + TagConstant.SEPERATOR_LINE); |
| 216 | } |
| 217 | if (str.length() > 2) { |
| 218 | return str.substring(0, str.length() - 2); |
| 219 | } |
| 220 | return str; |
| 221 | } |
| 222 | } |