Utf8StringBuilder extends Utf8Appendable { @Override public String toString() { checkState(); return buffer.toString(); } Utf8StringBuilder();  Utf8StringBuilder(int capacity); @Override int length(); @Override void reset(); StringBuilder getStringBuilder(); @Override String toString();  }
@Test void testUtfStringBuilder() { String source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdfirefly"; byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuilder buffer = new Utf8StringBuilder(); for (byte aByte : bytes) buffer.append(aByte); assertEquals(source, buffer.toString()); assertTrue(buffer.toString().endsWith("firefly")); }
@Test void testShort() { assertThrows(IllegalArgumentException.class, () -> { String source = "abc\u10fb"; byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuilder buffer = new Utf8StringBuilder(); for (int i = 0; i < bytes.length - 1; i++) { buffer.append(bytes[i]); } buffer.toString(); }); }
@Test void testLong() { String source = "abcXX"; byte[] bytes = source.getBytes(StandardCharsets.UTF_8); bytes[3] = (byte) 0xc0; bytes[4] = (byte) 0x00; Utf8StringBuilder buffer = new Utf8StringBuilder(); try { for (byte aByte : bytes) { buffer.append(aByte); } fail("Should have resulted in an Utf8Appendable.NotUtf8Exception"); } catch (Utf8Appendable.NotUtf8Exception e) { } assertEquals("abc\ufffd", buffer.toString()); }
@Test void testUTF32codes() { String source = "\uD842\uDF9F"; byte[] bytes = source.getBytes(StandardCharsets.UTF_8); String jvmcheck = new String(bytes, 0, bytes.length, StandardCharsets.UTF_8); assertEquals(source, jvmcheck); Utf8StringBuilder buffer = new Utf8StringBuilder(); buffer.append(bytes, 0, bytes.length); String result = buffer.toString(); assertEquals(source, result); }