iputil1

package com.security.dpi.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* IP��ַ��ع�����
*
* <p>
* detailed comment
* @author james 2008-9-10
* @see
* @since 1.0
*/
public class IpUtils
{
    static long IP_SPAN_MAX_SIZE = 100000;
   
    private static Log logger = LogFactory.getLog(IpUtils.class);

    public static long ipToLong(String strIp)
    {
        if (!ipValid(strIp))
        {
            return 0;
        }
        // //String ip[] = strIp.split("\\.");
        // /* ��λip��ַ�ַ��еġ�.����� */
        // int position1 = strIp.indexOf(".");
        // int position2 = strIp.indexOf(".", position1 + 1);
        // int position3 = strIp.indexOf(".", position2 + 1);
        // /* ��ÿ��.֮����ַ�ת����long */
        // long one = Long.parseLong(strIp.substring(0, position1));
        // long two = Long.parseLong(strIp.substring(position1 + 1, position2));
        // long three = Long.parseLong(strIp.substring(position2 + 1,
        // position3));
        // long four = Long.parseLong(strIp.substring(position3 + 1));

        // return (one << 24) + (two << 16) + (three << + four;
        int result = 0;
        try
        {
            InetAddress addr = InetAddress.getByName(strIp);
            byte[] bas = addr.getAddress();
            result |= ((short) (bas[0] & 0x00ff) << 24);
            result |= ((short) (bas[1] & 0x00ff) << 16);
            result |= ((short) (bas[2] & 0x00ff) << ;
            result |= (short) (bas[3] & 0x00ff);

            long ret = result >>> 1;
            ret = ret << 1;
            ret |= (result << 31) >>> 31;

            return ret;
        }
        catch (UnknownHostException e)
        {
            logger.error(e.getMessage(), e);
        }
        return 0L;
    }

猜你喜欢

转载自sunxuecheng.iteye.com/blog/1178979