*/
static InetAddress ANY_IF;
- private static final byte[] localhostAddress = { 127, 0, 0, 1 };
+ private static final byte[] loopbackAddress = { 127, 0, 0, 1 };
+
+ private static final InetAddress loopback
+ = new InetAddress (loopbackAddress, "localhost");
private static InetAddress localhost = null;
* default. This method is equivalent to returning the first element in
* the InetAddress array returned from GetAllByName.
*
- * @param hostname The name of the desired host, or null for the local machine.
+ * @param hostname The name of the desired host, or null for the local
+ * loopback address.
*
* @return The address of the host as an InetAddress object.
*
public static InetAddress getByName(String hostname)
throws UnknownHostException
{
+ // If null or the empty string is supplied, the loopback address
+ // is returned. Note that this is permitted without a security check.
+ if (hostname == null || hostname.length() == 0)
+ return loopback;
+
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkConnect(hostname, -1);
- // Default to current host if necessary
- if (hostname == null || hostname.length() == 0)
- return getLocalHost();
-
// Assume that the host string is an IP address
byte[] address = aton(hostname);
if (address != null)
}
// Try to resolve the host by DNS
- InetAddress[] addresses = getAllByName(hostname);
- return addresses[0];
+ InetAddress result = new InetAddress(null, null);
+ lookup (hostname, result, false);
+ return result;
}
/**
* hostname of the local machine is supplied by default.
*
* @param hostname The name of the desired host, or null for the
- * local machine.
+ * local loopback address.
*
* @return All addresses of the host as an array of InetAddress objects.
*
public static InetAddress[] getAllByName(String hostname)
throws UnknownHostException
{
+ // If null or the empty string is supplied, the loopback address
+ // is returned. Note that this is permitted without a security check.
+ if (hostname == null || hostname.length() == 0)
+ return new InetAddress[] {loopback};
+
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkConnect(hostname, -1);
// However, if there is a security manager, and the cached result
// is other than "localhost", we need to check again.
if (localhost == null
- || (s != null && localhost.addr != localhostAddress))
+ || (s != null && ! localhost.isLoopbackAddress()))
getLocalHost (s);
return localhost;
}
if (localhost == null)
- localhost = new InetAddress (localhostAddress, "localhost");
+ localhost = new InetAddress (loopbackAddress, "localhost");
}
/**