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
    评论
以下是一个简单的 Java IP 脱敏工具类的示例代码: ``` public class IpMaskUtil { /** * 将 IPv4 地址转换为脱敏格式 * * @param ip 原始 IPv4 地址 * @return 脱敏后的 IPv4 地址 */ public static String maskIpv4(String ip) { if (ip == null || ip.isEmpty()) { return ""; } String[] segments = ip.split("\\."); if (segments.length != 4) { return ip; // 非法 IP 地址,直接返回原始字符串 } StringBuilder builder = new StringBuilder(); builder.append(segments[0]).append("."); builder.append("***").append("."); builder.append("***").append("."); builder.append(segments[3]); return builder.toString(); } /** * 将 IPv6 地址转换为脱敏格式 * * @param ip 原始 IPv6 地址 * @return 脱敏后的 IPv6 地址 */ public static String maskIpv6(String ip) { if (ip == null || ip.isEmpty()) { return ""; } String[] segments = ip.split(":"); if (segments.length < 2 || segments.length > 8) { return ip; // 非法 IP 地址,直接返回原始字符串 } StringBuilder builder = new StringBuilder(); int i; for (i = 0; i < segments.length - 1; i++) { builder.append(segments[i]).append(":"); } builder.append("***:***:").append(segments[i]); return builder.toString(); } } ``` 使用示例: ``` String ipv4 = "192.168.0.1"; String ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; String maskedIpv4 = IpMaskUtil.maskIpv4(ipv4); String maskedIpv6 = IpMaskUtil.maskIpv6(ipv6); System.out.println(maskedIpv4); // 输出:192.***.***.1 System.out.println(maskedIpv6); // 输出:2001:0db8:85a3:***:***:8a2e:0370:7334 ``` 注意,这只是一个简单的示例代码,实际应用中需要根据具体情况进行适当的修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值