Moved fromUtf8 from TestUtils to Utility.

And cleaned up SimpleIcsWriter.toString().

Change-Id: I383d91256c48be5263c695c8e8dd151d0e95d0a6
This commit is contained in:
Makoto Onuki 2010-03-24 11:05:14 -07:00
parent e5be03d497
commit fe61f358ab
6 changed files with 35 additions and 31 deletions

View File

@ -475,6 +475,15 @@ public class Utility {
return bytes;
}
/** Build a String from UTF-8 bytes */
public static String fromUtf8(byte[] b) {
if (b == null) {
return null;
}
final CharBuffer cb = Utility.UTF_8.decode(ByteBuffer.wrap(b));
return new String(cb.array(), 0, cb.length());
}
/**
* @return true if the input is the first (or only) byte in a UTF-8 character
*/

View File

@ -104,11 +104,7 @@ public class SimpleIcsWriter {
*/
@Override
public String toString() {
try {
return new String(getBytes(), "UTF-8");
} catch (UnsupportedEncodingException wonthappen) {
}
return null;
return Utility.fromUtf8(getBytes());
}
/**

View File

@ -16,8 +16,7 @@
package com.android.email;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import android.test.MoreAsserts;
import junit.framework.TestCase;
@ -37,21 +36,9 @@ public class TestUtils extends TestCase /* It tests itself */ {
return ret;
}
/** Converts a String from UTF-8 */
public static String fromUtf8(byte[] b) {
if (b == null) {
return null;
}
final CharBuffer cb = Utility.UTF_8.decode(ByteBuffer.wrap(b));
return new String(cb.array(), 0, cb.length());
}
public void testUtf8() {
assertNull(fromUtf8(null));
assertEquals("", fromUtf8(new byte[] {}));
assertEquals("a", fromUtf8(b('a')));
assertEquals("ABC", fromUtf8(b('A', 'B', 'C')));
assertEquals("\u65E5\u672C\u8A9E",
fromUtf8(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E)));
public void testB() {
assertNull(b(null));
MoreAsserts.assertEquals(new byte[] {}, b());
MoreAsserts.assertEquals(new byte[] {1, 2, (byte) 0xff}, b(1, 2, 0xff));
}
}

View File

@ -114,17 +114,28 @@ public class UtilityUnitTests extends AndroidTestCase {
assertEquals(8, set.size());
}
private static byte[] b(int... array) {
return TestUtils.b(array);
}
/** Test for {@link Utility#toUtf8} and {@link Utility#fromUtf8} */
public void testUtf8() {
public void testToUtf8() {
assertNull(Utility.toUtf8(null));
MoreAsserts.assertEquals(new byte[] {}, Utility.toUtf8(""));
MoreAsserts.assertEquals(TestUtils.b('a'), Utility.toUtf8("a"));
MoreAsserts.assertEquals(TestUtils.b('A', 'B', 'C'), Utility.toUtf8("ABC"));
MoreAsserts.assertEquals(TestUtils.b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E),
MoreAsserts.assertEquals(b('a'), Utility.toUtf8("a"));
MoreAsserts.assertEquals(b('A', 'B', 'C'), Utility.toUtf8("ABC"));
MoreAsserts.assertEquals(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E),
Utility.toUtf8("\u65E5\u672C\u8A9E"));
}
public void testFromUtf8() {
assertNull(Utility.fromUtf8(null));
assertEquals("", Utility.fromUtf8(new byte[] {}));
assertEquals("a", Utility.fromUtf8(b('a')));
assertEquals("ABC", Utility.fromUtf8(b('A', 'B', 'C')));
assertEquals("\u65E5\u672C\u8A9E",
Utility.fromUtf8(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E)));
}
public void testIsFirstUtf8Byte() {
// 1 byte in UTF-8.
checkIsFirstUtf8Byte("0"); // First 2 bits: 00

View File

@ -537,7 +537,7 @@ public class CalendarUtilitiesTests extends AndroidTestCase {
}
private BlockHash parseIcsContent(byte[] bytes) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(TestUtils.fromUtf8(bytes)));
BufferedReader reader = new BufferedReader(new StringReader(Utility.fromUtf8(bytes)));
String line = reader.readLine();
if (!line.equals("BEGIN:VCALENDAR")) {
throw new IllegalArgumentException();

View File

@ -16,6 +16,7 @@
package com.android.exchange.utility;
import com.android.email.TestUtils;
import com.android.email.Utility;
import junit.framework.TestCase;
@ -42,7 +43,7 @@ public class SimpleIcsWriterTests extends TestCase {
ics.writeTag("TAG3", "xyz");
ics.writeTag("SUMMARY", "TEST-TEST,;\r\n\\TEST");
ics.writeTag("SUMMARY2", "TEST-TEST,;\r\n\\TEST");
final String actual = TestUtils.fromUtf8(ics.getBytes());
final String actual = Utility.fromUtf8(ics.getBytes());
assertEquals(
"TAG3:xyz" + CRLF +
@ -96,7 +97,7 @@ public class SimpleIcsWriterTests extends TestCase {
// If we're splitting up a UTF-8 character, fromUtf8() won't restore it correctly.
// If it becomes the same as input, we're doing the right thing.
final String actual = TestUtils.fromUtf8(bytes);
final String actual = Utility.fromUtf8(bytes);
final String unfolded = actual.replace("\r\n\t", "");
assertEquals("input=" + input, input + "\r\n", unfolded);
}