ByteBufUtil { public static int writeUtf8(ByteBuf buf, CharSequence seq) { if (buf == null) { throw new NullPointerException("buf"); } if (seq == null) { throw new NullPointerException("seq"); } final int len = seq.length(); final int maxSize = len * 3; buf.ensureWritable(maxSize); for (;;) { if (buf instanceof AbstractByteBuf) { return writeUtf8((AbstractByteBuf) buf, seq, len); } else if (buf instanceof WrappedByteBuf) { buf = buf.unwrap(); } else { byte[] bytes = seq.toString().getBytes(CharsetUtil.UTF_8); buf.writeBytes(bytes); return bytes.length; } } } private  ByteBufUtil(); static String hexDump(ByteBuf buffer); static String hexDump(ByteBuf buffer, int fromIndex, int length); static String hexDump(byte[] array); static String hexDump(byte[] array, int fromIndex, int length); static int hashCode(ByteBuf buffer); static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length); static boolean equals(ByteBuf bufferA, ByteBuf bufferB); static int compare(ByteBuf bufferA, ByteBuf bufferB); static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value); static short swapShort(short value); static int swapMedium(int value); static int swapInt(int value); static long swapLong(long value); static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length); static int writeUtf8(ByteBuf buf, CharSequence seq); static int writeAscii(ByteBuf buf, CharSequence seq); static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset); static ByteBuf threadLocalDirectBuffer(); static byte[] getBytes(ByteBuf buf); static byte[] getBytes(ByteBuf buf, int start, int length); static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy); static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length); static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length); static String prettyHexDump(ByteBuf buffer); static String prettyHexDump(ByteBuf buffer, int offset, int length); static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf); static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offset, int length);  }
@Test public void testWriteUtf8() { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8)); ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); ByteBufUtil.writeUtf8(buf2, usAscii); Assert.assertEquals(buf, buf2); }