commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark R. Diggory" <mdigg...@latte.harvard.edu>
Subject Re: [off topic] IP String to Integer function?
Date Wed, 11 Aug 2004 15:49:31 GMT
I have a chunk of code to do this, hope it helps:


public class IPAddress {
   
    /** Holds value of property ip. */
    protected int address;
   
    /** Creates new IP */
    public IPAddress(String ip) throws MalformedIPException{
        this.set(ip);
    }
   
    /** Getter for property ip.
     * @return Value of property ip.
     */
    public int get(){
        return address;
     }
   
    /** Setter for property ip.
     * @param ip New value of property ip.
     */
     public void set(String ip) throws MalformedIPException{

        if( ip == null) ip = "255.255.255.255";
           
        int IP = 0x00;
        int hitDots = 0;
        char[] data = ip.toCharArray();

        for(int i = 0; i < data.length; i++) {
        char c = data[i];
        if (c < 48 || c > 57) { // !digit
            throw new MalformedIPException("IP Contains Non-digit");
        }
        int b = 0x00;
        while(c != '.') {
            if (c < 48 || c > 57) { // !digit
            throw new MalformedIPException("IP Contains Non-digit");
            }
            b = b*10 + c - '0';

            if (++i >= data.length)
            break;
            c = data[i];
        }
        if(b > 0xFF) { /* bogus - bigger than a byte */
            throw new MalformedIPException("Value is larger than a byte");
        }
        IP = (IP << 8) + b;
        hitDots++;
        }

        if(hitDots != 4 || ip.endsWith(".")) {
        throw new MalformedIPException("Not properly formated IP address");
        }

        address = IP; 
    }
   
   
    public String toString(){
        return ((address >>> 24) & 0xFF) + "." +
               ((address >>> 16) & 0xFF) + "." +
               ((address >>>  8) & 0xFF) + "." +
               ((address >>>  0) & 0xFF);
    }
   
   
    public boolean contains(Object obj) {
       
        if (obj == null) return false;
       
        if (obj instanceof IPAddress)        
            return (((IPAddress)obj).address == address);
       
        return false;
    }

    /**
     * Returns a hashcode for this IP address.
     *
     * @return  a hash code value for this IP address.
     */
    public int hashCode() {
    return address;
    }
    
}


Michael McGrady wrote:

> At 01:49 AM 8/11/2004, you wrote:
>
>> check the standard java package java.net, class InetAddress etc...
>> peter
>
>
>
> I have not checked what the result is, but I do know that InetAddress 
> overides hashcode() in Object.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message