262 |
} |
} |
263 |
|
|
264 |
/** |
/** |
265 |
|
* Utility routine to check if the InetAddress in a wildcard address |
266 |
|
* |
267 |
|
* @since 1.4 |
268 |
|
*/ |
269 |
|
public boolean isAnyLocalAddress() |
270 |
|
{ |
271 |
|
// This is the IPv4 implementation. |
272 |
|
// Any class derived from InetAddress should override this. |
273 |
|
|
274 |
|
return equals (ANY_IF); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* Utility routine to check if the InetAddress is a loopback address |
279 |
|
* |
280 |
|
* @since 1.4 |
281 |
|
*/ |
282 |
|
public boolean isLoopbackAddress() |
283 |
|
{ |
284 |
|
// This is the IPv4 implementation. |
285 |
|
// Any class derived from InetAddress should override this. |
286 |
|
|
287 |
|
return addr [0] == 0x7F; |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
* Utility routine to check if InetAddress is a link local address |
* Utility routine to check if InetAddress is a link local address |
292 |
* |
* |
293 |
* @since 1.4 |
* @since 1.4 |
390 |
*/ |
*/ |
391 |
public byte[] getAddress() |
public byte[] getAddress() |
392 |
{ |
{ |
393 |
byte[] ipaddr = new byte [addr.length]; |
// An experiment shows that JDK1.2 returns a different byte array each |
394 |
|
// time. This makes sense, in terms of security. |
395 |
for (int i = 0; i < addr.length; i++) |
return (byte[]) addr.clone(); |
|
{ |
|
|
ipaddr [i] = (byte) addr [i]; |
|
|
} |
|
|
|
|
|
return ipaddr; |
|
396 |
} |
} |
397 |
|
|
398 |
/** |
/** |
426 |
*/ |
*/ |
427 |
public int hashCode() |
public int hashCode() |
428 |
{ |
{ |
429 |
long val1 = 0, val2 = 0; |
// There hashing algorithm is not specified, but a simple experiment |
430 |
|
// shows that it is equal to the address, as a 32-bit big-endian integer. |
431 |
// Its obvious here that I have no idea how to generate a good |
int hash = 0; |
432 |
// hash key |
int len = addr.length; |
433 |
for (int i = 0; i < addr.length; i++) |
int i = len > 4 ? len - 4 : 0; |
434 |
val1 = val1 + (addr [i] << ((addr.length - i) / 8)); |
|
435 |
|
for ( ; i < len; i++) |
436 |
for (int i = 0; i < addr.length; i++) |
hash = (hash << 8) | (addr[i] & 0xFF); |
437 |
val2 = val2 + (addr [i] * 10 * i); |
|
438 |
|
return hash; |
|
val1 = (val1 >> 1) ^ val2; |
|
|
|
|
|
return ((int) val1); |
|
439 |
} |
} |
440 |
|
|
441 |
/** |
/** |
454 |
|| ! (obj instanceof InetAddress)) |
|| ! (obj instanceof InetAddress)) |
455 |
return false; |
return false; |
456 |
|
|
457 |
byte[] test_ip = ((InetAddress) obj).getAddress(); |
// "The Java Class Libraries" 2nd edition says "If a machine has |
458 |
|
// multiple names instances of InetAddress for different name of |
459 |
if (test_ip.length != addr.length) |
// that same machine are not equal. This is because they have |
460 |
|
// different host names." This violates the description in the |
461 |
|
// JDK 1.2 API documentation. A little experimentation |
462 |
|
// shows that the latter is correct. |
463 |
|
byte[] addr2 = ((InetAddress) obj).addr; |
464 |
|
|
465 |
|
if (addr.length != addr2.length) |
466 |
return false; |
return false; |
467 |
|
|
468 |
for (int i = 0; i < addr.length; i++) |
for (int i = 0; i < addr.length; i++) |
469 |
if (test_ip [i] != (byte) addr [i]) |
if (addr [i] != addr2 [i]) |
470 |
return false; |
return false; |
471 |
|
|
472 |
return true; |
return true; |
473 |
} |
} |
553 |
* |
* |
554 |
* @return The address of the host as an InetAddress object. |
* @return The address of the host as an InetAddress object. |
555 |
* |
* |
556 |
* @exception UnknownHostException If no IP address can be found for the |
* @exception UnknownHostException If no IP address for the host could |
557 |
* given hostname |
* be found |
558 |
|
* @exception SecurityException If a security manager exists and its |
559 |
|
* checkConnect method doesn't allow the operation |
560 |
*/ |
*/ |
561 |
public static InetAddress getByName (String hostname) |
public static InetAddress getByName (String hostname) |
562 |
throws UnknownHostException |
throws UnknownHostException |
563 |
{ |
{ |
564 |
|
SecurityManager s = System.getSecurityManager(); |
565 |
|
if (s != null) |
566 |
|
s.checkConnect (hostname, -1); |
567 |
|
|
568 |
// Default to current host if necessary |
// Default to current host if necessary |
569 |
if (hostname == null) |
if (hostname == null) |
570 |
return getLocalHost(); |
return getLocalHost(); |
618 |
* |
* |
619 |
* @return All addresses of the host as an array of InetAddress objects. |
* @return All addresses of the host as an array of InetAddress objects. |
620 |
* |
* |
621 |
* @exception UnknownHostException If no IP address can be found for the |
* @exception UnknownHostException If no IP address for the host could |
622 |
* given hostname |
* be found |
623 |
|
* @exception SecurityException If a security manager exists and its |
624 |
|
* checkConnect method doesn't allow the operation |
625 |
*/ |
*/ |
626 |
public static InetAddress[] getAllByName (String hostname) |
public static InetAddress[] getAllByName (String hostname) |
627 |
throws UnknownHostException |
throws UnknownHostException |