@Override protected void unpackFrameData(byte[] bytes) throws InvalidDataException { text = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, 1, bytes.length - 1)); }
@Override protected byte[] packFrameData() { byte[] bytes = new byte[getLength()]; if (text != null) { bytes[0] = text.getTextEncoding(); byte[] textBytes = text.toBytes(true, false); if (textBytes.length > 0) { BufferTools.copyIntoByteBuffer(textBytes, 0, textBytes.length, bytes, 1); } } return bytes; }
public void setTextEncoding(byte textEncoding, boolean transcode) throws CharacterCodingException { if (this.textEncoding != textEncoding) { CharBuffer charBuffer = bytesToCharBuffer(this.value, characterSetForTextEncoding(this.textEncoding)); byte[] transcodedBytes = charBufferToBytes(charBuffer, characterSetForTextEncoding(textEncoding)); this.textEncoding = textEncoding; this.value = transcodedBytes; } }
@Override protected int getLength() { int length = 4; if (description != null) length += description.toBytes(true, true).length; else length += comment != null ? comment.getTerminator().length : 1; if (comment != null) length += comment.toBytes(true, false).length; return length; }
@Override public String toString() { try { return bytesToString(value, characterSetForTextEncoding(textEncoding)); } catch (CharacterCodingException e) { return null; } }
@Override protected void unpackFrameData(byte[] bytes) throws InvalidDataException { int marker = BufferTools.indexOfTerminatorForEncoding(bytes, 1, bytes[0]); if (marker >= 0) { description = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, 1, marker - 1)); marker += description.getTerminator().length; } else { description = new EncodedText(bytes[0], ""); marker = 1; } try { url = BufferTools.byteBufferToString(bytes, marker, bytes.length - marker); } catch (UnsupportedEncodingException e) { url = ""; } }
private void updateCustomTag() { byte[] existingCustomTag = mp3file.getCustomTag(); byte[] newCustomTag = null; if (keepCustomTag && existingCustomTag != null && existingCustomTag.length > 0) { if (customTag != null && customTag.length() > 0) { EncodedText customTagEncodedText = new EncodedText(customTag); byte bytes[] = customTagEncodedText.toBytes(true); int newLength = existingCustomTag.length + bytes.length; newCustomTag = new byte[newLength]; BufferTools.copyIntoByteBuffer(existingCustomTag, 0, existingCustomTag.length, newCustomTag, 0); BufferTools.copyIntoByteBuffer(bytes, 0, bytes.length, newCustomTag, existingCustomTag.length); } else { newCustomTag = mp3file.getCustomTag(); } } else if (customTag != null && customTag.length() > 0) { EncodedText customTagEncodedText = new EncodedText(customTag); newCustomTag = customTagEncodedText.toBytes(true); } mp3file.setCustomTag(newCustomTag); }
@Override protected int getLength() { int length = 1; if (text != null) length += text.toBytes(true, false).length; return length; }
public EncodedText(String string) throws IllegalArgumentException { for (byte textEncoding : textEncodingFallback) { this.textEncoding = textEncoding; value = stringToBytes(string, characterSetForTextEncoding(textEncoding)); if (value != null && this.toString() != null) { this.stripBomAndTerminator(); return; } } throw new IllegalArgumentException("Invalid string, could not find appropriate encoding"); }
@Override protected byte[] packFrameData() { byte[] bytes = new byte[getLength()]; if (comment != null) bytes[0] = comment.getTextEncoding(); else bytes[0] = 0; String langPadded; byte[] descriptionBytes = description.toBytes(true, true); BufferTools.copyIntoByteBuffer(descriptionBytes, 0, descriptionBytes.length, bytes, marker); marker += descriptionBytes.length; } else { byte[] terminatorBytes = comment != null ? comment.getTerminator() : new byte[]{0}; BufferTools.copyIntoByteBuffer(terminatorBytes, 0, terminatorBytes.length, bytes, marker); marker += terminatorBytes.length; byte[] commentBytes = comment.toBytes(true, false); BufferTools.copyIntoByteBuffer(commentBytes, 0, commentBytes.length, bytes, marker);
public EncodedText(byte textEncoding, String string) { this.textEncoding = textEncoding; value = stringToBytes(string, characterSetForTextEncoding(textEncoding)); this.stripBomAndTerminator(); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; ID3v2TextFrameData other = (ID3v2TextFrameData) obj; if (text == null) { if (other.text != null) return false; } else if (!text.equals(other.text)) return false; return true; } }
public byte[] toBytes(boolean includeBom, boolean includeTerminator) { characterSetForTextEncoding(textEncoding); // ensured textEncoding is valid int newLength = value.length + (includeBom ? boms[textEncoding].length : 0) + (includeTerminator ? getTerminator().length : 0); if (newLength == value.length) { return value; } else { byte bytes[] = new byte[newLength]; int i = 0; if (includeBom) { byte[] bom = boms[textEncoding]; if (bom.length > 0) { System.arraycopy(boms[textEncoding], 0, bytes, i, boms[textEncoding].length); i += boms[textEncoding].length; } } if (value.length > 0) { System.arraycopy(value, 0, bytes, i, value.length); i += value.length; } if (includeTerminator) { byte[] terminator = getTerminator(); if (terminator.length > 0) { System.arraycopy(terminator, 0, bytes, i, terminator.length); } } return bytes; } }
private static String bytesToString(byte[] bytes, String characterSet) throws CharacterCodingException { CharBuffer cbuf = bytesToCharBuffer(bytes, characterSet); String s = cbuf.toString(); int length = s.indexOf(0); if (length == -1) return s; return s.substring(0, length); }
public ID3v2CommentFrameData(boolean unsynchronisation, String language, EncodedText description, EncodedText comment) { super(unsynchronisation); if (description != null && comment != null && description.getTextEncoding() != comment.getTextEncoding()) { throw new IllegalArgumentException("description and comment must have same text encoding"); } this.language = language; this.description = description; this.comment = comment; }
private static byte[] stringToBytes(String s, String characterSet) { try { return charBufferToBytes(CharBuffer.wrap(s), characterSet); } catch (CharacterCodingException e) { return null; } }
public String getCharacterSet() { return characterSetForTextEncoding(textEncoding); }
@Override protected void unpackFrameData(byte[] bytes) throws InvalidDataException { try { language = BufferTools.byteBufferToString(bytes, 1, 3); } catch (UnsupportedEncodingException e) { language = ""; } int marker = BufferTools.indexOfTerminatorForEncoding(bytes, 4, bytes[0]); if (marker >= 4) { description = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, 4, marker - 4)); marker += description.getTerminator().length; } else { description = new EncodedText(bytes[0], ""); marker = 4; } comment = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, marker, bytes.length - marker)); }
public byte[] toBytes(boolean includeBom) { return toBytes(includeBom, false); }
public EncodedText(String string) throws IllegalArgumentException { for (byte textEncoding : textEncodingFallback) { this.textEncoding = textEncoding; value = stringToBytes(string, characterSetForTextEncoding(textEncoding)); if (value != null && this.toString() != null) { this.stripBomAndTerminator(); return; } } throw new IllegalArgumentException("Invalid string, could not find appropriate encoding"); }