| 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.TagException; |
| 27 | |
import org.jaudiotagger.tag.TagNotFoundException; |
| 28 | |
import org.jaudiotagger.tag.id3.AbstractTag; |
| 29 | |
import org.jaudiotagger.tag.id3.ID3Tags; |
| 30 | |
import org.jaudiotagger.tag.id3.ID3v1Tag; |
| 31 | |
|
| 32 | |
import java.io.IOException; |
| 33 | |
import java.io.RandomAccessFile; |
| 34 | |
import java.nio.ByteBuffer; |
| 35 | |
import java.util.Iterator; |
| 36 | |
|
| 37 | |
public class Lyrics3v1 extends AbstractLyrics3 |
| 38 | |
{ |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | private String lyric = ""; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public Lyrics3v1() |
| 48 | 0 | { |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public Lyrics3v1(Lyrics3v1 copyObject) |
| 52 | |
{ |
| 53 | 0 | super(copyObject); |
| 54 | 0 | this.lyric = copyObject.lyric; |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public Lyrics3v1(AbstractTag mp3Tag) |
| 58 | 0 | { |
| 59 | 0 | if (mp3Tag != null) |
| 60 | |
{ |
| 61 | |
Lyrics3v2 lyricTag; |
| 62 | |
|
| 63 | 0 | if (mp3Tag instanceof Lyrics3v1) |
| 64 | |
{ |
| 65 | 0 | throw new UnsupportedOperationException("Copy Constructor not called. Please type cast the argument"); |
| 66 | |
} |
| 67 | 0 | else if (mp3Tag instanceof Lyrics3v2) |
| 68 | |
{ |
| 69 | 0 | lyricTag = (Lyrics3v2) mp3Tag; |
| 70 | |
} |
| 71 | |
else |
| 72 | |
{ |
| 73 | 0 | lyricTag = new Lyrics3v2(mp3Tag); |
| 74 | |
} |
| 75 | |
|
| 76 | |
FieldFrameBodyLYR lyricField; |
| 77 | 0 | lyricField = (FieldFrameBodyLYR) lyricTag.getField("LYR").getBody(); |
| 78 | 0 | this.lyric = lyricField.getLyric(); |
| 79 | |
} |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public Lyrics3v1(ByteBuffer byteBuffer) throws TagNotFoundException, java.io.IOException |
| 91 | 0 | { |
| 92 | |
try |
| 93 | |
{ |
| 94 | 0 | this.read(byteBuffer); |
| 95 | |
} |
| 96 | 0 | catch (TagException e) |
| 97 | |
{ |
| 98 | 0 | e.printStackTrace(); |
| 99 | 0 | } |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public String getIdentifier() |
| 106 | |
{ |
| 107 | 0 | return "Lyrics3v1.00"; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public void setLyric(String lyric) |
| 114 | |
{ |
| 115 | 0 | this.lyric = ID3Tags.truncate(lyric, 5100); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public String getLyric() |
| 122 | |
{ |
| 123 | 0 | return lyric; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public int getSize() |
| 130 | |
{ |
| 131 | 0 | return "LYRICSBEGIN".length() + lyric.length() + "LYRICSEND".length(); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public boolean isSubsetOf(Object obj) |
| 139 | |
{ |
| 140 | 0 | return (obj instanceof Lyrics3v1) && (((Lyrics3v1) obj).lyric.contains(this.lyric)); |
| 141 | |
|
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public boolean equals(Object obj) |
| 149 | |
{ |
| 150 | 0 | if (!(obj instanceof Lyrics3v1)) |
| 151 | |
{ |
| 152 | 0 | return false; |
| 153 | |
} |
| 154 | |
|
| 155 | 0 | Lyrics3v1 object = (Lyrics3v1) obj; |
| 156 | |
|
| 157 | 0 | return this.lyric.equals(object.lyric) && super.equals(obj); |
| 158 | |
|
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public Iterator iterator() |
| 167 | |
{ |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | 0 | throw new java.lang.UnsupportedOperationException("Method iterator() not yet implemented."); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public boolean seek(ByteBuffer byteBuffer) |
| 182 | |
{ |
| 183 | 0 | return false; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public void read(ByteBuffer byteBuffer) throws TagException |
| 192 | |
{ |
| 193 | 0 | byte[] buffer = new byte[5100 + 9 + 11]; |
| 194 | |
String lyricBuffer; |
| 195 | |
|
| 196 | 0 | if (!seek(byteBuffer)) |
| 197 | |
{ |
| 198 | 0 | throw new TagNotFoundException("ID3v1 tag not found"); |
| 199 | |
} |
| 200 | |
|
| 201 | 0 | byteBuffer.get(buffer); |
| 202 | 0 | lyricBuffer = new String(buffer); |
| 203 | |
|
| 204 | 0 | lyric = lyricBuffer.substring(0, lyricBuffer.indexOf("LYRICSEND")); |
| 205 | 0 | } |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
public boolean seek(RandomAccessFile file) throws IOException |
| 213 | |
{ |
| 214 | 0 | byte[] buffer = new byte[5100 + 9 + 11]; |
| 215 | |
String lyricsEnd; |
| 216 | |
String lyricsStart; |
| 217 | |
long offset; |
| 218 | |
|
| 219 | |
|
| 220 | 0 | file.seek(file.length() - 128 - 9); |
| 221 | 0 | file.read(buffer, 0, 9); |
| 222 | 0 | lyricsEnd = new String(buffer, 0, 9); |
| 223 | |
|
| 224 | 0 | if (lyricsEnd.equals("LYRICSEND")) |
| 225 | |
{ |
| 226 | 0 | offset = file.getFilePointer(); |
| 227 | |
} |
| 228 | |
else |
| 229 | |
{ |
| 230 | |
|
| 231 | |
|
| 232 | 0 | file.seek(file.length() - 9); |
| 233 | 0 | file.read(buffer, 0, 9); |
| 234 | 0 | lyricsEnd = new String(buffer, 0, 9); |
| 235 | |
|
| 236 | 0 | if (lyricsEnd.equals("LYRICSEND")) |
| 237 | |
{ |
| 238 | 0 | offset = file.getFilePointer(); |
| 239 | |
} |
| 240 | |
else |
| 241 | |
{ |
| 242 | 0 | return false; |
| 243 | |
} |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | 0 | offset -= (5100 + 9 + 11); |
| 248 | 0 | file.seek(offset); |
| 249 | 0 | file.read(buffer); |
| 250 | 0 | lyricsStart = new String(buffer); |
| 251 | |
|
| 252 | |
|
| 253 | 0 | int i = lyricsStart.indexOf("LYRICSBEGIN"); |
| 254 | |
|
| 255 | 0 | if (i == -1) |
| 256 | |
{ |
| 257 | 0 | return false; |
| 258 | |
} |
| 259 | |
|
| 260 | 0 | file.seek(offset + i + 11); |
| 261 | |
|
| 262 | 0 | return true; |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public String toString() |
| 269 | |
{ |
| 270 | 0 | String str = getIdentifier() + " " + this.getSize() + "\n"; |
| 271 | |
|
| 272 | 0 | return str + lyric; |
| 273 | |
} |
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public void write(RandomAccessFile file) throws IOException |
| 280 | |
{ |
| 281 | |
String str; |
| 282 | |
int offset; |
| 283 | |
byte[] buffer; |
| 284 | |
ID3v1Tag id3v1tag; |
| 285 | |
|
| 286 | 0 | id3v1tag = null; |
| 287 | |
|
| 288 | 0 | delete(file); |
| 289 | 0 | file.seek(file.length()); |
| 290 | |
|
| 291 | 0 | buffer = new byte[lyric.length() + 11 + 9]; |
| 292 | |
|
| 293 | 0 | str = "LYRICSBEGIN"; |
| 294 | |
|
| 295 | 0 | for (int i = 0; i < str.length(); i++) |
| 296 | |
{ |
| 297 | 0 | buffer[i] = (byte) str.charAt(i); |
| 298 | |
} |
| 299 | |
|
| 300 | 0 | offset = str.length(); |
| 301 | |
|
| 302 | 0 | str = ID3Tags.truncate(lyric, 5100); |
| 303 | |
|
| 304 | 0 | for (int i = 0; i < str.length(); i++) |
| 305 | |
{ |
| 306 | 0 | buffer[i + offset] = (byte) str.charAt(i); |
| 307 | |
} |
| 308 | |
|
| 309 | 0 | offset += str.length(); |
| 310 | |
|
| 311 | 0 | str = "LYRICSEND"; |
| 312 | |
|
| 313 | 0 | for (int i = 0; i < str.length(); i++) |
| 314 | |
{ |
| 315 | 0 | buffer[i + offset] = (byte) str.charAt(i); |
| 316 | |
} |
| 317 | |
|
| 318 | 0 | offset += str.length(); |
| 319 | |
|
| 320 | 0 | file.write(buffer, 0, offset); |
| 321 | |
|
| 322 | 0 | if (id3v1tag != null) |
| 323 | |
{ |
| 324 | 0 | id3v1tag.write(file); |
| 325 | |
} |
| 326 | 0 | } |
| 327 | |
|
| 328 | |
} |