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

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

Introduction

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

Prototype

public static Inet4Address fromInteger(int address) 

Source Link

Document

Returns an Inet4Address having the integer value specified by the argument.

Usage

From source file:net.tomp2p.peers.PeerAddress.java

/**
 * Creates a PeerAddress from a continuous byte array. This is useful if you
 * don't know the size beforehand. The new offset can be accessed with
 * offset().//ww  w  . j ava  2  s . c o  m
 * 
 * @param me
 *            The serialized array
 * @param offset
 *            the offset, where to start
 * @throws UnknownHostException
 *             Using InetXAddress.getByAddress creates this exception.
 */
public PeerAddress(final byte[] me, int offset) {
    // get the peer ID, this is independent of the type
    final int offsetOld = offset;
    final byte tmp[] = new byte[Number160.BYTE_ARRAY_SIZE];
    System.arraycopy(me, offset, tmp, 0, Number160.BYTE_ARRAY_SIZE);
    this.id = new Number160(tmp);
    offset += Number160.BYTE_ARRAY_SIZE;
    // get the type
    final int options = me[offset] & 0xff;
    this.net6 = (options & NET6) > 0;
    this.firewalledUDP = (options & FIREWALL_UDP) > 0;
    this.firewalledTCP = (options & FIREWALL_TCP) > 0;
    offset++;
    // get the port for UDP and TCP
    this.portTCP = ((me[offset] & 0xff) << 8) + (me[offset + 1] & 0xff);
    this.portUDP = ((me[offset + 2] & 0xff) << 8) + (me[offset + 3] & 0xff);
    offset += 4;
    //
    final byte tmp2[];
    if (isIPv4()) {
        // IPv4 is 32 bit
        tmp2 = new byte[4];
        System.arraycopy(me, offset, tmp2, 0, 4);
        InetAddresses.fromInteger(22);
        try {
            this.address = Inet4Address.getByAddress(tmp2);
        } catch (UnknownHostException e) {
            /*
              * This really shouldn't happen in practice since all our byte
              * sequences have the right length.
              *
              * However {@link InetAddress#getByAddress} is documented as
              * potentially throwing this "if IP address is of illegal length".
              *
              */
            throw new IllegalArgumentException(
                    String.format("Host address '%s' is not a valid IPv4 address.", Arrays.toString(tmp2)), e);
        }
        offset += 4;
    } else {
        // IPv6 is 128 bit
        tmp2 = new byte[16];
        System.arraycopy(me, offset, tmp2, 0, 16);
        try {
            this.address = Inet6Address.getByAddress(tmp2);
        } catch (UnknownHostException e) {
            /*
              * This really shouldn't happen in practice since all our byte
              * sequences have the right length.
              *
              * However {@link InetAddress#getByAddress} is documented as
              * potentially throwing this "if IP address is of illegal length".
              *
              */
            throw new IllegalArgumentException(
                    String.format("Host address '%s' is not a valid IPv4 address.", Arrays.toString(tmp2)), e);
        }
        offset += 16;
    }
    this.readBytes = offset - offsetOld;
    this.offset = offset;
    this.hashCode = id.hashCode();
}

From source file:com.eucalyptus.util.Internets.java

public static InetAddress any() {
    return InetAddresses.fromInteger(0);
}

From source file:com.buildabrand.gsb.util.URLUtils.java

private String convertIpAddress(String ipAddr) {
    String[] ipAddrSplit = StringUtils.split(ipAddr, '.');

    if (ipAddrSplit.length == 0 || ipAddrSplit.length > 4) {
        return null;
    }/* w w w  .  j a  v  a  2s.com*/

    // Basically we should parse octal if we can, but if there are illegal octal
    // numbers, i.e. 08 or 09, then we should just look at decimal and hex.
    boolean allowOctal = !FIND_BAD_OCTAL_REGEXP.matcher(ipAddr).find();

    BigInteger ipNumeric = BigInteger.ZERO;
    int i = 0;
    while (i < ipAddrSplit.length - 1) {
        ipNumeric = ipNumeric.shiftLeft(8);
        BigInteger componentBigInt = convertComponent(ipAddrSplit[i], allowOctal);
        if (componentBigInt == null) {
            return null;
        }

        ipNumeric = ipNumeric.add(componentBigInt);
        i++;
    }
    while (i < 4) {
        ipNumeric = ipNumeric.shiftLeft(8);
        i++;
    }
    BigInteger componentBigInt = convertComponent(ipAddrSplit[ipAddrSplit.length - 1], allowOctal);
    if (componentBigInt == null) {
        return null;
    }
    ipNumeric = ipNumeric.add(componentBigInt);

    return InetAddresses.fromInteger((ipNumeric.intValue())).getHostAddress();
}

From source file:org.wso2.carbon.apimgt.rest.api.core.utils.SampleTestObjectCreator.java

/**
 * Create unique BlockConditions  objects.
 *
 * @return BlockConditions object/*from   w w  w.  ja  va 2s. c o m*/
 */
public static BlockConditions createUniqueBlockConditions(String conditionType) {
    BlockConditions blockConditions = new BlockConditions();
    blockConditions.setUuid(UUID.randomUUID().toString());
    blockConditions.setConditionId(new Random().nextInt());
    blockConditions.setConditionType(conditionType);
    if ((APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE).equals(conditionType)) {
        // Generate random IP addresses
        blockConditions.setEndingIP(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
        blockConditions.setStartingIP(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
    } else if ((APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP).equals(conditionType)) {
        blockConditions.setConditionValue(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
    }
    blockConditions.setEnabled(new Random().nextBoolean());
    return blockConditions;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static void createSampleData() throws UnknownHostException {
    //setup the random generator and seeds
    Integer serverMin = 10000000, serverMax = 20000000;
    Integer networkMin = 30000000, networkMax = 40000000;
    Integer appMin = 50000000, appMax = 60000000;
    int numServers = 40, numNetworks = 5, numApps = 2;

    //for now use a clean all the tables before generating the data
    ds.getCollection(GeminiApplication.class).drop();
    ds.getCollection(GeminiNetwork.class).drop();
    ds.getCollection(GeminiServer.class).drop();

    //create the application repository object
    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    GeminiNetworkRepositoryMongoDBImpl netDB = new GeminiNetworkRepositoryMongoDBImpl(mongoClient, morphia,
            "Gemini");
    GeminiServerRepositoryMongoDBImpl srvDB = new GeminiServerRepositoryMongoDBImpl(mongoClient, morphia,
            "Gemini");

    //first the server objects
    Random sRand = new Random();
    for (int i = 0; i < numServers; i++) {
        String serverID = String.valueOf(sRand.nextInt((serverMax - serverMin) + 1) + serverMin);
        GeminiServer s = new GeminiServer();
        s.setName(serverID + "name");
        s.setDescription(serverID + " description");
        s.setAddress(InetAddresses/*from   www .j  a va  2 s  .c o  m*/
                .forString(InetAddresses.fromInteger(Integer.parseInt(serverID)).getHostAddress()));
        s.setSubnetMask("255.255.255.0");
        s.setPort(443);
        s.setOs("Linux");
        s.setManufacturer(serverID + " manu");
        s.setBackupSize(Integer.parseInt(serverID) + 10);
        s.setLocation(serverID + " locker");
        s.setAdmin(serverID + " admin");
        s.setPassword(serverID + " password");
        srvDB.add(s);
        servers.add(s);
    }

    //next create network objects
    Random nRand = new Random();
    for (int i = 0; i < numNetworks; i++) {
        String networkID = String.valueOf(nRand.nextInt((networkMax - networkMin) + 1) + networkMin);
        GeminiNetwork n = new GeminiNetwork();
        n.setNetworkType("Class C");
        n.setStart(InetAddresses
                .forString(InetAddresses.fromInteger(Integer.parseInt(networkID)).getHostAddress()));
        n.setEnd(InetAddresses.increment(n.getStart()));

        //add 'numServer' divided by 'numNetwork' servers from the servers array to each network
        for (int start = i * (numServers / numNetworks), j = 0; j < (numServers / numNetworks)
                && (start + j) < numServers; j++) {
            n.addServer(servers.get(start + j));
        }
        netDB.add(n);
        networks.add(n);
    }

    //next create the application objects
    Random aRand = new Random();
    for (int i = 0; i < numApps; i++) {
        String appID = String.valueOf(aRand.nextInt((appMax - appMin) + 1) + appMin);
        GeminiApplication a = new GeminiApplication();
        //a.setId(appID.toString());
        a.setName(appID + " name");
        a.setDescription(appID + " description");
        a.setCustom(appID + " custom");
        a.setLocation(appID + " location");
        a.setBackupSize(500);

        //add 'numNetworks' divided by 'numApps' networks from the networks array to each applicaiton
        for (int start = i * (numNetworks / numApps), j = 0; j < (numNetworks / numApps)
                && (start + j) < numNetworks; j++) {
            a.addNetwork(networks.get(start + j));
        }

        //add 'numServers' divided by 'numApps' servers from the servers array to each applicaiton
        for (int start = i * (numServers / numApps), j = 0; j < (numServers / numApps)
                && (start + j) < numServers; j++) {
            a.addServer(servers.get(start + j));
        }

        //add to database
        applications.add(a);
        appDB.add(a);
    }
}