// // DottedQuad is a class that converts string representations of // dotted quads into an equivalent integer representation (and vice // versa). Generally, you want to use java.net.InetAddress, but if // you communicate directly with the DNS server, you need to convert // integer representations of IP addresses into string // representations. This class will help you do that. // import java.util.Vector; import java.util.Enumeration; import java.lang.IllegalArgumentException; import java.lang.NumberFormatException; public class DottedQuad { public DottedQuad() { // empty } public DottedQuad(String s) throws IllegalArgumentException { initialize(s); } public DottedQuad(int i) { initialize(i); } public void initialize(String s) { componentStrings = StringUtil.split(s, '.'); componentIntegers = componentStringsToComponentIntegers(componentStrings); verifyComponentIntegers(componentIntegers); this.s = StringUtil.join(componentStrings, '.'); this.i = componentIntegersToInt(componentIntegers); } public void initialize(int i) { componentIntegers = IntToComponentIntegers(i); verifyComponentIntegers(componentIntegers); componentStrings = componentIntegersToComponentStrings(componentIntegers); this.s = StringUtil.join(componentStrings, '.'); this.i = i; } public String toString() { return s; } public int intValue() { return i; } private Vector componentStringsToComponentIntegers(Vector componentStrings) throws IllegalArgumentException { int size = componentStrings.size(); if( size != 4 ) { throw new IllegalArgumentException("not a quad"); } Vector rv = new Vector(); for( int i = 0 ; i < size ; ++i ) { String s = (String)componentStrings.elementAt(i); try { rv.add(Integer.decode(s)); } catch( NumberFormatException e ) { throw new IllegalArgumentException("component \"" + s + "\" is not an integer"); } } return rv; } private void verifyComponentIntegers(Vector componentIntegers) throws IllegalArgumentException { int size = componentIntegers.size(); if( size != 4 ) { throw new IllegalArgumentException("not a quad"); } for( int i = 0 ; i < size ; ++i ) { int x = ((Integer)componentIntegers.elementAt(i)).intValue(); if( (x < 0) || (x > 255) ) { throw new IllegalArgumentException("component \"" + x + "\" out of range"); } } } private int componentIntegersToInt(Vector componentIntegers) { // This assumes you have already called verifyComponentIntegers(). int[] v = new int[4]; for( int i = 0 ; i < 4 ; ++i ) { v[i] = ((Integer)componentIntegers.elementAt(i)).intValue(); } return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3]; } private Vector IntToComponentIntegers(int i) { int mask = 0xff; int[] v = new int[4]; Vector rv = new Vector(); for( int j = 3 ; j >= 0 ; --j ) { v[j] = i & mask; i >>= 8; } for( int j = 0 ; j < 4 ; ++j ) { rv.add(new Integer(v[j])); } return rv; } private Vector componentIntegersToComponentStrings(Vector componentIntegers) { Vector rv = new Vector(); for( int i = 0 ; i < 4 ; ++i ) { rv.add(String.valueOf(componentIntegers.elementAt(i))); } return rv; } // "s" and "i" are the end results. private String s; private int i; // For "componentStrings", the leftmost component of the dotted // quad is at element "0". private Vector componentStrings; // For "componentIntegers", the most-significant byte is in // element "0". private Vector componentIntegers; }