Java IP Address Get getIPFromHash(final long ipHash)

Here you can find the source of getIPFromHash(final long ipHash)

Description

get IP From Hash

License

Open Source License

Declaration

public static final byte[] getIPFromHash(final long ipHash) 

Method Source Code


//package com.java2s;
/*//from  ww w  . ja va2s.co  m
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.InetAddress;

public class Main {
    public static final byte[] getIPFromHash(final long ipHash) {
        final byte[] ip;

        if (isIPv6(ipHash)) {
            ip = new byte[] { (byte) (ipHash & 0xFF), (byte) (ipHash >> 8 & 0xFF), (byte) (ipHash >> 16 & 0xFF),
                    (byte) (ipHash >> 24 & 0xFF), (byte) (ipHash >> 32 & 0xFF), (byte) (ipHash >> 40 & 0xFF) };
        } else {
            ip = new byte[] { (byte) (ipHash & 0xFF), (byte) (ipHash >> 8 & 0xFF), (byte) (ipHash >> 16 & 0xFF),
                    (byte) (ipHash >> 24 & 0xFF) };
        }

        return ip;
    }

    public static final boolean isIPv6(final InetAddress address) {
        return isIPv6(address.getAddress());
    }

    public static final boolean isIPv6(final byte[] address) {
        return address.length == 6;
    }

    public static final boolean isIPv6(final long ipHash) {
        return ipHash < 0;
    }
}

Related

  1. getIpByHost(String hostName)
  2. getIpByHost(String hostName)
  3. getIPByInterfaceName(String name)
  4. getIpByName(String name)
  5. getIpByNetworkInterfaceName(String name)
  6. getIPFromInterface(String ni)
  7. getIPFromNetworkInterface()
  8. getIpHostnameLocal()
  9. getIPList(String address)