inetaddress.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 558 行 · 第 1/2 页
JAVA
558 行
/*
* @(#)InetAddress.java 1.46 98/07/27
*
* Copyright 1995-1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.net;
import java.util.Hashtable;
/**
* This class represents an Internet Protocol (IP) address.
* <p>
* Applications should use the methods <code>getLocalHost</code>,
* <code>getByName</code>, or <code>getAllByName</code> to
* create a new <code>InetAddress</code> instance.
*
* @author Chris Warth
* @version 1.46, 07/27/98
* @see java.net.InetAddress#getAllByName(java.lang.String)
* @see java.net.InetAddress#getByName(java.lang.String)
* @see java.net.InetAddress#getLocalHost()
* @since JDK1.0
*/
public final
class InetAddress implements java.io.Serializable {
String hostName;
int address; // Currently we only deal effectively with 32-bit addresses.
// However this field can be expanded to be a byte array
// or a 64-bit quantity without too much effort.
int family;
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 3286316764910316507L;
/*
* Load net library into runtime.
*/
static {
System.loadLibrary("net");
}
/**
* Constructor for the Socket.accept() method.
* This creates an empty InetAddress, which is filled in by
* the accept() method. This InetAddress, however, is not
* put in the address cache, since it is not created by name.
*/
InetAddress() {
family = impl.getInetFamily();
}
/**
* Creates an InetAddress with the specified host name and IP address.
* @param hostName the specified host name
* @param addr the specified IP address. The address is expected in
* network byte order.
* @exception UnknownHostException If the address is unknown.
*/
InetAddress(String hostName, byte addr[]) {
this.hostName = new String(hostName);
this.family = impl.getInetFamily();
/*
* We must be careful here to maintain the network byte
* order of the address. As it comes in, the most
* significant byte of the address is in addr[0]. It
* actually doesn't matter what order they end up in the
* array, as long as it is documented and consistent.
*/
address = addr[3] & 0xFF;
address |= ((addr[2] << 8) & 0xFF00);
address |= ((addr[1] << 16) & 0xFF0000);
address |= ((addr[0] << 24) & 0xFF000000);
}
/**
* Utility routine to check if the InetAddress is a
* IP multicast address. IP multicast address is a Class D
* address i.e first four bits of the address are 1110.
* @since JDK1.1
*/
public boolean isMulticastAddress() {
return ((address & 0xf0000000) == 0xe0000000);
}
/**
* Returns the host name for this address.
* If the host is equal to null, then this address refers to any
* of the local machine's available network addresses.
*
* @return the fully qualified host name for this address.
* @since JDK1.0
*/
public String getHostName() {
if (hostName == null) {
try {
hostName = new String(impl.getHostByAddr(address));
InetAddress[] arr = (InetAddress[])addressCache.get(hostName);
if(arr != null) {
for(int i = 0; i < arr.length; i++) {
if(hostName.equalsIgnoreCase(arr[i].hostName) &&
address != arr[i].address) {
/* This means someone's playing funny games with DNS
* This hostName used to have one IP address, now it
* has another. At any rate, don't let them "see"
* the new hostName, and don't cache it either.
*/
hostName = getHostAddress();
break;
}
}
} else {
/* hostname wasn't in cache before. It's a real hostname,
* not "%d.%d.%d.%d" so cache it
*/
arr = new InetAddress[1];
arr[0] = this;
if (cacheing ()) /* IBM */
addressCache.put(hostName, arr);
}
/* finally check to see if calling code is allowed to know
* the hostname for this IP address, ie, connect to the host
*/
SecurityManager sec = System.getSecurityManager();
if (sec != null && !sec.getInCheck()) {
sec.checkConnect(hostName, -1);
}
} catch (SecurityException e) {
hostName = getHostAddress();
} catch (UnknownHostException e) {
hostName = getHostAddress();
}
}
return hostName;
}
/**
* Returns the raw IP address of this <code>InetAddress</code>
* object. The result is in network byte order: the highest order
* byte of the address is in <code>getAddress()[0]</code>.
*
* @return the raw IP address of this object.
* @since JDK1.0
*/
public byte[] getAddress() {
byte[] addr = new byte[4];
addr[0] = (byte) ((address >>> 24) & 0xFF);
addr[1] = (byte) ((address >>> 16) & 0xFF);
addr[2] = (byte) ((address >>> 8) & 0xFF);
addr[3] = (byte) (address & 0xFF);
return addr;
}
/**
* Returns the IP address string "%d.%d.%d.%d"
* @return raw IP address in a string format
* @since JDK1.1
*/
public String getHostAddress() {
return ((address >>> 24) & 0xFF) + "." +
((address >>> 16) & 0xFF) + "." +
((address >>> 8) & 0xFF) + "." +
((address >>> 0) & 0xFF);
}
/**
* Returns a hashcode for this IP address.
*
* @return a hash code value for this IP address.
* @since JDK1.0
*/
public int hashCode() {
return address;
}
/**
* Compares this object against the specified object.
* The result is <code>true</code> if and only if the argument is
* not <code>null</code> and it represents the same IP address as
* this object.
* <p>
* Two instances of <code>InetAddress</code> represent the same IP
* address if the length of the byte arrays returned by
* <code>getAddress</code> is the same for both, and each of the
* array components is the same for the byte arrays.
*
* @param obj the object to compare against.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
* @see java.net.InetAddress#getAddress()
* @since JDK1.0
*/
public boolean equals(Object obj) {
return (obj != null) && (obj instanceof InetAddress) &&
(((InetAddress)obj).address == address);
}
/**
* Converts this IP address to a <code>String</code>.
*
* @return a string representation of this IP address.
* @since JDK1.0
*/
public String toString() {
return getHostName() + "/" + getHostAddress();
}
/*
* Cached addresses - our own litle nis, not!
*
* Do not purge cache of numerical IP addresses, since
* duplicate dynamic DNS name lookups can leave the system
* vulnerable to hostname spoofing attacks. Once a hostname
* has been looked up in DNS and entered into the Java cache,
* from then on, the hostname is translated to IP address only
* via the cache.
*/
static Hashtable addressCache = new Hashtable();
static InetAddress unknownAddress;
static InetAddress anyLocalAddress;
static InetAddress localHost;
static InetAddress[] unknown_array; // put THIS in cache
static InetAddressImpl impl;
/*
* generic localHost to give back to applets
* - private so not API delta
*/
private static InetAddress loopbackHost;
static {
/*
* Property "impl.prefix" will be prepended to the classname of the
* implementation object we instantiate, to which we delegate the real work
* (like native methods). This property can vary across implementations
* of the java.* classes. The default is an empty String "".
*/
String prefix = System.getProperty("impl.prefix", "");
try {
impl = null;
impl = (InetAddressImpl)(Class.forName("java.net." + prefix + "InetAddressImpl")
.newInstance());
} catch (ClassNotFoundException e) {
System.err.println("Class not found: java.net." + prefix +
"InetAddressImpl:\ncheck impl.prefix property " +
"in your properties file.");
} catch (InstantiationException e) {
System.err.println("Could not instantiate: java.net." + prefix +
"InetAddressImpl:\ncheck impl.prefix property " +
"in your properties file.");
} catch (IllegalAccessException e) {
System.err.println("Cannot access class: java.net." + prefix +
"InetAddressImpl:\ncheck impl.prefix property " +
"in your properties file.");
}
if (impl == null) {
try {
impl = (InetAddressImpl)(Class.forName("java.net.InetAddressImpl")
.newInstance());
} catch (Exception e) {
throw new Error("System property impl.prefix incorrect");
}
}
unknownAddress = new InetAddress();
anyLocalAddress = new InetAddress();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?