/** * If the frame body's values have been modified, then resize the raw binary buffer and store the * new values there. When finished, the dirty flag is reset to indicate that the buffer is up to * date, and the frame is now ready to be saved to the .mp3 file. */ @Override public void setBuffer() { if (isDirty()) { int numSynchronizedLyricBytes = 0; for (SynchronizedLyric synchronizedLyric : synchronizedLyrics) numSynchronizedLyricBytes += stringToBytes(encoding, synchronizedLyric.getText()).length + 4; byte[] languageBytes = language.getCodeBytes(); byte[] descriptionBytes = stringToBytes(encoding, description); byte[] synchronizedLyricBytes = new byte[numSynchronizedLyricBytes]; byte[] textBytes = null; byte[] timeStampBytes = null; int index = 0; buffer = new byte [1 + languageBytes.length + 1 + 1 + descriptionBytes.length + synchronizedLyricBytes.length]; buffer[index] = (byte) encoding.ordinal(); index = 1; System.arraycopy(languageBytes, 0, buffer, index, languageBytes.length); index += languageBytes.length; buffer[index] = (byte) timeStampFormat.getValue(); index++; buffer[index] = (byte) contentType.ordinal(); index++; System.arraycopy(descriptionBytes, 0, buffer, index, descriptionBytes.length); index += descriptionBytes.length; for (SynchronizedLyric synchronizedLyric : synchronizedLyrics) { numSynchronizedLyricBytes += stringToBytes(encoding, synchronizedLyric.getText()).length + 4; textBytes = stringToBytes(encoding, synchronizedLyric.getText()); System.arraycopy(textBytes, 0, buffer, index, textBytes.length); index += textBytes.length; timeStampBytes = intToBytes(synchronizedLyric.getTimeStamp()); System.arraycopy(timeStampBytes, 0, buffer, index, timeStampBytes.length); index += timeStampBytes.length; } dirty = false; } }
/** * parses the raw bytes of the frame body and stores the parsed values in the frame's fields. * * @throws IllegalArgumentException if an invalid value is detected while parsing the frame body's * raw bytes. */ @Override public void parse() throws IllegalArgumentException { try { setEncoding(Encoding.valueOf(buffer[0])); } catch ( IllegalArgumentException ex) { // ignore the bad value and set it to ISO-8859-1 so we can continue parsing the // tag setEncoding(Encoding.ISO_8859_1); } try { setLanguage( Language.getLanguage(new String(buffer, 1, 3, Encoding.ISO_8859_1.getCharacterSet()))); } catch ( IllegalArgumentException ex) { // ignore the bad value and set it to english so we can continue parsing the tag setLanguage(Language.ENG); } try { setTimeStampFormat(TimeStampFormat.valueOf(buffer[4])); } catch ( IllegalArgumentException ex) { // ignore the bad value and set it to milliseconds so we can continue parsing the // tag setTimeStampFormat(TimeStampFormat.MS); } try { setContentType(ContentType.getContentType(buffer[5])); } catch ( IllegalArgumentException ex) { // ignore the bad value and set it to other so we can continue parsing the tag setContentType(ContentType.OTHER); } nullTerminatorIndex = getNextNullTerminator(6, encoding); description = new String(buffer, 6, nullTerminatorIndex - 6, encoding.getCharacterSet()).trim(); nullTerminatorIndex += encoding.getNumBytesInNullTerminator(); Vector<SynchronizedLyric> lyrics = new Vector<SynchronizedLyric>(); String text; int timeStamp; while (nullTerminatorIndex < buffer.length) { nextNullTerminatorIndex = getNextNullTerminator(nullTerminatorIndex, encoding); text = new String( buffer, nullTerminatorIndex, nextNullTerminatorIndex - nullTerminatorIndex, encoding.getCharacterSet()) .trim(); nullTerminatorIndex = nextNullTerminatorIndex + encoding.getNumBytesInNullTerminator(); timeStamp = ((buffer[nullTerminatorIndex] & 0xFF) << 24) + ((buffer[nullTerminatorIndex + 1] & 0xFF) << 16) + ((buffer[nullTerminatorIndex + 2] & 0xFF) << 8) + (buffer[nullTerminatorIndex + 3] & 0xFF); nullTerminatorIndex += 4; lyrics.add(new SynchronizedLyric(text, timeStamp)); } setSynchronizedLyrics(lyrics); dirty = false; // we just read in the frame info, so the frame body's internal byte buffer is up to // date }