Text extends BinaryComparable implements WritableComparable<BinaryComparable> { public static int bytesToCodePoint(ByteBuffer bytes) { bytes.mark(); byte b = bytes.get(); bytes.reset(); int extraBytesToRead = bytesFromUTF8[(b & 0xFF)]; if (extraBytesToRead < 0) return -1; int ch = 0; switch (extraBytesToRead) { case 5: ch += (bytes.get() & 0xFF); ch <<= 6; case 4: ch += (bytes.get() & 0xFF); ch <<= 6; case 3: ch += (bytes.get() & 0xFF); ch <<= 6; case 2: ch += (bytes.get() & 0xFF); ch <<= 6; case 1: ch += (bytes.get() & 0xFF); ch <<= 6; case 0: ch += (bytes.get() & 0xFF); } ch -= offsetsFromUTF8[extraBytesToRead]; return ch; } 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 testBytesToCodePoint() { try { ByteBuffer bytes = ByteBuffer.wrap(new byte[] {-2, 45, 23, 12, 76, 89}); Text.bytesToCodePoint(bytes); assertTrue("testBytesToCodePoint error !!!", bytes.position() == 6 ); } catch (BufferUnderflowException ex) { fail("testBytesToCodePoint unexp exception"); } catch (Exception e) { fail("testBytesToCodePoint unexp exception"); } }
@Test public void testbytesToCodePointWithInvalidUTF() { try { Text.bytesToCodePoint(ByteBuffer.wrap(new byte[] {-2})); fail("testbytesToCodePointWithInvalidUTF error unexp exception !!!"); } catch (BufferUnderflowException ex) { } catch(Exception e) { fail("testbytesToCodePointWithInvalidUTF error unexp exception !!!"); } }