// InetAddressFix.java Charles Wicksteed 16 February 2002 // Make numeric host names (IP addresses) work in JDK 1.0.2 package java.net; public final class InetAddressFix { InetAddressFix() {} public static synchronized void fix(String host) { // System.out.println("host = " + host); // System.out.println("size = " + java.net.InetAddress.addressCache.size()); // System.out.println("cache = " + java.net.InetAddress.addressCache.toString()); Object obj = java.net.InetAddress.addressCache.get(host); // If entry in cache, we are OK, already done this name if (obj != null) { return; } // Not in cache, work out the IP address from the string java.util.StringTokenizer st = new java.util.StringTokenizer(host, "."); if (st.countTokens() != 4) return; // or: byte addr[] = new byte[4]; byte[] addr = new byte[4]; // skip out if any component is not numeric for (int i = 0; i < 4; i++) { try { int ip = Integer.parseInt(st.nextToken()); if (ip < 0 || ip > 255) { return; } addr[i] = (byte)ip; } catch (NumberFormatException e) { return; } } // add to the cache // eg add "1.1.1.1"/1.1.1.1 to the cache // each address cache entry is an array of InetAddress // we make an array with one element (see the InetAddress code) InetAddress[] hostByAddr_array = new InetAddress[1]; hostByAddr_array[0] = new InetAddress(host, addr); java.net.InetAddress.addressCache.put(host, hostByAddr_array); // System.out.println("added \"" + host + "\"/" + hostByAddr_array[0].getHostAddress()); } }