ByteBufUtil { public static byte[] getBytes(ByteBuf buf) { return getBytes(buf, buf.readerIndex(), buf.readableBytes()); } 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 byte decodeHexByte(CharSequence s, int pos); static byte[] decodeHexDump(CharSequence hexDump); static byte[] decodeHexDump(CharSequence hexDump, int fromIndex, int length); static boolean ensureWritableSuccess(int ensureWritableResult); static int hashCode(ByteBuf buffer); static int indexOf(ByteBuf needle, ByteBuf haystack); 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); @SuppressWarnings("deprecation") static ByteBuf writeShortBE(ByteBuf buf, int shortValue); @SuppressWarnings("deprecation") static ByteBuf setShortBE(ByteBuf buf, int index, int shortValue); @SuppressWarnings("deprecation") static ByteBuf writeMediumBE(ByteBuf buf, int mediumValue); static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length); static ByteBuf writeUtf8(ByteBufAllocator alloc, CharSequence seq); static int writeUtf8(ByteBuf buf, CharSequence seq); static int utf8MaxBytes(CharSequence seq); static ByteBuf writeAscii(ByteBufAllocator alloc, CharSequence seq); static int writeAscii(ByteBuf buf, CharSequence seq); static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset); static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset, int extraCapacity); 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, ByteBuf dst); 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); static boolean isText(ByteBuf buf, Charset charset); static boolean isText(ByteBuf buf, int index, int length, Charset charset);  }
@Test public void testToStringDoesNotThrowIndexOutOfBounds() { CompositeByteBuf buffer = Unpooled.compositeBuffer(); try { byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8); buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes)); buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes)); assertEquals("1234", buffer.toString(bytes.length, bytes.length, CharsetUtil.UTF_8)); } finally { buffer.release(); } }
@Test public void testIsTextWithUtf8() { byte[][] validUtf8Bytes = new byte[][]{ "netty".getBytes(CharsetUtil.UTF_8), new byte[]{(byte) 0x24}, new byte[]{(byte) 0xC2, (byte) 0xA2}, new byte[]{(byte) 0xE2, (byte) 0x82, (byte) 0xAC}, new byte[]{(byte) 0xF0, (byte) 0x90, (byte) 0x8D, (byte) 0x88}, new byte[]{(byte) 0x24, (byte) 0xC2, (byte) 0xA2, (byte) 0xE2, (byte) 0x82, (byte) 0xAC, (byte) 0xF0, (byte) 0x90, (byte) 0x8D, (byte) 0x88} }; for (byte[] bytes : validUtf8Bytes) { assertIsText(bytes, true, CharsetUtil.UTF_8); } byte[][] invalidUtf8Bytes = new byte[][]{ new byte[]{(byte) 0x80}, new byte[]{(byte) 0xF0, (byte) 0x82, (byte) 0x82, (byte) 0xAC}, new byte[]{(byte) 0xC2}, new byte[]{(byte) 0xE2, (byte) 0x82}, new byte[]{(byte) 0xF0, (byte) 0x90, (byte) 0x8D}, new byte[]{(byte) 0xC2, (byte) 0xC0}, new byte[]{(byte) 0xE2, (byte) 0x82, (byte) 0xC0}, new byte[]{(byte) 0xF0, (byte) 0x90, (byte) 0x8D, (byte) 0xC0}, new byte[]{(byte) 0xC1, (byte) 0x80}, new byte[]{(byte) 0xE0, (byte) 0x80, (byte) 0x80}, new byte[]{(byte) 0xED, (byte) 0xAF, (byte) 0x80} }; for (byte[] bytes : invalidUtf8Bytes) { assertIsText(bytes, false, CharsetUtil.UTF_8); } }