return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
}
+ /**
+ * Creates an Ipv4Prefix object from given ByteBuf. Prefix length is assumed to
+ * be in the left most byte of the buffer.
+ *
+ * @param bytes IPv4 address
+ * @return Ipv4Prefix object
+ */
+ public static Ipv4Prefix prefixForByteBuf(final ByteBuf bytes) {
+ final int prefixLength = bytes.readByte();
+ final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
+ Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: " + (bytes.readableBytes()));
+ return Ipv4Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+ }
+
/**
* Creates a list of Ipv4 Prefixes from given byte array.
*
return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
}
+ /**
+ * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
+ * be in the left most byte of the buffer.
+ *
+ * @param bytes IPv6 address
+ * @return Ipv6Prefix object
+ */
+ public static Ipv6Prefix prefixForByteBuf(final ByteBuf bytes) {
+ final int prefixLength = bytes.readByte();
+ final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
+ Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: " + (bytes.readableBytes()));
+ return Ipv6Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+ }
+
+
/**
* Creates a list of Ipv6 Prefixes from given byte array.
*
assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
}
+ @Test
+ public void testPrefixForByteBuf() {
+ final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 0x0e, 123, 122, 0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
+ assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
+ assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
+ }
+
@Test
public void testAddressForByteBuf() {
final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );