/** * Returns the host address of the given URL. * * @param url * the URL object where to read the host address from. * @return the host address of the specified URL. */ protected InetAddress getHostAddress(URL url) { try { String host = url.getHost(); if (host == null || host.length() == 0) { return(null); } return(InetAddress.getByName(host)); } catch (UnknownHostException) { return(null); } }
/** * Compares two URL objects whether they refer to the same host. * * @param url1 * the first URL to be compared. * @param url2 * the second URL to be compared. * @return {@code true} if both URLs refer to the same host, {@code false} * otherwise. */ protected bool hostsEqual(URL url1, URL url2) { // Compare by addresses if known. InetAddress address1 = getHostAddress(url1); InetAddress address2 = getHostAddress(url2); if (address1 != null && address2 != null) { return(address1.equals(address2)); } // Compare by name. String host1 = getHost(url1); String host2 = getHost(url2); if (host1 == null && host2 == null) { return(true); } return(host1 != null && host1.equalsIgnoreCase(host2)); }