You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/01/05 15:44:06 UTC
svn commit: r493040 [4/11] - in
/harmony/enhanced/classlib/trunk/modules/luni/src/main/java: java/net/
org/apache/harmony/luni/util/
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java?view=diff&rev=493040&r1=493039&r2=493040
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java Fri Jan 5 06:44:04 2007
@@ -17,7 +17,6 @@
package java.net;
-
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -44,707 +43,709 @@
*/
public class InetAddress extends Object implements Serializable {
- final static byte[] any_bytes = { 0, 0, 0, 0 };
+ final static byte[] any_bytes = { 0, 0, 0, 0 };
+
+ final static byte[] localhost_bytes = { 127, 0, 0, 1 };
+
+ static InetAddress ANY = new Inet4Address(any_bytes);
- final static byte[] localhost_bytes = { 127, 0, 0, 1 };
+ private final static INetworkSystem NETIMPL = Platform.getNetworkSystem();
- static InetAddress ANY = new Inet4Address(any_bytes);
-
- private final static INetworkSystem NETIMPL = Platform.getNetworkSystem();
+ final static InetAddress LOOPBACK = new Inet4Address(localhost_bytes,
+ "localhost"); //$NON-NLS-1$
- final static InetAddress LOOPBACK = new Inet4Address(localhost_bytes,
- "localhost");
-
private static final String ERRMSG_CONNECTION_REFUSED = "Connection refused"; //$NON-NLS-1$
- private static final long serialVersionUID = 3286316764910316507L;
+ private static final long serialVersionUID = 3286316764910316507L;
+
+ String hostName;
+
+ private static class WaitReachable {
+ }
- String hostName;
-
- private static class WaitReachable {}
private transient Object waitReachable = new WaitReachable();
-
+
private boolean reached;
-
+
private int addrCount;
- int family = 2;
+ int family = 2;
+
+ byte[] ipaddress;
+
+ // Fill in the JNI id caches
+ private static native void oneTimeInitialization(boolean supportsIPv6);
+
+ static {
+ oneTimeInitialization(true);
+ }
+
+ /**
+ * Constructs an InetAddress.
+ */
+ InetAddress() {
+ super();
+ }
- byte[] ipaddress;
+ /**
+ * Constructs an InetAddress, representing the <code>address</code> and
+ * <code>hostName</code>.
+ *
+ * @param address
+ * network address
+ */
+ InetAddress(byte[] address) {
+ super();
+ this.ipaddress = address;
+ }
- // Fill in the JNI id caches
- private static native void oneTimeInitialization(boolean supportsIPv6);
+ /**
+ * Constructs an InetAddress, representing the <code>address</code> and
+ * <code>hostName</code>.
+ *
+ * @param address
+ * network address
+ */
+ InetAddress(byte[] address, String hostName) {
+ super();
+ this.ipaddress = address;
+ this.hostName = hostName;
+ }
- static {
- oneTimeInitialization(true);
- }
-
- /**
- * Constructs an InetAddress.
- */
- InetAddress() {
- super();
- }
-
- /**
- * Constructs an InetAddress, representing the <code>address</code> and
- * <code>hostName</code>.
- *
- * @param address
- * network address
- */
- InetAddress(byte[] address) {
- super();
- this.ipaddress = address;
- }
-
- /**
- * Constructs an InetAddress, representing the <code>address</code> and
- * <code>hostName</code>.
- *
- * @param address
- * network address
- */
- InetAddress(byte[] address, String hostName) {
- super();
- this.ipaddress = address;
- this.hostName = hostName;
- }
-
- /**
- * Returns the IP address of the argument <code>addr</code> as an array.
- * The elements are in network order (the highest order address byte is in
- * the zero-th element).
- *
- * @return byte[] the network address as a byte array
- */
- static byte[] addressOf(int addr) {
- int temp = addr;
- byte array[] = new byte[4];
- array[3] = (byte) (temp & 0xFF);
- array[2] = (byte) ((temp >>>= 8) & 0xFF);
- array[1] = (byte) ((temp >>>= 8) & 0xFF);
- array[0] = (byte) ((temp >>>= 8) & 0xFF);
- return array;
- }
-
- CacheElement cacheElement() {
- return new CacheElement();
- }
-
- /**
- * Compares this <code>InetAddress</code> against the specified object.
- *
- * @param obj
- * the object to be tested for equality
- * @return boolean true, if the objects are equal
- */
- @Override
+ /**
+ * Returns the IP address of the argument <code>addr</code> as an array.
+ * The elements are in network order (the highest order address byte is in
+ * the zero-th element).
+ *
+ * @return byte[] the network address as a byte array
+ */
+ static byte[] addressOf(int addr) {
+ int temp = addr;
+ byte array[] = new byte[4];
+ array[3] = (byte) (temp & 0xFF);
+ array[2] = (byte) ((temp >>>= 8) & 0xFF);
+ array[1] = (byte) ((temp >>>= 8) & 0xFF);
+ array[0] = (byte) ((temp >>>= 8) & 0xFF);
+ return array;
+ }
+
+ CacheElement cacheElement() {
+ return new CacheElement();
+ }
+
+ /**
+ * Compares this <code>InetAddress</code> against the specified object.
+ *
+ * @param obj
+ * the object to be tested for equality
+ * @return boolean true, if the objects are equal
+ */
+ @Override
public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj.getClass() != this.getClass()) {
- return false;
- }
-
- // now check if their byte arrays match...
- byte[] objIPaddress = ((InetAddress) obj).ipaddress;
- for (int i = 0; i < objIPaddress.length; i++) {
- if (objIPaddress[i] != this.ipaddress[i]) {
+ if (obj == null) {
+ return false;
+ }
+ if (obj.getClass() != this.getClass()) {
+ return false;
+ }
+
+ // now check if their byte arrays match...
+ byte[] objIPaddress = ((InetAddress) obj).ipaddress;
+ for (int i = 0; i < objIPaddress.length; i++) {
+ if (objIPaddress[i] != this.ipaddress[i]) {
return false;
}
- }
- return true;
- }
-
- /**
- * Returns the IP address of this <code>InetAddress</code> as an array.
- * The elements are in network order (the highest order address byte is in
- * the zero-th element).
- *
- * @return byte[] the address as a byte array
- */
- public byte[] getAddress() {
- return ipaddress.clone();
- }
-
- /**
- * Answer the IP addresses of a named host. The host name may either be a
- * machine name or a dotted string IP address. If the host name is empty or
- * null, an UnknownHostException is thrown. If the host name is a dotted IP
- * string, an array with the corresponding single InetAddress is returned.
- *
- * @param host
- * the hostName to be resolved to an address
- *
- * @return InetAddress[] an array of addresses for the host
- * @throws UnknownHostException
- * if the address lookup fails
- */
- public static InetAddress[] getAllByName(String host)
- throws UnknownHostException {
- if (host == null) {
- return new InetAddress[]{preferIPv6Addresses() ? Inet6Address.LOOPBACK : LOOPBACK};
- }
- if (0 == host.length()) {
- throw new UnknownHostException(Msg.getString("K0038"));
- }
-
- if (isHostName(host)) {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
+ }
+ return true;
+ }
+
+ /**
+ * Returns the IP address of this <code>InetAddress</code> as an array.
+ * The elements are in network order (the highest order address byte is in
+ * the zero-th element).
+ *
+ * @return byte[] the address as a byte array
+ */
+ public byte[] getAddress() {
+ return ipaddress.clone();
+ }
+
+ /**
+ * Answer the IP addresses of a named host. The host name may either be a
+ * machine name or a dotted string IP address. If the host name is empty or
+ * null, an UnknownHostException is thrown. If the host name is a dotted IP
+ * string, an array with the corresponding single InetAddress is returned.
+ *
+ * @param host
+ * the hostName to be resolved to an address
+ *
+ * @return InetAddress[] an array of addresses for the host
+ * @throws UnknownHostException
+ * if the address lookup fails
+ */
+ public static InetAddress[] getAllByName(String host)
+ throws UnknownHostException {
+ if (host == null) {
+ return new InetAddress[] { preferIPv6Addresses() ? Inet6Address.LOOPBACK
+ : LOOPBACK };
+ }
+ if (0 == host.length()) {
+ throw new UnknownHostException(Msg.getString("K0038")); //$NON-NLS-1$
+ }
+
+ if (isHostName(host)) {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
security.checkConnect(host, -1);
}
- if (Socket.preferIPv4Stack()) {
- return getAliasesByNameImpl(host);
- }
-
- // ok we may have to re-order to make sure the
- // preferIPv6Addresses is respected
- InetAddress[] returnedAddresses = getAliasesByNameImpl(host);
- InetAddress[] orderedAddresses = null;
- if (returnedAddresses != null) {
- orderedAddresses = new InetAddress[returnedAddresses.length];
- int curPosition = 0;
- if (InetAddress.preferIPv6Addresses()) {
- for (int i = 0; i < returnedAddresses.length; i++) {
- if (returnedAddresses[i] instanceof Inet6Address) {
- orderedAddresses[curPosition] = returnedAddresses[i];
- curPosition++;
- }
- }
- for (int i = 0; i < returnedAddresses.length; i++) {
- if (returnedAddresses[i] instanceof Inet4Address) {
- orderedAddresses[curPosition] = returnedAddresses[i];
- curPosition++;
- }
- }
- } else {
- for (int i = 0; i < returnedAddresses.length; i++) {
- if (returnedAddresses[i] instanceof Inet4Address) {
- orderedAddresses[curPosition] = returnedAddresses[i];
- curPosition++;
- }
- }
- for (int i = 0; i < returnedAddresses.length; i++) {
- if (returnedAddresses[i] instanceof Inet6Address) {
- orderedAddresses[curPosition] = returnedAddresses[i];
- curPosition++;
- }
- }
- }
- }
- return orderedAddresses;
- }
-
- byte[] hBytes = Inet6Util.createByteArrayFromIPAddressString(host);
- return (new InetAddress[] { new InetAddress(hBytes) });
- }
-
- /**
- * Answers the address of a host, given a host string name. The host string
- * may be either a machine name or a dotted string IP address. If the
- * latter, the hostName field will be determined upon demand.
- *
- * @param host
- * the hostName to be resolved to an address
- * @return InetAddress the InetAddress representing the host
- *
- * @throws UnknownHostException
- * if the address lookup fails
- */
- public static InetAddress getByName(String host)
- throws UnknownHostException {
- if (host == null || 0 == host.length()) {
+ if (Socket.preferIPv4Stack()) {
+ return getAliasesByNameImpl(host);
+ }
+
+ // ok we may have to re-order to make sure the
+ // preferIPv6Addresses is respected
+ InetAddress[] returnedAddresses = getAliasesByNameImpl(host);
+ InetAddress[] orderedAddresses = null;
+ if (returnedAddresses != null) {
+ orderedAddresses = new InetAddress[returnedAddresses.length];
+ int curPosition = 0;
+ if (InetAddress.preferIPv6Addresses()) {
+ for (int i = 0; i < returnedAddresses.length; i++) {
+ if (returnedAddresses[i] instanceof Inet6Address) {
+ orderedAddresses[curPosition] = returnedAddresses[i];
+ curPosition++;
+ }
+ }
+ for (int i = 0; i < returnedAddresses.length; i++) {
+ if (returnedAddresses[i] instanceof Inet4Address) {
+ orderedAddresses[curPosition] = returnedAddresses[i];
+ curPosition++;
+ }
+ }
+ } else {
+ for (int i = 0; i < returnedAddresses.length; i++) {
+ if (returnedAddresses[i] instanceof Inet4Address) {
+ orderedAddresses[curPosition] = returnedAddresses[i];
+ curPosition++;
+ }
+ }
+ for (int i = 0; i < returnedAddresses.length; i++) {
+ if (returnedAddresses[i] instanceof Inet6Address) {
+ orderedAddresses[curPosition] = returnedAddresses[i];
+ curPosition++;
+ }
+ }
+ }
+ }
+ return orderedAddresses;
+ }
+
+ byte[] hBytes = Inet6Util.createByteArrayFromIPAddressString(host);
+ return (new InetAddress[] { new InetAddress(hBytes) });
+ }
+
+ /**
+ * Answers the address of a host, given a host string name. The host string
+ * may be either a machine name or a dotted string IP address. If the
+ * latter, the hostName field will be determined upon demand.
+ *
+ * @param host
+ * the hostName to be resolved to an address
+ * @return InetAddress the InetAddress representing the host
+ *
+ * @throws UnknownHostException
+ * if the address lookup fails
+ */
+ public static InetAddress getByName(String host)
+ throws UnknownHostException {
+ if (host == null || 0 == host.length()) {
return InetAddress.LOOPBACK;
}
- if (host.equals("0")) {
- return InetAddress.ANY;
- }
-
- if (isHostName(host)) {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
+ if (host.equals("0")) { //$NON-NLS-1$
+ return InetAddress.ANY;
+ }
+
+ if (isHostName(host)) {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
security.checkConnect(host, -1);
}
- return lookupHostByName(host);
- }
-
- return createHostNameFromIPAddress(host);
- }
-
- /**
- * Answer the dotted string IP address representing this address.
- *
- * @return String the corresponding dotted string IP address
- */
- public String getHostAddress() {
-
- return inetNtoaImpl(bytesToInt(ipaddress, 0));
- }
-
- /**
- * Answer the host name.
- *
- * @return String the corresponding string name
- */
- public String getHostName() {
- try {
- if (hostName == null) {
- int address = 0;
- if (ipaddress.length == 4) {
- address = bytesToInt(ipaddress, 0);
- if (address == 0) {
- return hostName = inetNtoaImpl(address);
- }
- }
- hostName = getHostByAddrImpl(ipaddress).hostName;
- if (hostName.equals("localhost") && ipaddress.length == 4
- && address != 0x7f000001) {
- return hostName = inetNtoaImpl(address);
- }
- }
- } catch (UnknownHostException e) {
- return hostName = Inet6Util
- .createIPAddrStringFromByteArray(ipaddress);
- }
- SecurityManager security = System.getSecurityManager();
- try {
- // Only check host names, not addresses
- if (security != null && isHostName(hostName)) {
+ return lookupHostByName(host);
+ }
+
+ return createHostNameFromIPAddress(host);
+ }
+
+ /**
+ * Answer the dotted string IP address representing this address.
+ *
+ * @return String the corresponding dotted string IP address
+ */
+ public String getHostAddress() {
+ return inetNtoaImpl(bytesToInt(ipaddress, 0));
+ }
+
+ /**
+ * Answer the host name.
+ *
+ * @return String the corresponding string name
+ */
+ public String getHostName() {
+ try {
+ if (hostName == null) {
+ int address = 0;
+ if (ipaddress.length == 4) {
+ address = bytesToInt(ipaddress, 0);
+ if (address == 0) {
+ return hostName = inetNtoaImpl(address);
+ }
+ }
+ hostName = getHostByAddrImpl(ipaddress).hostName;
+ if (hostName.equals("localhost") && ipaddress.length == 4 //$NON-NLS-1$
+ && address != 0x7f000001) {
+ return hostName = inetNtoaImpl(address);
+ }
+ }
+ } catch (UnknownHostException e) {
+ return hostName = Inet6Util
+ .createIPAddrStringFromByteArray(ipaddress);
+ }
+ SecurityManager security = System.getSecurityManager();
+ try {
+ // Only check host names, not addresses
+ if (security != null && isHostName(hostName)) {
security.checkConnect(hostName, -1);
}
- } catch (SecurityException e) {
- return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
- }
- return hostName;
-}
-
- /**
- * Answers canonical name for the host associated with the internet address
- *
- * @return String string containing the host name
- */
- public String getCanonicalHostName() {
- String canonicalName;
- try {
- int address = 0;
- if (ipaddress.length == 4) {
- address = bytesToInt(ipaddress, 0);
- if (address == 0) {
- return inetNtoaImpl(address);
- }
- }
- canonicalName = getHostByAddrImpl(ipaddress).hostName;
- } catch (UnknownHostException e) {
- return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
- }
- SecurityManager security = System.getSecurityManager();
- try {
- // Only check host names, not addresses
- if (security != null && isHostName(canonicalName)) {
+ } catch (SecurityException e) {
+ return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
+ }
+ return hostName;
+ }
+
+ /**
+ * Answers canonical name for the host associated with the internet address
+ *
+ * @return String string containing the host name
+ */
+ public String getCanonicalHostName() {
+ String canonicalName;
+ try {
+ int address = 0;
+ if (ipaddress.length == 4) {
+ address = bytesToInt(ipaddress, 0);
+ if (address == 0) {
+ return inetNtoaImpl(address);
+ }
+ }
+ canonicalName = getHostByAddrImpl(ipaddress).hostName;
+ } catch (UnknownHostException e) {
+ return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
+ }
+ SecurityManager security = System.getSecurityManager();
+ try {
+ // Only check host names, not addresses
+ if (security != null && isHostName(canonicalName)) {
security.checkConnect(canonicalName, -1);
}
- } catch (SecurityException e) {
- return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
- }
- return canonicalName;
-}
-
- /**
- * Answer the local host, if allowed by the security policy. Otherwise,
- * answer the loopback address which allows this machine to be contacted.
- *
- * @return InetAddress the InetAddress representing the local host
- * @throws UnknownHostException
- * if the address lookup fails
- */
- public static InetAddress getLocalHost() throws UnknownHostException {
- String host = getHostNameImpl();
- SecurityManager security = System.getSecurityManager();
- try {
- if (security != null) {
+ } catch (SecurityException e) {
+ return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
+ }
+ return canonicalName;
+ }
+
+ /**
+ * Answer the local host, if allowed by the security policy. Otherwise,
+ * answer the loopback address which allows this machine to be contacted.
+ *
+ * @return InetAddress the InetAddress representing the local host
+ * @throws UnknownHostException
+ * if the address lookup fails
+ */
+ public static InetAddress getLocalHost() throws UnknownHostException {
+ String host = getHostNameImpl();
+ SecurityManager security = System.getSecurityManager();
+ try {
+ if (security != null) {
security.checkConnect(host, -1);
}
- } catch (SecurityException e) {
- return InetAddress.LOOPBACK;
- }
- return lookupHostByName(host);
- }
-
- /**
- * Answer a hashcode for this IP address.
- *
- * @return int the hashcode
- */
- @Override
+ } catch (SecurityException e) {
+ return InetAddress.LOOPBACK;
+ }
+ return lookupHostByName(host);
+ }
+
+ /**
+ * Answer a hashcode for this IP address.
+ *
+ * @return int the hashcode
+ */
+ @Override
public int hashCode() {
- return bytesToInt(ipaddress, 0);
- }
+ return bytesToInt(ipaddress, 0);
+ }
+
+ /**
+ * Answer true if the InetAddress is an IP multicast address.
+ *
+ * @return boolean true, if the address is in the multicast group
+ */
+ public boolean isMulticastAddress() {
+ return ((ipaddress[0] & 255) >>> 4) == 0xE;
+ }
- /**
- * Answer true if the InetAddress is an IP multicast address.
- *
- * @return boolean true, if the address is in the multicast group
- */
- public boolean isMulticastAddress() {
- return ((ipaddress[0] & 255) >>> 4) == 0xE;
- }
-
- static synchronized InetAddress lookupHostByName(String host)
- throws UnknownHostException {
- int ttl = -1;
-
- String ttlValue = AccessController.doPrivileged(
- new PriviAction<String>("networkaddress.cache.ttl"));
- try {
- if (ttlValue != null) {
+ static synchronized InetAddress lookupHostByName(String host)
+ throws UnknownHostException {
+ int ttl = -1;
+
+ String ttlValue = AccessController
+ .doPrivileged(new PriviAction<String>(
+ "networkaddress.cache.ttl")); //$NON-NLS-1$
+ try {
+ if (ttlValue != null) {
ttl = Integer.decode(ttlValue).intValue();
}
- } catch (NumberFormatException e) {
- }
- CacheElement element = null;
- if (ttl == 0) {
+ } catch (NumberFormatException e) {
+ // Ignored
+ }
+ CacheElement element = null;
+ if (ttl == 0) {
Cache.clear();
} else {
- element = Cache.get(host);
- if (element != null
- && ttl > 0
- && element.timeAdded + (ttl * 1000) < System
- .currentTimeMillis()) {
+ element = Cache.get(host);
+ if (element != null
+ && ttl > 0
+ && element.timeAdded + (ttl * 1000) < System
+ .currentTimeMillis()) {
element = null;
}
- }
- if (element != null) {
+ }
+ if (element != null) {
return element.inetAddress();
}
-
- // now try the negative cache
- String failedMessage = NegativeCache.getFailedMessage(host);
- if (failedMessage != null) {
- throw new UnknownHostException(host + " - " + failedMessage);
- }
-
- InetAddress anInetAddress;
- try {
- anInetAddress = getHostByNameImpl(host, preferIPv6Addresses());
- } catch (UnknownHostException e) {
- // put the entry in the negative cache
- NegativeCache.put(host, e.getMessage());
- throw new UnknownHostException(host + " - " + e.getMessage());
- }
-
- Cache.add(anInetAddress);
- return anInetAddress;
- }
-
- /**
- * Query the IP stack for aliases for the host. The host is in string name
- * form.
- *
- * @param name
- * the host name to lookup
- * @throws UnknownHostException
- * if an error occurs during lookup
- */
- static native InetAddress[] getAliasesByNameImpl(String name)
- throws UnknownHostException;
-
- /**
- * Query the IP stack for the host address. The host is in address form.
- *
- * @param addr
- * the host address to lookup
- * @throws UnknownHostException
- * if an error occurs during lookup
- */
- static native InetAddress getHostByAddrImpl(byte[] addr)
- throws UnknownHostException;
-
- static int inetAddr(String host) throws UnknownHostException {
- return (host.equals("255.255.255.255")) ? 0xFFFFFFFF
- : inetAddrImpl(host);
- }
-
- /**
- * Convert a string containing an Ipv4 Internet Protocol dotted address into
- * a binary address. Note, the special case of '255.255.255.255' throws an
- * exception, so this value should not be used as an argument. See also
- * inetAddr(String).
- */
- static native int inetAddrImpl(String host) throws UnknownHostException;
-
- /**
- * Convert a binary address into a string containing an Ipv4 Internet
- * Protocol dotted address.
- */
- static native String inetNtoaImpl(int hipAddr);
-
- /**
- * Query the IP stack for the host address. The host is in string name form.
- *
- * @param name
- * the host name to lookup
- * @param preferIPv6Addresses
- * address preference if underlying platform is V4/V6
- * @return InetAddress the host address
- *
- * @throws UnknownHostException
- * if an error occurs during lookup
- */
- static native InetAddress getHostByNameImpl(String name,
- boolean preferIPv6Address) throws UnknownHostException;
-
- /**
- * Query the IP stack for the host machine name.
- *
- * @return String the host machine name
- */
- static native String getHostNameImpl();
- static String getHostNameInternal(String host) throws UnknownHostException {
- if (host == null || 0 == host.length()) {
+ // now try the negative cache
+ String failedMessage = NegativeCache.getFailedMessage(host);
+ if (failedMessage != null) {
+ throw new UnknownHostException(host + " - " + failedMessage); //$NON-NLS-1$
+ }
+
+ InetAddress anInetAddress;
+ try {
+ anInetAddress = getHostByNameImpl(host, preferIPv6Addresses());
+ } catch (UnknownHostException e) {
+ // put the entry in the negative cache
+ NegativeCache.put(host, e.getMessage());
+ throw new UnknownHostException(host + " - " + e.getMessage()); //$NON-NLS-1$
+ }
+
+ Cache.add(anInetAddress);
+ return anInetAddress;
+ }
+
+ /**
+ * Query the IP stack for aliases for the host. The host is in string name
+ * form.
+ *
+ * @param name
+ * the host name to lookup
+ * @throws UnknownHostException
+ * if an error occurs during lookup
+ */
+ static native InetAddress[] getAliasesByNameImpl(String name)
+ throws UnknownHostException;
+
+ /**
+ * Query the IP stack for the host address. The host is in address form.
+ *
+ * @param addr
+ * the host address to lookup
+ * @throws UnknownHostException
+ * if an error occurs during lookup
+ */
+ static native InetAddress getHostByAddrImpl(byte[] addr)
+ throws UnknownHostException;
+
+ static int inetAddr(String host) throws UnknownHostException {
+ return (host.equals("255.255.255.255")) ? 0xFFFFFFFF //$NON-NLS-1$
+ : inetAddrImpl(host);
+ }
+
+ /**
+ * Convert a string containing an Ipv4 Internet Protocol dotted address into
+ * a binary address. Note, the special case of '255.255.255.255' throws an
+ * exception, so this value should not be used as an argument. See also
+ * inetAddr(String).
+ */
+ static native int inetAddrImpl(String host) throws UnknownHostException;
+
+ /**
+ * Convert a binary address into a string containing an Ipv4 Internet
+ * Protocol dotted address.
+ */
+ static native String inetNtoaImpl(int hipAddr);
+
+ /**
+ * Query the IP stack for the host address. The host is in string name form.
+ *
+ * @param name
+ * the host name to lookup
+ * @param preferIPv6Addresses
+ * address preference if underlying platform is V4/V6
+ * @return InetAddress the host address
+ *
+ * @throws UnknownHostException
+ * if an error occurs during lookup
+ */
+ static native InetAddress getHostByNameImpl(String name,
+ boolean preferIPv6Address) throws UnknownHostException;
+
+ /**
+ * Query the IP stack for the host machine name.
+ *
+ * @return String the host machine name
+ */
+ static native String getHostNameImpl();
+
+ static String getHostNameInternal(String host) throws UnknownHostException {
+ if (host == null || 0 == host.length()) {
return InetAddress.LOOPBACK.getHostAddress();
}
- if (isHostName(host)) {
+ if (isHostName(host)) {
return lookupHostByName(host).getHostAddress();
}
- return host;
- }
+ return host;
+ }
- /**
- * Answers a string containing a concise, human-readable description of the
- * address.
- *
- * @return String the description, as host/address
- */
- @Override
+ /**
+ * Answers a string containing a concise, human-readable description of the
+ * address.
+ *
+ * @return String the description, as host/address
+ */
+ @Override
public String toString() {
- return (hostName == null ? "" : hostName) + "/" + getHostAddress();
- }
+ return (hostName == null ? "" : hostName) + "/" + getHostAddress(); //$NON-NLS-1$ //$NON-NLS-2$
+ }
- class CacheElement {
- long timeAdded = System.currentTimeMillis();
+ class CacheElement {
+ long timeAdded = System.currentTimeMillis();
- CacheElement next;
+ CacheElement next;
- public CacheElement() {
- super();
- }
+ public CacheElement() {
+ super();
+ }
- String hostName() {
- return hostName;
- }
+ String hostName() {
+ return hostName;
+ }
- InetAddress inetAddress() {
- return InetAddress.this;
- }
- }
+ InetAddress inetAddress() {
+ return InetAddress.this;
+ }
+ }
- static class Cache {
- static int maxSize = 5;
+ static class Cache {
+ static int maxSize = 5;
- private static int size = 0;
+ private static int size = 0;
- private static CacheElement head;
+ private static CacheElement head;
- static void clear() {
- size = 0;
- head = null;
- }
+ static void clear() {
+ size = 0;
+ head = null;
+ }
- static void add(InetAddress value) {
- CacheElement newElement = value.cacheElement();
- if (size < maxSize) {
+ static void add(InetAddress value) {
+ CacheElement newElement = value.cacheElement();
+ if (size < maxSize) {
size++;
} else {
deleteTail();
}
- newElement.next = head; // If the head is null, this does no harm.
- head = newElement;
- }
-
- static CacheElement get(String name) {
- CacheElement previous = null;
- CacheElement current = head;
- boolean notFound = true;
- while ((null != current)
- && (notFound = !(name.equals(current.hostName())))) {
- previous = current;
- current = current.next;
- }
- if (notFound) {
+ newElement.next = head; // If the head is null, this does no harm.
+ head = newElement;
+ }
+
+ static CacheElement get(String name) {
+ CacheElement previous = null;
+ CacheElement current = head;
+ boolean notFound = true;
+ while ((null != current)
+ && (notFound = !(name.equals(current.hostName())))) {
+ previous = current;
+ current = current.next;
+ }
+ if (notFound) {
return null;
}
- moveToHead(current, previous);
- return current;
- }
+ moveToHead(current, previous);
+ return current;
+ }
- private static void deleteTail() {
- if (0 == size) {
+ private static void deleteTail() {
+ if (0 == size) {
return;
}
- if (1 == size) {
+ if (1 == size) {
head = null;
}
- CacheElement previous = null;
- CacheElement current = head;
- while (null != current.next) {
- previous = current;
- current = current.next;
- }
- previous.next = null;
- }
-
- private static void moveToHead(CacheElement element,
- CacheElement elementPredecessor) {
- if (null == elementPredecessor) {
- head = element;
- } else {
- elementPredecessor.next = element.next;
- element.next = head;
- head = element;
- }
- }
- }
-
- /**
- * Answer true if the string is a host name, false if it is an IP Address.
- */
-
- private static boolean isHostName(String value) {
- return !(Inet6Util.isValidIPV4Address(value) || Inet6Util
- .isValidIP6Address(value));
- }
-
- /**
- * Answer true if the address is a loop back address. Valid IPv4 loopback
- * addresses are 127.d.d.d Valid IPv6 loopback address is ::1
- *
- * @return boolean
- */
- public boolean isLoopbackAddress() {
- return false;
- }
-
- /**
- * Answers true if the address is a link local address.
- *
- * Valid IPv6 link local addresses are FE80::0 through to
- * FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
- *
- * There are no valid IPv4 link local addresses.
- *
- * @return boolean
- */
- public boolean isLinkLocalAddress() {
- return false;
- }
-
- /**
- * Answers true if the address is a site local address.
- *
- * Valid IPv6 link local addresses are FEC0::0 through to
- * FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
- *
- * There are no valid IPv4 site local addresses.
- *
- * @return boolean
- */
- public boolean isSiteLocalAddress() {
- return false;
- }
-
- /**
- * Answers true if the address is a global multicast address.
- *
- * Valid IPv6 link global multicast addresses are FFxE:/112 where x is a set
- * of flags, and the additional 112 bits make up the global multicast
- * address space
- *
- * Valid IPv4 global multicast addresses are between: 224.0.1.0 to
- * 238.255.255.255
- *
- * @return boolean
- */
- public boolean isMCGlobal() {
- return false;
- }
-
- /**
- * Answers true if the address is a node local multicast address.
- *
- * Valid IPv6 node local multicast addresses are FFx1:/112 where x is a set
- * of flags, and the additional 112 bits make up the node local multicast
- * address space
- *
- * There are no valid IPv4 node local multicast addresses.
- *
- * @return boolean
- */
- public boolean isMCNodeLocal() {
- return false;
- }
-
- /**
- * Answers true if the address is a link local multicast address.
- *
- * Valid IPv6 link local multicast addresses are FFx2:/112 where x is a set
- * of flags, and the additional 112 bits make up the node local multicast
- * address space
- *
- * Valid IPv4 link-local addresses are between: 224.0.0.0 to 224.0.0.255
- *
- * @return boolean
- */
- public boolean isMCLinkLocal() {
- return false;
- }
-
- /**
- * Answers true if the address is a site local multicast address.
- *
- * Valid IPv6 site local multicast addresses are FFx5:/112 where x is a set
- * of flags, and the additional 112 bits make up the node local multicast
- * address space
- *
- * Valid IPv4 site-local addresses are between: 239.252.0.0 to
- * 239.255.255.255
- *
- * @return boolean
- */
- public boolean isMCSiteLocal() {
- return false;
- }
-
- /**
- * Answers true if the address is a organization local multicast address.
- *
- * Valid IPv6 organization local multicast addresses are FFx8:/112 where x
- * is a set of flags, and the additional 112 bits make up the node local
- * multicast address space
- *
- * Valid IPv4 organization-local addresses are between: 239.192.0.0 to
- * 239.251.255.255
- *
- * @return boolean
- */
- public boolean isMCOrgLocal() {
- return false;
- }
-
- /**
- * Method isAnyLocalAddress.
- *
- * @return boolean
- */
- public boolean isAnyLocalAddress() {
- return false;
- }
-
-
+ CacheElement previous = null;
+ CacheElement current = head;
+ while (null != current.next) {
+ previous = current;
+ current = current.next;
+ }
+ previous.next = null;
+ }
+
+ private static void moveToHead(CacheElement element,
+ CacheElement elementPredecessor) {
+ if (null == elementPredecessor) {
+ head = element;
+ } else {
+ elementPredecessor.next = element.next;
+ element.next = head;
+ head = element;
+ }
+ }
+ }
+
+ /**
+ * Answer true if the string is a host name, false if it is an IP Address.
+ */
+ private static boolean isHostName(String value) {
+ return !(Inet6Util.isValidIPV4Address(value) || Inet6Util
+ .isValidIP6Address(value));
+ }
+
+ /**
+ * Answer true if the address is a loop back address. Valid IPv4 loopback
+ * addresses are 127.d.d.d Valid IPv6 loopback address is ::1
+ *
+ * @return boolean
+ */
+ public boolean isLoopbackAddress() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a link local address.
+ *
+ * Valid IPv6 link local addresses are FE80::0 through to
+ * FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
+ *
+ * There are no valid IPv4 link local addresses.
+ *
+ * @return boolean
+ */
+ public boolean isLinkLocalAddress() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a site local address.
+ *
+ * Valid IPv6 link local addresses are FEC0::0 through to
+ * FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
+ *
+ * There are no valid IPv4 site local addresses.
+ *
+ * @return boolean
+ */
+ public boolean isSiteLocalAddress() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a global multicast address.
+ *
+ * Valid IPv6 link global multicast addresses are FFxE:/112 where x is a set
+ * of flags, and the additional 112 bits make up the global multicast
+ * address space
+ *
+ * Valid IPv4 global multicast addresses are between: 224.0.1.0 to
+ * 238.255.255.255
+ *
+ * @return boolean
+ */
+ public boolean isMCGlobal() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a node local multicast address.
+ *
+ * Valid IPv6 node local multicast addresses are FFx1:/112 where x is a set
+ * of flags, and the additional 112 bits make up the node local multicast
+ * address space
+ *
+ * There are no valid IPv4 node local multicast addresses.
+ *
+ * @return boolean
+ */
+ public boolean isMCNodeLocal() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a link local multicast address.
+ *
+ * Valid IPv6 link local multicast addresses are FFx2:/112 where x is a set
+ * of flags, and the additional 112 bits make up the node local multicast
+ * address space
+ *
+ * Valid IPv4 link-local addresses are between: 224.0.0.0 to 224.0.0.255
+ *
+ * @return boolean
+ */
+ public boolean isMCLinkLocal() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a site local multicast address.
+ *
+ * Valid IPv6 site local multicast addresses are FFx5:/112 where x is a set
+ * of flags, and the additional 112 bits make up the node local multicast
+ * address space
+ *
+ * Valid IPv4 site-local addresses are between: 239.252.0.0 to
+ * 239.255.255.255
+ *
+ * @return boolean
+ */
+ public boolean isMCSiteLocal() {
+ return false;
+ }
+
+ /**
+ * Answers true if the address is a organization local multicast address.
+ *
+ * Valid IPv6 organization local multicast addresses are FFx8:/112 where x
+ * is a set of flags, and the additional 112 bits make up the node local
+ * multicast address space
+ *
+ * Valid IPv4 organization-local addresses are between: 239.192.0.0 to
+ * 239.251.255.255
+ *
+ * @return boolean
+ */
+ public boolean isMCOrgLocal() {
+ return false;
+ }
+
+ /**
+ * Method isAnyLocalAddress.
+ *
+ * @return boolean
+ */
+ public boolean isAnyLocalAddress() {
+ return false;
+ }
+
/**
* Tries to see if the InetAddress is reachable. This method first tries to
* use ICMP(ICMP ECHO REQUEST). When first step fails, the TCP connection on
@@ -782,7 +783,7 @@
public boolean isReachable(NetworkInterface netif, final int ttl,
final int timeout) throws IOException {
if (0 > ttl || 0 > timeout) {
- throw new IllegalArgumentException(Msg.getString("K0051"));
+ throw new IllegalArgumentException(Msg.getString("K0051")); //$NON-NLS-1$
}
boolean reachable = false;
if (null == netif) {
@@ -792,10 +793,10 @@
reachable = isReachableByTCP(this, null, timeout);
}
} else {
- //Not Bind to any address
+ // Not Bind to any address
if (null == netif.addresses) {
return false;
- }
+ }
// binds to all address on this NetworkInterface, tries ICMP ping
// first
reachable = isReachableByICMPUseMultiThread(netif, ttl, timeout);
@@ -808,7 +809,7 @@
}
/*
- * uses multi-Thread to try if isReachable, returns true if any of threads
+ * Uses multi-Thread to try if isReachable, returns true if any of threads
* returns in time
*/
private boolean isReachableByMultiThread(NetworkInterface netif,
@@ -823,26 +824,26 @@
boolean needWait = false;
while (addresses.hasMoreElements()) {
final InetAddress addr = addresses.nextElement();
-
- //loopback interface can only reach to local addresses
- if(addr.isLoopbackAddress())
- {
- Enumeration<NetworkInterface> NetworkInterfaces = NetworkInterface.getNetworkInterfaces();
- while(NetworkInterfaces.hasMoreElements())
- {
- NetworkInterface networkInterface = NetworkInterfaces.nextElement();
- Enumeration<InetAddress> localAddresses = networkInterface.getInetAddresses();
- while(localAddresses.hasMoreElements())
- {
- if(InetAddress.this.equals(localAddresses.nextElement()))
- {
+
+ // loopback interface can only reach to local addresses
+ if (addr.isLoopbackAddress()) {
+ Enumeration<NetworkInterface> NetworkInterfaces = NetworkInterface
+ .getNetworkInterfaces();
+ while (NetworkInterfaces.hasMoreElements()) {
+ NetworkInterface networkInterface = NetworkInterfaces
+ .nextElement();
+ Enumeration<InetAddress> localAddresses = networkInterface
+ .getInetAddresses();
+ while (localAddresses.hasMoreElements()) {
+ if (InetAddress.this.equals(localAddresses
+ .nextElement())) {
return true;
}
- }
+ }
}
continue;
}
-
+
needWait = true;
new Thread() {
@Override
@@ -854,8 +855,8 @@
InetAddress.this, addr, ttl, timeout);
} else {
try {
- threadReached = isReachableByTCP(
- InetAddress.this, addr, timeout);
+ threadReached = isReachableByTCP(InetAddress.this,
+ addr, timeout);
} catch (IOException e) {
// do nothing
}
@@ -879,7 +880,7 @@
}
}.start();
}
-
+
if (needWait) {
synchronized (waitReachable) {
try {
@@ -891,19 +892,20 @@
return reached;
}
}
- else {
- return false;
- }
- }
- private boolean isReachableByICMPUseMultiThread(NetworkInterface netif, int ttl, int timeout) throws IOException{
+ return false;
+ }
+
+ private boolean isReachableByICMPUseMultiThread(NetworkInterface netif,
+ int ttl, int timeout) throws IOException {
return isReachableByMultiThread(netif, ttl, timeout, true);
}
-
- private boolean isReachableByTCPUseMultiThread(NetworkInterface netif, int ttl, int timeout) throws IOException{
+
+ private boolean isReachableByTCPUseMultiThread(NetworkInterface netif,
+ int ttl, int timeout) throws IOException {
return isReachableByMultiThread(netif, ttl, timeout, false);
- }
-
+ }
+
private boolean isReachableByTCP(InetAddress dest, InetAddress source,
int timeout) throws IOException {
FileDescriptor fd = new FileDescriptor();
@@ -915,7 +917,8 @@
if (null != source) {
NETIMPL.bind(fd, 0, source);
}
- NETIMPL.connectStreamWithTimeoutSocket(fd, 7, timeout, traffic, dest);
+ NETIMPL.connectStreamWithTimeoutSocket(fd, 7, timeout, traffic,
+ dest);
reached = true;
} catch (IOException e) {
if (ERRMSG_CONNECTION_REFUSED.equals(e.getMessage())) {
@@ -923,414 +926,418 @@
reached = true;
}
}
-
+
NETIMPL.socketClose(fd);
-
+
return reached;
}
- /**
- * Answers the InetAddress corresponding to the array of bytes. In the case
- * of an IPv4 address there must be exactly 4 bytes and for IPv6 exactly 16
- * bytes. If not, an UnknownHostException is thrown.
- *
- * The IP address is not validated by a name service.
- *
- * The high order byte is <code>ipAddress[0]</code>.
- *
- * @param ipAddress either a 4 (IPv4) or 16 (IPv6) byte array
- * @return the InetAddress
- *
- * @throws UnknownHostException
- */
- public static InetAddress getByAddress(byte[] ipAddress)
- throws UnknownHostException {
- // simply call the method by the same name specifying the default scope
- // id of 0
- return getByAddress(ipAddress, 0);
- }
-
- /**
- * Answers the InetAddress corresponding to the array of bytes. In the case
- * of an IPv4 address there must be exactly 4 bytes and for IPv6 exactly 16
- * bytes. If not, an UnknownHostException is thrown.
- *
- * The IP address is not validated by a name service.
- *
- * The high order byte is <code>ipAddress[0]</code>.
- *
- * @param ipAddress either a 4 (IPv4) or 16 (IPv6) byte array
- * @param scope_id the scope id for an IPV6 scoped address. If not a scoped
- * address just pass in 0
- * @return the InetAddress
- *
- * @throws UnknownHostException
- */
- static InetAddress getByAddress(byte[] ipAddress, int scope_id)
- throws UnknownHostException {
- byte[] copy_address;
- if (ipAddress != null && ipAddress.length == 4) {
- copy_address = new byte[4];
- for (int i = 0; i < 4; i++) {
- copy_address[i] = ipAddress[i];
- }
- return new Inet4Address(ipAddress);
- }
-
- if (ipAddress != null && ipAddress.length == 16) {
- // First check to see if the address is an IPv6-mapped
- // IPv4 address. If it is, then we can make it a IPv4
- // address, otherwise, we'll create an IPv6 address.
- if (isIPv4MappedAddress(ipAddress)) {
- copy_address = new byte[4];
- for (int i = 0; i < 4; i++) {
- copy_address[i] = ipAddress[12 + i];
- }
- return new Inet4Address(copy_address);
- }
- copy_address = ipAddress.clone();
- return new Inet6Address(copy_address, scope_id);
- }
- throw new UnknownHostException(Msg.getString("K0339"));
- }
-
- private static boolean isIPv4MappedAddress(byte ipAddress[]) {
-
- // Check if the address matches ::FFFF:d.d.d.d
- // The first 10 bytes are 0. The next to are -1 (FF).
- // The last 4 bytes are varied.
- for (int i = 0; i < 10; i++) {
- if (ipAddress[i] != 0) {
- return false;
- }
- }
-
- if (ipAddress[10] != -1 || ipAddress[11] != -1) {
- return false;
- }
-
- return true;
-
- }
-
- /**
- * Answers the InetAddress corresponding to the array of bytes, and the
- * given hostname. In the case of an IPv4 address there must be exactly 4
- * bytes and for IPv6 exactly 16 bytes. If not, an UnknownHostException is
- * thrown.
- *
- * The host name and IP address are not validated.
- *
- * The hostname either be a machine alias or a valid IPv6 or IPv4 address
- * format.
- *
- * The high order byte is <code>ipAddress[0]</code>.
- *
- * @param hostName string representation of hostname or ip address
- * @param ipAddress either a 4 (IPv4) or 16 (IPv6) byte array
- * @return the InetAddress
- *
- * @throws UnknownHostException
- */
- public static InetAddress getByAddress(String hostName, byte[] ipAddress)
- throws UnknownHostException {
- // just call the method by the same name passing in a default scope id
- // of 0
- return getByAddressInternal(hostName, ipAddress, 0);
- }
-
- /**
- * Answers the InetAddress corresponding to the array of bytes, and the
- * given hostname. In the case of an IPv4 address there must be exactly 4
- * bytes and for IPv6 exactly 16 bytes. If not, an UnknownHostException is
- * thrown.
- *
- * The host name and IP address are not validated.
- *
- * The hostname either be a machine alias or a valid IPv6 or IPv4 address
- * format.
- *
- * The high order byte is <code>ipAddress[0]</code>.
- *
- * @param hostName string representation of hostname or IP address
- * @param ipAddress either a 4 (IPv4) or 16 (IPv6) byte array
- * @param scope_id the scope id for a scoped address. If not a scoped address just pass
- * in 0
- * @return the InetAddress
- *
- * @throws UnknownHostException
- */
- static InetAddress getByAddressInternal(String hostName, byte[] ipAddress,
- int scope_id) throws UnknownHostException {
- byte[] copy_address;
- if (ipAddress != null && ipAddress.length == 4) {
- copy_address = new byte[4];
- for (int i = 0; i < 4; i++) {
- copy_address[i] = ipAddress[i];
- }
- return new Inet4Address(ipAddress, hostName);
- }
-
- if (ipAddress != null && ipAddress.length == 16) {
- // First check to see if the address is an IPv6-mapped
- // IPv4 address. If it is, then we can make it a IPv4
- // address, otherwise, we'll create an IPv6 address.
- if (isIPv4MappedAddress(ipAddress)) {
- copy_address = new byte[4];
- for (int i = 0; i < 4; i++) {
- copy_address[i] = ipAddress[12 + i];
- }
- return new Inet4Address(ipAddress, hostName);
- }
-
- copy_address = new byte[16];
- for (int i = 0; i < 16; i++) {
- copy_address[i] = ipAddress[i];
- }
-
- return new Inet6Address(ipAddress, hostName, scope_id);
- }
-
- throw new UnknownHostException(Msg.getString("K0332", hostName));
- }
-
- /**
- * Takes the integer and chops it into 4 bytes, putting it into the byte
- * array starting with the high order byte at the index start. This method
- * makes no checks on the validity of the parameters.
- */
- static void intToBytes(int value, byte bytes[], int start) {
- // Shift the int so the current byte is right-most
- // Use a byte mask of 255 to single out the last byte.
- bytes[start] = (byte) ((value >> 24) & 255);
- bytes[start + 1] = (byte) ((value >> 16) & 255);
- bytes[start + 2] = (byte) ((value >> 8) & 255);
- bytes[start + 3] = (byte) (value & 255);
- }
-
- /**
- * Takes the byte array and creates an integer out of four bytes starting at
- * start as the high-order byte. This method makes no checks on the validity
- * of the parameters.
- */
- static int bytesToInt(byte bytes[], int start) {
- // First mask the byte with 255, as when a negative
- // signed byte converts to an integer, it has bits
- // on in the first 3 bytes, we are only concerned
- // about the right-most 8 bits.
- // Then shift the rightmost byte to align with its
- // position in the integer.
- int value = ((bytes[start + 3] & 255))
- | ((bytes[start + 2] & 255) << 8)
- | ((bytes[start + 1] & 255) << 16)
- | ((bytes[start] & 255) << 24);
- return value;
- }
-
- /**
- * Creates an InetAddress based on an ipAddressString. No error handling is
- * performed here.
- */
- static InetAddress createHostNameFromIPAddress(String ipAddressString)
- throws UnknownHostException {
+ /**
+ * Answers the InetAddress corresponding to the array of bytes. In the case
+ * of an IPv4 address there must be exactly 4 bytes and for IPv6 exactly 16
+ * bytes. If not, an UnknownHostException is thrown.
+ *
+ * The IP address is not validated by a name service.
+ *
+ * The high order byte is <code>ipAddress[0]</code>.
+ *
+ * @param ipAddress
+ * either a 4 (IPv4) or 16 (IPv6) byte array
+ * @return the InetAddress
+ *
+ * @throws UnknownHostException
+ */
+ public static InetAddress getByAddress(byte[] ipAddress)
+ throws UnknownHostException {
+ // simply call the method by the same name specifying the default scope
+ // id of 0
+ return getByAddress(ipAddress, 0);
+ }
+
+ /**
+ * Answers the InetAddress corresponding to the array of bytes. In the case
+ * of an IPv4 address there must be exactly 4 bytes and for IPv6 exactly 16
+ * bytes. If not, an UnknownHostException is thrown.
+ *
+ * The IP address is not validated by a name service.
+ *
+ * The high order byte is <code>ipAddress[0]</code>.
+ *
+ * @param ipAddress
+ * either a 4 (IPv4) or 16 (IPv6) byte array
+ * @param scope_id
+ * the scope id for an IPV6 scoped address. If not a scoped
+ * address just pass in 0
+ * @return the InetAddress
+ *
+ * @throws UnknownHostException
+ */
+ static InetAddress getByAddress(byte[] ipAddress, int scope_id)
+ throws UnknownHostException {
+ byte[] copy_address;
+ if (ipAddress != null && ipAddress.length == 4) {
+ copy_address = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ copy_address[i] = ipAddress[i];
+ }
+ return new Inet4Address(ipAddress);
+ }
+
+ if (ipAddress != null && ipAddress.length == 16) {
+ // First check to see if the address is an IPv6-mapped
+ // IPv4 address. If it is, then we can make it a IPv4
+ // address, otherwise, we'll create an IPv6 address.
+ if (isIPv4MappedAddress(ipAddress)) {
+ copy_address = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ copy_address[i] = ipAddress[12 + i];
+ }
+ return new Inet4Address(copy_address);
+ }
+ copy_address = ipAddress.clone();
+ return new Inet6Address(copy_address, scope_id);
+ }
+ throw new UnknownHostException(Msg.getString("K0339")); //$NON-NLS-1$
+ }
+
+ private static boolean isIPv4MappedAddress(byte ipAddress[]) {
+ // Check if the address matches ::FFFF:d.d.d.d
+ // The first 10 bytes are 0. The next to are -1 (FF).
+ // The last 4 bytes are varied.
+ for (int i = 0; i < 10; i++) {
+ if (ipAddress[i] != 0) {
+ return false;
+ }
+ }
+
+ if (ipAddress[10] != -1 || ipAddress[11] != -1) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Answers the InetAddress corresponding to the array of bytes, and the
+ * given hostname. In the case of an IPv4 address there must be exactly 4
+ * bytes and for IPv6 exactly 16 bytes. If not, an UnknownHostException is
+ * thrown.
+ *
+ * The host name and IP address are not validated.
+ *
+ * The hostname either be a machine alias or a valid IPv6 or IPv4 address
+ * format.
+ *
+ * The high order byte is <code>ipAddress[0]</code>.
+ *
+ * @param hostName
+ * string representation of hostname or ip address
+ * @param ipAddress
+ * either a 4 (IPv4) or 16 (IPv6) byte array
+ * @return the InetAddress
+ *
+ * @throws UnknownHostException
+ */
+ public static InetAddress getByAddress(String hostName, byte[] ipAddress)
+ throws UnknownHostException {
+ // just call the method by the same name passing in a default scope id
+ // of 0
+ return getByAddressInternal(hostName, ipAddress, 0);
+ }
+
+ /**
+ * Answers the InetAddress corresponding to the array of bytes, and the
+ * given hostname. In the case of an IPv4 address there must be exactly 4
+ * bytes and for IPv6 exactly 16 bytes. If not, an UnknownHostException is
+ * thrown.
+ *
+ * The host name and IP address are not validated.
+ *
+ * The hostname either be a machine alias or a valid IPv6 or IPv4 address
+ * format.
+ *
+ * The high order byte is <code>ipAddress[0]</code>.
+ *
+ * @param hostName
+ * string representation of hostname or IP address
+ * @param ipAddress
+ * either a 4 (IPv4) or 16 (IPv6) byte array
+ * @param scope_id
+ * the scope id for a scoped address. If not a scoped address
+ * just pass in 0
+ * @return the InetAddress
+ *
+ * @throws UnknownHostException
+ */
+ static InetAddress getByAddressInternal(String hostName, byte[] ipAddress,
+ int scope_id) throws UnknownHostException {
+ byte[] copy_address;
+ if (ipAddress != null && ipAddress.length == 4) {
+ copy_address = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ copy_address[i] = ipAddress[i];
+ }
+ return new Inet4Address(ipAddress, hostName);
+ }
+
+ if (ipAddress != null && ipAddress.length == 16) {
+ // First check to see if the address is an IPv6-mapped
+ // IPv4 address. If it is, then we can make it a IPv4
+ // address, otherwise, we'll create an IPv6 address.
+ if (isIPv4MappedAddress(ipAddress)) {
+ copy_address = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ copy_address[i] = ipAddress[12 + i];
+ }
+ return new Inet4Address(ipAddress, hostName);
+ }
+
+ copy_address = new byte[16];
+ for (int i = 0; i < 16; i++) {
+ copy_address[i] = ipAddress[i];
+ }
+
+ return new Inet6Address(ipAddress, hostName, scope_id);
+ }
+
+ throw new UnknownHostException(Msg.getString("K0332", hostName)); //$NON-NLS-1$
+ }
+
+ /**
+ * Takes the integer and chops it into 4 bytes, putting it into the byte
+ * array starting with the high order byte at the index start. This method
+ * makes no checks on the validity of the parameters.
+ */
+ static void intToBytes(int value, byte bytes[], int start) {
+ // Shift the int so the current byte is right-most
+ // Use a byte mask of 255 to single out the last byte.
+ bytes[start] = (byte) ((value >> 24) & 255);
+ bytes[start + 1] = (byte) ((value >> 16) & 255);
+ bytes[start + 2] = (byte) ((value >> 8) & 255);
+ bytes[start + 3] = (byte) (value & 255);
+ }
+
+ /**
+ * Takes the byte array and creates an integer out of four bytes starting at
+ * start as the high-order byte. This method makes no checks on the validity
+ * of the parameters.
+ */
+ static int bytesToInt(byte bytes[], int start) {
+ // First mask the byte with 255, as when a negative
+ // signed byte converts to an integer, it has bits
+ // on in the first 3 bytes, we are only concerned
+ // about the right-most 8 bits.
+ // Then shift the rightmost byte to align with its
+ // position in the integer.
+ int value = ((bytes[start + 3] & 255))
+ | ((bytes[start + 2] & 255) << 8)
+ | ((bytes[start + 1] & 255) << 16)
+ | ((bytes[start] & 255) << 24);
+ return value;
+ }
+
+ /**
+ * Creates an InetAddress based on an ipAddressString. No error handling is
+ * performed here.
+ */
+ static InetAddress createHostNameFromIPAddress(String ipAddressString)
+ throws UnknownHostException {
- InetAddress address = null;
+ InetAddress address = null;
- if (Inet6Util.isValidIPV4Address(ipAddressString)) {
+ if (Inet6Util.isValidIPV4Address(ipAddressString)) {
byte[] byteAddress = new byte[4];
- String[] parts = ipAddressString.split("\\.");
+ String[] parts = ipAddressString.split("\\."); //$NON-NLS-1$
int length = parts.length;
if (length == 1) {
Long value = Long.parseLong(parts[0]);
for (int i = 0; i < 4; i++) {
- byteAddress[i] = (byte) (value>>((3-i)*8));
+ byteAddress[i] = (byte) (value >> ((3 - i) * 8));
}
} else {
for (int i = 0; i < length; i++) {
- byteAddress[i]=(byte)Integer.parseInt(parts[i]);
+ byteAddress[i] = (byte) Integer.parseInt(parts[i]);
}
}
-
+
// adjust for 2/3 parts address
- if(length == 2){
- byteAddress[3]=byteAddress[1];
- byteAddress[1]=0;
- }
- if(length == 3){
- byteAddress[3]=byteAddress[2];
- byteAddress[2]=0;
+ if (length == 2) {
+ byteAddress[3] = byteAddress[1];
+ byteAddress[1] = 0;
+ }
+ if (length == 3) {
+ byteAddress[3] = byteAddress[2];
+ byteAddress[2] = 0;
}
-
+
address = new Inet4Address(byteAddress);
- } else { // otherwise it must be ipv6
+ } else { // otherwise it must be ipv6
+
+ if (ipAddressString.charAt(0) == '[') {
+ ipAddressString = ipAddressString.substring(1, ipAddressString
+ .length() - 1);
+ }
+
+ StringTokenizer tokenizer = new StringTokenizer(ipAddressString,
+ ":.%", true); //$NON-NLS-1$
+ ArrayList<String> hexStrings = new ArrayList<String>();
+ ArrayList<String> decStrings = new ArrayList<String>();
+ String scopeString = null;
+ String token = ""; //$NON-NLS-1$
+ String prevToken = ""; //$NON-NLS-1$
+ String prevPrevToken = ""; //$NON-NLS-1$
+ int doubleColonIndex = -1; // If a double colon exists, we need to
+ // insert 0s.
+
+ // Go through the tokens, including the separators ':' and '.'
+ // When we hit a : or . the previous token will be added to either
+ // the hex list or decimal list. In the case where we hit a ::
+ // we will save the index of the hexStrings so we can add zeros
+ // in to fill out the string
+ while (tokenizer.hasMoreTokens()) {
+ prevPrevToken = prevToken;
+ prevToken = token;
+ token = tokenizer.nextToken();
+
+ if (token.equals(":")) { //$NON-NLS-1$
+ if (prevToken.equals(":")) { //$NON-NLS-1$
+ doubleColonIndex = hexStrings.size();
+ } else if (!prevToken.equals("")) { //$NON-NLS-1$
+ hexStrings.add(prevToken);
+ }
+ } else if (token.equals(".")) { //$NON-NLS-1$
+ decStrings.add(prevToken);
+ } else if (token.equals("%")) { //$NON-NLS-1$
+ // add the last word before the % properly
+ if (!prevToken.equals(":") && !prevToken.equals(".")) { //$NON-NLS-1$ //$NON-NLS-2$
+ if (prevPrevToken.equals(":")) { //$NON-NLS-1$
+ hexStrings.add(prevToken);
+ } else if (prevPrevToken.equals(".")) { //$NON-NLS-1$
+ decStrings.add(prevToken);
+ }
+ }
+
+ // the rest should be the scope string
+ scopeString = tokenizer.nextToken();
+ while (tokenizer.hasMoreTokens()) {
+ scopeString = scopeString + tokenizer.nextToken();
+ }
+ }
+ }
- if (ipAddressString.charAt(0) == '[') {
- ipAddressString = ipAddressString.substring(1, ipAddressString
- .length() - 1);
- }
-
- StringTokenizer tokenizer = new StringTokenizer(ipAddressString,
- ":.%", true);
- ArrayList<String> hexStrings = new ArrayList<String>();
- ArrayList<String> decStrings = new ArrayList<String>();
- String scopeString = null;
- String token = "";
- String prevToken = "";
- String prevPrevToken = "";
- int doubleColonIndex = -1; // If a double colon exists, we need to
- // insert 0s.
-
- // Go through the tokens, including the separators ':' and '.'
- // When we hit a : or . the previous token will be added to either
- // the hex list or decimal list. In the case where we hit a ::
- // we will save the index of the hexStrings so we can add zeros
- // in to fill out the string
- while (tokenizer.hasMoreTokens()) {
- prevPrevToken = prevToken;
- prevToken = token;
- token = tokenizer.nextToken();
-
- if (token.equals(":")) {
- if (prevToken.equals(":")) {
- doubleColonIndex = hexStrings.size();
- } else if (!prevToken.equals("")) {
- hexStrings.add(prevToken);
- }
- } else if (token.equals(".")) {
- decStrings.add(prevToken);
- } else if (token.equals("%")) {
- // add the last word before the % properly
- if (!prevToken.equals(":") && !prevToken.equals(".")) {
- if (prevPrevToken.equals(":")) {
- hexStrings.add(prevToken);
- } else if (prevPrevToken.equals(".")) {
- decStrings.add(prevToken);
- }
- }
-
- // the rest should be the scope string
- scopeString = tokenizer.nextToken();
- while (tokenizer.hasMoreTokens()) {
- scopeString = scopeString + tokenizer.nextToken();
- }
- }
- }
-
- if (prevToken.equals(":")) {
- if (token.equals(":")) {
- doubleColonIndex = hexStrings.size();
- } else {
- hexStrings.add(token);
- }
- } else if (prevToken.equals(".")) {
- decStrings.add(token);
- }
-
- // figure out how many hexStrings we should have
- // also check if it is a IPv4 address
- int hexStringsLength = 8;
-
- // If we have an IPv4 address tagged on at the end, subtract
- // 4 bytes, or 2 hex words from the total
- if (decStrings.size() > 0) {
- hexStringsLength -= 2;
- }
-
- // if we hit a double Colon add the appropriate hex strings
- if (doubleColonIndex != -1) {
- int numberToInsert = hexStringsLength - hexStrings.size();
- for (int i = 0; i < numberToInsert; i++) {
- hexStrings.add(doubleColonIndex, "0");
- }
- }
-
- byte ipByteArray[] = new byte[16];
-
- // Finally convert these strings to bytes...
- for (int i = 0; i < hexStrings.size(); i++) {
- Inet6Util.convertToBytes(hexStrings.get(i),
- ipByteArray, i * 2);
- }
-
- // Now if there are any decimal values, we know where they go...
- for (int i = 0; i < decStrings.size(); i++) {
- ipByteArray[i + 12] = (byte) (Integer
- .parseInt(decStrings.get(i)) & 255);
- }
-
- // now check to see if this guy is actually and IPv4 address
- // an ipV4 address is ::FFFF:d.d.d.d
- boolean ipV4 = true;
- for (int i = 0; i < 10; i++) {
- if (ipByteArray[i] != 0) {
- ipV4 = false;
- break;
- }
- }
-
- if (ipByteArray[10] != -1 || ipByteArray[11] != -1) {
- ipV4 = false;
- }
-
- if (ipV4) {
- byte ipv4ByteArray[] = new byte[4];
- for (int i = 0; i < 4; i++) {
- ipv4ByteArray[i] = ipByteArray[i + 12];
- }
- address = InetAddress.getByAddress(ipv4ByteArray);
- } else {
- int scopeId = 0;
- if (scopeString != null) {
- try {
- scopeId = Integer.parseInt(scopeString);
- } catch (Exception e) {
- // this should not occur as we should not get into this
- // function
- // unless the address is in a valid format
- }
- }
- address = InetAddress.getByAddress(ipByteArray, scopeId);
- }
- }
-
- return address;
- }
-
- static boolean preferIPv6Addresses() {
- String result = AccessController.doPrivileged(
- new PriviAction<String>("java.net.preferIPv6Addresses"));
- return "true".equals(result);
- }
-
- private static final ObjectStreamField[] serialPersistentFields = {
- new ObjectStreamField("address", Integer.TYPE),
- new ObjectStreamField("family", Integer.TYPE),
- new ObjectStreamField("hostName", String.class) };
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- ObjectOutputStream.PutField fields = stream.putFields();
- if (ipaddress == null) {
- fields.put("address", 0);
- } else {
- fields.put("address", bytesToInt(ipaddress, 0));
- }
- fields.put("family", family);
- fields.put("hostName", hostName);
-
- stream.writeFields();
- }
-
- private void readObject(ObjectInputStream stream) throws IOException,
- ClassNotFoundException {
- ObjectInputStream.GetField fields = stream.readFields();
- int addr = fields.get("address", 0);
- ipaddress = new byte[4];
- intToBytes(addr, ipaddress, 0);
- hostName = (String) fields.get("hostName", null);
- family = fields.get("family", 2);
- }
-
- private Object readResolve() throws ObjectStreamException {
- return new Inet4Address(ipaddress, hostName);
- }
+ if (prevToken.equals(":")) { //$NON-NLS-1$
+ if (token.equals(":")) { //$NON-NLS-1$
+ doubleColonIndex = hexStrings.size();
+ } else {
+ hexStrings.add(token);
+ }
+ } else if (prevToken.equals(".")) { //$NON-NLS-1$
+ decStrings.add(token);
+ }
+
+ // figure out how many hexStrings we should have
+ // also check if it is a IPv4 address
+ int hexStringsLength = 8;
+
+ // If we have an IPv4 address tagged on at the end, subtract
+ // 4 bytes, or 2 hex words from the total
+ if (decStrings.size() > 0) {
+ hexStringsLength -= 2;
+ }
+
+ // if we hit a double Colon add the appropriate hex strings
+ if (doubleColonIndex != -1) {
+ int numberToInsert = hexStringsLength - hexStrings.size();
+ for (int i = 0; i < numberToInsert; i++) {
+ hexStrings.add(doubleColonIndex, "0"); //$NON-NLS-1$
+ }
+ }
+
+ byte ipByteArray[] = new byte[16];
+
+ // Finally convert these strings to bytes...
+ for (int i = 0; i < hexStrings.size(); i++) {
+ Inet6Util.convertToBytes(hexStrings.get(i), ipByteArray, i * 2);
+ }
+
+ // Now if there are any decimal values, we know where they go...
+ for (int i = 0; i < decStrings.size(); i++) {
+ ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings
+ .get(i)) & 255);
+ }
+
+ // now check to see if this guy is actually and IPv4 address
+ // an ipV4 address is ::FFFF:d.d.d.d
+ boolean ipV4 = true;
+ for (int i = 0; i < 10; i++) {
+ if (ipByteArray[i] != 0) {
+ ipV4 = false;
+ break;
+ }
+ }
+
+ if (ipByteArray[10] != -1 || ipByteArray[11] != -1) {
+ ipV4 = false;
+ }
+
+ if (ipV4) {
+ byte ipv4ByteArray[] = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ ipv4ByteArray[i] = ipByteArray[i + 12];
+ }
+ address = InetAddress.getByAddress(ipv4ByteArray);
+ } else {
+ int scopeId = 0;
+ if (scopeString != null) {
+ try {
+ scopeId = Integer.parseInt(scopeString);
+ } catch (Exception e) {
+ // this should not occur as we should not get into this
+ // function unless the address is in a valid format
+ }
+ }
+ address = InetAddress.getByAddress(ipByteArray, scopeId);
+ }
+ }
+
+ return address;
+ }
+
+ static boolean preferIPv6Addresses() {
+ String result = AccessController.doPrivileged(new PriviAction<String>(
+ "java.net.preferIPv6Addresses")); //$NON-NLS-1$
+ return "true".equals(result); //$NON-NLS-1$
+ }
+
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("address", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("family", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("hostName", String.class) }; //$NON-NLS-1$
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ ObjectOutputStream.PutField fields = stream.putFields();
+ if (ipaddress == null) {
+ fields.put("address", 0); //$NON-NLS-1$
+ } else {
+ fields.put("address", bytesToInt(ipaddress, 0)); //$NON-NLS-1$
+ }
+ fields.put("family", family); //$NON-NLS-1$
+ fields.put("hostName", hostName); //$NON-NLS-1$
+
+ stream.writeFields();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream.GetField fields = stream.readFields();
+ int addr = fields.get("address", 0); //$NON-NLS-1$
+ ipaddress = new byte[4];
+ intToBytes(addr, ipaddress, 0);
+ hostName = (String) fields.get("hostName", null); //$NON-NLS-1$
+ family = fields.get("family", 2); //$NON-NLS-1$
+ }
+
+ private Object readResolve() throws ObjectStreamException {
+ return new Inet4Address(ipaddress, hostName);
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java?view=diff&rev=493040&r1=493039&r2=493040
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java Fri Jan 5 06:44:04 2007
@@ -17,143 +17,144 @@
package java.net;
-
import java.io.IOException;
import java.io.ObjectInputStream;
public class InetSocketAddress extends SocketAddress {
- private static final long serialVersionUID = 5076001401234631237L;
+ private static final long serialVersionUID = 5076001401234631237L;
- private String hostname;
+ private String hostname;
- private InetAddress addr;
+ private InetAddress addr;
- private int port;
+ private int port;
- public InetSocketAddress(int port) {
- this((InetAddress) null, port);
- }
+ public InetSocketAddress(int port) {
+ this((InetAddress) null, port);
+ }
- public InetSocketAddress(InetAddress address, int port) {
- if (port < 0 || port > 65535) {
+ public InetSocketAddress(InetAddress address, int port) {
+ if (port < 0 || port > 65535) {
throw new IllegalArgumentException();
}
- if (address == null) {
+ if (address == null) {
addr = InetAddress.ANY;
} else {
addr = address;
}
- hostname = addr.getHostName();
- this.port = port;
- }
-
- public InetSocketAddress(String host, int port) {
- this(host,port,true);
- }
-
- /*
- * Internal contructor for InetSocketAddress(String, int) and
- * createUnresolved(String, int);
- */
- InetSocketAddress(String host, int port, boolean needResolved){
- if (host == null || port < 0 || port > 65535) {
+ hostname = addr.getHostName();
+ this.port = port;
+ }
+
+ public InetSocketAddress(String host, int port) {
+ this(host, port, true);
+ }
+
+ /*
+ * Internal contructor for InetSocketAddress(String, int) and
+ * createUnresolved(String, int);
+ */
+ InetSocketAddress(String host, int port, boolean needResolved) {
+ if (host == null || port < 0 || port > 65535) {
throw new IllegalArgumentException();
}
- hostname = host;
- this.port = port;
- if(needResolved){
- try {
- addr = InetAddress.getByName(hostname);
- hostname = null;
- } catch (UnknownHostException e) {
- }
- }else{
- addr = null;
- }
- }
-
- /**
- * Creats an <code>InetSocketAddress</code> without trying to resolve
- * hostname into an InetAddress. The address field is marked as unresolved.
- * @param host
- * @param port
- * @return an <code>InetSocketAddress</code> instance.
- * @throws IllegalArgumentException
- * if host is null or the port is not in the range between 0 and 65535.
- */
- public static InetSocketAddress createUnresolved(String host, int port){
- return new InetSocketAddress(host,port,false);
- }
-
- public final int getPort() {
- return port;
- }
-
- public final InetAddress getAddress() {
- return addr;
- }
+ hostname = host;
+ this.port = port;
+ if (needResolved) {
+ try {
+ addr = InetAddress.getByName(hostname);
+ hostname = null;
+ } catch (UnknownHostException e) {
+ // Ignored
+ }
+ } else {
+ addr = null;
+ }
+ }
+
+ /**
+ * Creats an <code>InetSocketAddress</code> without trying to resolve
+ * hostname into an InetAddress. The address field is marked as unresolved.
+ *
+ * @param host
+ * @param port
+ * @return an <code>InetSocketAddress</code> instance.
+ * @throws IllegalArgumentException
+ * if host is null or the port is not in the range between 0 and
+ * 65535.
+ */
+ public static InetSocketAddress createUnresolved(String host, int port) {
+ return new InetSocketAddress(host, port, false);
+ }
+
+ public final int getPort() {
+ return port;
+ }
+
+ public final InetAddress getAddress() {
+ return addr;
+ }
- public final String getHostName() {
+ public final String getHostName() {
return (null != addr) ? addr.getHostName() : hostname;
}
- public final boolean isUnresolved() {
- return addr == null;
- }
+ public final boolean isUnresolved() {
+ return addr == null;
+ }
- @Override
+ @Override
public String toString() {
- String host;
- if (addr != null) {
+ String host;
+ if (addr != null) {
host = addr.toString();
} else {
host = hostname;
}
- return host + ":" + port; //$NON-NLS-1$
- }
+ return host + ":" + port; //$NON-NLS-1$
+ }
- @Override
+ @Override
public final boolean equals(Object socketAddr) {
- if (this == socketAddr) {
+ if (this == socketAddr) {
return true;
}
- if (!(socketAddr instanceof InetSocketAddress)) {
+ if (!(socketAddr instanceof InetSocketAddress)) {
return false;
}
- InetSocketAddress iSockAddr = (InetSocketAddress) socketAddr;
+ InetSocketAddress iSockAddr = (InetSocketAddress) socketAddr;
+
+ // check the ports as we always need to do this
+ if (port != iSockAddr.port) {
+ return false;
+ }
+
+ // we only use the hostnames in the comparison if the addrs were not
+ // resolved
+ if ((addr == null) && (iSockAddr.addr == null)) {
+ return hostname.equals(iSockAddr.hostname);
+ }
- // check the ports as we always need to do this
- if (port != iSockAddr.port) {
- return false;
- }
-
- // we only use the hostnames in the comparison if the addrs were not
- // resolved
- if ((addr == null) && (iSockAddr.addr == null)) {
- return hostname.equals(iSockAddr.hostname);
- }
-
// addrs were resolved so use them for the comparison
if (addr == null) {
- // if we are here we know iSockAddr is not null so just return
- // false
- return false;
+ // if we are here we know iSockAddr is not null so just return
+ // false
+ return false;
}
return addr.equals(iSockAddr.addr);
- }
+ }
- @Override
+ @Override
public final int hashCode() {
- if (addr == null) {
+ if (addr == null) {
return hostname.hashCode() + port;
}
- return addr.hashCode() + port;
- }
+ return addr.hashCode() + port;
+ }
- private void readObject(ObjectInputStream stream) throws IOException,
- ClassNotFoundException {
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
stream.defaultReadObject();
- }
-
+ }
}