public abstract class ByteBufUtils {
public static final Splitter DOT_SPLITTER = Splitter.on('.');
public static final Splitter COLON_SPLITTER = Splitter.on(':');
- private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
private static final Splitter HEXSTRING_SPLITTER = Splitter.onPattern("\\s+").omitEmptyStrings();
private static final Splitter HEXSTRING_NOSPACE_SPLITTER = Splitter.onPattern("(?<=\\G.{2})").omitEmptyStrings();
return sb.toString().trim();
}
- private static void appendHexUnsignedShort(final StringBuilder sb, final int val) {
- sb.append(ByteBufUtils.HEX_CHARS[val >>> 12 & 15]);
- sb.append(ByteBufUtils.HEX_CHARS[val >>> 8 & 15]);
- sb.append(ByteBufUtils.HEX_CHARS[val >>> 4 & 15]);
- sb.append(ByteBufUtils.HEX_CHARS[val & 15]);
- }
-
/**
* Reads and parses null-terminated string from ByteBuf.
*
return sb.toString();
}
-
- /**
- * Read an IPv6 address from a buffer and format it into a string of eight groups of four
- * hexadecimal digits separated by colons.
- *
- * @param buf Input buffer
- * @return IPv6 address in string format
- */
- public static String readIpv6Address(final ByteBuf buf) {
- final StringBuilder sb = new StringBuilder(EncodeConstants.GROUPS_IN_IPV6_ADDRESS * 5 - 1);
-
- appendHexUnsignedShort(sb, buf.readUnsignedShort());
- for (int i = 1; i < EncodeConstants.GROUPS_IN_IPV6_ADDRESS; i++) {
- sb.append(':');
- appendHexUnsignedShort(sb, buf.readUnsignedShort());
- }
-
- return sb.toString();
- }
-
public static Ipv4Address readIetfIpv4Address(final ByteBuf buf) {
final byte[] tmp = new byte[4];
buf.readBytes(tmp);
ipv4Address = ByteBufUtils.readIpv4Address(buffer2);
}
- /**
- * Test ipv6 address conversion.
- */
- @Test(expected = IndexOutOfBoundsException.class)
- public void testReadIpv6Address() {
- ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
- buffer.writeShort(10);
- buffer.writeShort(65535);
- buffer.writeShort(4096);
- buffer.writeShort(0);
- buffer.writeShort(1024);
- buffer.writeShort(42);
- buffer.writeShort(2568);
- buffer.writeShort(45689);
- String ipv4Address = ByteBufUtils.readIpv6Address(buffer);
- Assert.assertEquals("Wrong conversion", "000A:FFFF:1000:0000:0400:002A:0A08:B279", ipv4Address);
- Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
-
- ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
- buffer.writeShort(10);
- ipv4Address = ByteBufUtils.readIpv6Address(buffer2);
- }
-
@Test
public void testSerializeList() {