UTF8 implements WritableComparable<UTF8> { public static String fromBytes(byte[] bytes) throws IOException { DataInputBuffer dbuf = new DataInputBuffer(); dbuf.reset(bytes, 0, bytes.length); StringBuilder buf = new StringBuilder(bytes.length); readChars(dbuf, buf, bytes.length); return buf.toString(); } UTF8();  UTF8(String string);  UTF8(UTF8 utf8); byte[] getBytes(); int getLength(); void set(String string); void set(UTF8 other); @Override void readFields(DataInput in); static void skip(DataInput in); @Override void write(DataOutput out); @Override int compareTo(UTF8 o); @Override String toString(); String toStringChecked(); @Override boolean equals(Object o); @Override int hashCode(); static byte[] getBytes(String string); static String fromBytes(byte[] bytes); static String readString(DataInput in); static int writeString(DataOutput out, String s);  }
@Test public void testInvalidUTF8() throws Exception { byte[] invalid = new byte[] { 0x01, 0x02, (byte)0xff, (byte)0xff, 0x01, 0x02, 0x03, 0x04, 0x05 }; try { UTF8.fromBytes(invalid); fail("did not throw an exception"); } catch (UTFDataFormatException utfde) { GenericTestUtils.assertExceptionContains( "Invalid UTF8 at ffff01020304", utfde); } }
@Test public void test5ByteUtf8Sequence() throws Exception { byte[] invalid = new byte[] { 0x01, 0x02, (byte)0xf8, (byte)0x88, (byte)0x80, (byte)0x80, (byte)0x80, 0x04, 0x05 }; try { UTF8.fromBytes(invalid); fail("did not throw an exception"); } catch (UTFDataFormatException utfde) { GenericTestUtils.assertExceptionContains( "Invalid UTF8 at f88880808004", utfde); } }
@Test public void testInvalidUTF8Truncated() throws Exception { byte[] truncated = new byte[] { (byte)0xF0, (byte)0x9F, (byte)0x90 }; try { UTF8.fromBytes(truncated); fail("did not throw an exception"); } catch (UTFDataFormatException utfde) { GenericTestUtils.assertExceptionContains( "Truncated UTF8 at f09f90", utfde); } }