Java InetAddress Create getIPAsLong(InetAddress address)

Here you can find the source of getIPAsLong(InetAddress address)

Description

A convenient method that accepts an IP address represented by a byte[] of size 4 and returns this as a long representation of the same IP address.

License

MIT License

Parameter

Parameter Description
address The local address.

Return

A long representation of the IP address.

Declaration

public static long getIPAsLong(InetAddress address) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. jav a 2s  .c o m*/
 * Copyright (C) 2015 Nameless Production Committee
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://opensource.org/licenses/mit-license.php
 */

import java.net.InetAddress;
import java.net.InetSocketAddress;

import java.net.ServerSocket;
import java.net.Socket;

public class Main {
    /**
     * A convenient method that accepts an IP address represented by a byte[] of size 4 and returns
     * this as a long representation of the same IP address.
     * 
     * @param socket The local socket.
     * @return A long representation of the IP address.
     */
    public static long getIPAsLong(Socket socket) {
        return getIPAsLong(socket.getLocalAddress().getAddress());
    }

    /**
     * A convenient method that accepts an IP address represented by a byte[] of size 4 and returns
     * this as a long representation of the same IP address.
     * 
     * @param socket The server socket.
     * @return A long representation of the IP address.
     */
    public static long getIPAsLong(ServerSocket socket) {
        return getIPAsLong(socket.getInetAddress());
    }

    /**
     * A convenient method that accepts an IP address represented by a byte[] of size 4 and returns
     * this as a long representation of the same IP address.
     * 
     * @param address The local address.
     * @return A long representation of the IP address.
     */
    public static long getIPAsLong(InetAddress address) {
        return getIPAsLong(address.getAddress());
    }

    /**
     * A convenient method that accepts an IP address represented by a byte[] of size 4 and returns
     * this as a long representation of the same IP address.
     * 
     * @param address The local address.
     * @return A long representation of the IP address.
     */
    public static long getIPAsLong(InetSocketAddress address) {
        return getIPAsLong(address.getAddress());
    }

    /**
     * A convenient method that accepts an IP address represented by a byte[] of size 4 and returns
     * this as a long representation of the same IP address.
     * 
     * @param address The byte[] of size 4 representing the IP address.
     * @return A long representation of the IP address.
     */
    public static long getIPAsLong(byte[] address) {
        // check address
        if (address.length != 4) {
            throw new IllegalArgumentException("The given byte array must be of length 4");
        }

        long ip = 0;
        long multiplier = 1;

        for (int i = 3; i >= 0; i--) {
            int byteValue = (address[i] + 256) % 256;
            ip += byteValue * multiplier;
            multiplier *= 256;
        }
        return ip;
    }
}

Related

  1. getInt32FromAddress(Inet4Address inetAddress)
  2. getInterface(InetAddress addr)
  3. getIP(InetAddress ip)
  4. getIP(InetAddress ip)
  5. getIP(InetAddress ip)
  6. getIPAsString(InetAddress addr)
  7. getIPv4LocalNetMask(InetAddress ip, int netPrefix)
  8. getIPv6InetAddresses(NetworkInterface networkInterface)
  9. getLocalInetAddress()