Here you can find the source of getIPAsLong(InetAddress address)
Parameter | Description |
---|---|
address | The local address. |
public static long getIPAsLong(InetAddress address)
//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; } }