Example usage for com.google.common.net InetAddresses fromLittleEndianByteArray

List of usage examples for com.google.common.net InetAddresses fromLittleEndianByteArray

Introduction

In this page you can find the example usage for com.google.common.net InetAddresses fromLittleEndianByteArray.

Prototype

public static InetAddress fromLittleEndianByteArray(byte[] addr) throws UnknownHostException 

Source Link

Document

Returns an address from a little-endian ordered byte array (the opposite of what InetAddress#getByAddress expects).

Usage

From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

private ExtendedCommunityPathAttribute decodeExtendedCommunityPathAttribute(final ByteBuf buffer) {
    final ExtendedCommunityPathAttribute attr = new ExtendedCommunityPathAttribute();

    if ((buffer.readableBytes() < 8) || ((buffer.readableBytes() % 8) != 0)) {
        throw new OptionalAttributeErrorException();
    }//from   www  . ja v  a  2 s. c  o m

    while (buffer.isReadable()) {
        AbstractExtendedCommunityInterface extcomm;
        // we need to check whether this is a transitive or non-transitive value
        // and then determine whether we need to red the lower type byte
        byte higherType = buffer.readByte();
        byte higherTypeCompare = (byte) (higherType & ~(1 << 6));
        if (((higherType >> 6) & 1) == 0) {
            // bit 7 is not set in the byte, therefore this is a transitive type
            // clear bit 8, not interested in this value
            TransitiveExtendedCommunityType transCommType = TransitiveExtendedCommunityType
                    .fromCode((byte) (higherType & (~(3 << 6))));

            switch (transCommType) {
            case TWO_OCTET_AS_SPECIFIC:
                TransitiveTwoOctetASSpecificExtCommSubTypes twoOctASNLowerType = TransitiveTwoOctetASSpecificExtCommSubTypes
                        .fromCode(buffer.readByte());
                switch (twoOctASNLowerType) {
                case ROUTE_TARGET:
                    extcomm = new TransitiveTwoByteASNFourByteAdministratorRT((int) buffer.readShort(),
                            (long) buffer.readInt());
                    break;
                default:
                    // all non-RT types are currently unimplemented
                    extcomm = new UnknownTransitiveTwoByteASNSpecificExtendedCommunity(transCommType,
                            twoOctASNLowerType, buffer.readBytes(6).array());
                }
                break;

            case TWO_OCTET_IPv4_ADDRESS_SPECIFIC:

                TransitiveIPv4AddressSpecificExtCommSubTypes ipv4LowerType = TransitiveIPv4AddressSpecificExtCommSubTypes
                        .fromCode(buffer.readByte());

                switch (ipv4LowerType) {
                case ROUTE_TARGET:
                    try {
                        extcomm = new TransitiveIPv4AddressTwoByteAdministratorRT(
                                (Inet4Address) InetAddresses
                                        .fromLittleEndianByteArray(buffer.readBytes(4).array()),
                                (int) buffer.readShort());
                    } catch (UnknownHostException e) {
                        ByteBuf data = Unpooled.buffer();
                        data.getByte(ipv4LowerType.toCode());
                        data.readBytes(buffer.readBytes(6));
                        extcomm = new UnknownTransitiveExtendedCommunity(transCommType, data.array());
                    }
                    break;

                default:

                    // all non-RT types are currently unimplemented
                    extcomm = new UnknownTransitiveIPv4AddressSpecificExtendedCommunity(transCommType,
                            ipv4LowerType, buffer.readBytes(6).array());
                    break;

                }
                break;

            default:
                // by default, just create an unknown type, reading the subsequent
                // 7 bytes (we have already read byte 1)
                extcomm = new UnknownTransitiveExtendedCommunity(transCommType, buffer.readBytes(7).array());
            }
        } else {
            // bit 7 is set, these are non-transitive
            NonTransitiveExtendedCommunityType nonTransCommType = NonTransitiveExtendedCommunityType
                    .fromCode((byte) (higherType & (~(3 << 6))));
            // all non-transitive types are currently unimplemented
            extcomm = new UnknownNonTransitiveExtendedCommunity(nonTransCommType, buffer.readBytes(7).array());
        }
        attr.getMembers().add(extcomm);
    }
    return attr;
}