ByteBufUtil { public static int writeAscii(ByteBuf buf, CharSequence seq) { if (buf == null) { throw new NullPointerException("buf"); } if (seq == null) { throw new NullPointerException("seq"); } final int len = seq.length(); buf.ensureWritable(len); if (buf instanceof AbstractByteBuf) { AbstractByteBuf buffer = (AbstractByteBuf) buf; int writerIndex = buffer.writerIndex; for (int i = 0; i < len; i++) { buffer._setByte(writerIndex++, (byte) seq.charAt(i)); } buffer.writerIndex = writerIndex; } else { buf.writeBytes(seq.toString().getBytes(CharsetUtil.US_ASCII)); } return len; } private  ByteBufUtil(); static String hexDump(ByteBuf buffer); static String hexDump(ByteBuf buffer, int fromIndex, int length); static int hashCode(ByteBuf buffer); 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();  }
@Test public void testWriteUsAscii() { String usAscii = "NettyRocks"; ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); ByteBufUtil.writeAscii(buf2, usAscii); Assert.assertEquals(buf, buf2); }