Permalink
Browse files

Unicode support for text information frames

  • Loading branch information...
1 parent d7bbab7 commit 27abd9449fea2263862fb9b28a0bf0020c1817e1 @mpatric committed May 9, 2012
View
7 src/main/java/com/mpatric/mp3agic/ID3v2TextFrameData.java
@@ -26,15 +26,16 @@ protected void unpackFrameData(byte[] bytes) throws InvalidDataException {
byte[] bytes = new byte[getLength()];
if (text != null) bytes[0] = text.getTextEncoding();
else bytes[0] = 0;
- if (text != null && text.toBytes().length > 0) {
- BufferTools.copyIntoByteBuffer(text.toBytes(), 0, text.toBytes().length, bytes, 1);
+ byte[] textBytes = text.toBytes(true, false);
+ if (textBytes.length > 0) {
+ BufferTools.copyIntoByteBuffer(textBytes, 0, textBytes.length, bytes, 1);
}
return bytes;
}
protected int getLength() {
int length = 1;
- if (text != null) length += text.toBytes().length;
+ if (text != null) length += text.toBytes(true, false).length;
return length;
}
View
24 src/test/java/com/mpatric/mp3agic/ID3v2TextFrameDataTest.java
@@ -1,5 +1,7 @@
package com.mpatric.mp3agic;
+import java.util.Arrays;
+
import com.mpatric.mp3agic.EncodedText;
import com.mpatric.mp3agic.ID3v2TextFrameData;
@@ -8,23 +10,29 @@
public class ID3v2TextFrameDataTest extends TestCase {
private static final String TEST_TEXT = "ABCDEFGHIJKLMNOPQ";
+ private static final String TEST_TEXT_UNICODE = "\u03B3\u03B5\u03B9\u03AC";
public void testShouldConsiderTwoEquivalentObjectsEqual() throws Exception {
- ID3v2TextFrameData frameData1 = createTestTextFrameData();
- ID3v2TextFrameData frameData2 = createTestTextFrameData();
+ ID3v2TextFrameData frameData1 = new ID3v2TextFrameData(false, new EncodedText(EncodedText.TEXT_ENCODING_ISO_8859_1, TEST_TEXT));
+ ID3v2TextFrameData frameData2 = new ID3v2TextFrameData(false, new EncodedText(EncodedText.TEXT_ENCODING_ISO_8859_1, TEST_TEXT));
assertEquals(frameData1, frameData2);
}
public void testShouldConvertFrameDataToBytesAndBackToEquivalentObject() throws Exception {
- ID3v2TextFrameData frameData = createTestTextFrameData();
+ ID3v2TextFrameData frameData = new ID3v2TextFrameData(false, new EncodedText(EncodedText.TEXT_ENCODING_ISO_8859_1, TEST_TEXT));
byte[] bytes = frameData.toBytes();
+ byte[] expectedBytes = {0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q'};
+ assertTrue(Arrays.equals(expectedBytes, bytes));
ID3v2TextFrameData frameDataCopy = new ID3v2TextFrameData(false, bytes);
- assertEquals(1 + TEST_TEXT.length(), bytes.length);
assertEquals(frameData, frameDataCopy);
}
-
- private ID3v2TextFrameData createTestTextFrameData() {
- ID3v2TextFrameData frameData = new ID3v2TextFrameData(false, new EncodedText(EncodedText.TEXT_ENCODING_ISO_8859_1, TEST_TEXT));
- return frameData;
+
+ public void testShouldConvertFrameDataWithUnicodeToBytesAndBackToEquivalentObject() throws Exception {
+ ID3v2TextFrameData frameData = new ID3v2TextFrameData(false, new EncodedText(EncodedText.TEXT_ENCODING_UTF_16, TEST_TEXT_UNICODE));
+ byte[] bytes = frameData.toBytes();
+ byte[] expectedBytes = {1, (byte)0xff, (byte)0xfe, (byte)0xb3, 0x03, (byte)0xb5, 0x03, (byte)0xb9, 0x03, (byte)0xac, 0x03};
+ assertTrue(Arrays.equals(expectedBytes, bytes));
+ ID3v2TextFrameData frameDataCopy = new ID3v2TextFrameData(false, bytes);
+ assertEquals(frameData, frameDataCopy);
}
}

0 comments on commit 27abd94

Please sign in to comment.