Text extends BinaryComparable implements WritableComparable<BinaryComparable> { public static int utf8Length(String string) { CharacterIterator iter = new StringCharacterIterator(string); char ch = iter.first(); int size = 0; while (ch != CharacterIterator.DONE) { if ((ch >= 0xD800) && (ch < 0xDC00)) { char trail = iter.next(); if ((trail > 0xDBFF) && (trail < 0xE000)) { size += 4; } else { size += 3; iter.previous(); } } else if (ch < 0x80) { size++; } else if (ch < 0x800) { size += 2; } else { size += 3; } ch = iter.next(); } return size; } Text();  Text(String string);  Text(Text utf8);  Text(byte[] utf8); byte[] copyBytes(); @Override byte[] getBytes(); @Override int getLength(); int charAt(int position); int find(String what); int find(String what, int start); void set(String string); void set(byte[] utf8); void set(Text other); void set(byte[] utf8, int start, int len); void append(byte[] utf8, int start, int len); void clear(); @Override String toString(); @Override void readFields(DataInput in); void readFields(DataInput in, int maxLength); static void skip(DataInput in); void readWithKnownLength(DataInput in, int len); @Override void write(DataOutput out); void write(DataOutput out, int maxLength); @Override boolean equals(Object o); @Override int hashCode(); static String decode(byte[] utf8); static String decode(byte[] utf8, int start, int length); static String decode(byte[] utf8, int start, int length, boolean replace); static ByteBuffer encode(String string); static ByteBuffer encode(String string, boolean replace); static String readString(DataInput in); static String readString(DataInput in, int maxLength); static int writeString(DataOutput out, String s); static int writeString(DataOutput out, String s, int maxLength); static void validateUTF8(byte[] utf8); static void validateUTF8(byte[] utf8, int start, int len); static int bytesToCodePoint(ByteBuffer bytes); static int utf8Length(String string); static final public int DEFAULT_MAX_LEN; }
@Test public void testUtf8Length() { assertEquals("testUtf8Length1 error !!!", 1, Text.utf8Length(new String(new char[]{(char) 1}))); assertEquals("testUtf8Length127 error !!!", 1, Text.utf8Length(new String(new char[]{(char) 127}))); assertEquals("testUtf8Length128 error !!!", 2, Text.utf8Length(new String(new char[]{(char) 128}))); assertEquals("testUtf8Length193 error !!!", 2, Text.utf8Length(new String(new char[]{(char) 193}))); assertEquals("testUtf8Length225 error !!!", 2, Text.utf8Length(new String(new char[]{(char) 225}))); assertEquals("testUtf8Length254 error !!!", 2, Text.utf8Length(new String(new char[]{(char)254}))); }