IPv4工具类

 public class IPv4Util {
    /**
     * <P>
     * Converts an 8-bit byte to a 64-bit long integer. If the quantity is a
     * sign extended negative number then the value of 256 is added to wrap the
     * conversion into the range of [0..255].
     * </P>
     *
     * @param b
     *            The byte to convert
     *
     * @return The converted 64-bit unsigned value.
     */
    static long byteToLong(byte b) {
        long r = (long) b;
        if (r < 0)
            r += 256;
        return r;
    }

    /**
     * <P>
     * The convertToLong method takes an array of bytes and shifts them into a
     * long value. The bytes at the front of the array are shifted into the MSB
     * of the long as each new byte is added to the LSB of the long. if the
     * array is of sufficent size the first bytes of the array may be shifted
     * out of the returned long.
     * </P>
     *
     * @param addr
     *            The array to convert to a long.
     *
     * @return The created long value.
     *
     * @exception IllegalArgumentException
     *                Thrown if the addr parameter is null.
     *
     */
    public static long convertToLong(byte[] addr) {
        if (addr == null)
            throw new IllegalArgumentException("The passed array must not be null");

        long address = 0;
        for (int i = 0; i < addr.length; i++) {
            address <<= 8;
            address |= byteToLong(addr[i]);
        }
        return address;
    }

    /**
     * This method is used to convert a dotted decimal IP address composed of
     * four octets into a long value. The long is returned in network byte order
     * so that it can be compared using simple equality operators.
     *
     * @param ipAddressString
     *            The dotted decimal address string to convert
     *
     * @return The created long value
     *
     * @exception IllegalArgumentException
     *                Thrown if the ipAddressString parameter is null.
     */
    public static long convertToLong(String ipAddressString) {
        long result = 0;
        int octet = 0;
        byte[] buf = ipAddressString.getBytes();

        for (int i = 0; i < buf.length; i++) {
            if (buf[i] == '.') {
                result = (result << 8) | octet;
                octet = 0;
            } else {
                // 48 is decimal for the character '0'
                //
                octet = octet * 10 + (buf[i] - 48);
            }
        }
        return (result << 8) | (long) octet;
    }

    /**
     * Given a list of IP addresses, return the lowest as determined by the
     * numeric representation and not the alphanumeric string.
     */
    public static InetAddress getLowestInetAddress(InetAddress[] addresses) {
        if (addresses == null) {
            throw new IllegalArgumentException("Cannot take null parameters.");
        }

        ArrayList list = new ArrayList();

        for (int i = 0; i < addresses.length; i++) {
            list.add(addresses[i]);
        }

        return getLowestInetAddress(list);
    }

    /**
     * Given a list of IP addresses, return the lowest as determined by the
     * numeric representation and not the alphanumeric string.
     */
    public static InetAddress getLowestInetAddress(List addresses) {
        if (addresses == null) {
            throw new IllegalArgumentException("Cannot take null parameters.");
        }

        InetAddress lowest = null;
        long lowestLong = Long.MAX_VALUE;
        Iterator iterator = addresses.iterator();

        while (iterator.hasNext()) {
            InetAddress temp = (InetAddress) iterator.next();
            long tempLong = convertToLong(temp.getAddress());

            if (tempLong < lowestLong) {
                lowestLong = tempLong;
                lowest = temp;
            }
        }

        return lowest;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值