📄 inetaddress.java
字号:
} catch (SecurityException ex) { return ex; } } /** * Returns the IP address as string * * @since 1.0.2 */ public String getHostAddress () { StringBuffer sbuf = new StringBuffer(40); int len = addr.length; int i = 0; if (len == 16) { // An IPv6 address. for (; ; i += 2) { if (i >= 16) return sbuf.toString(); int x = ((addr[i] & 0xFF) << 8) | (addr[i+1] & 0xFF); boolean empty = sbuf.length() == 0; if (empty) { if (i == 10 && x == 0xFFFF) { // IPv4-mapped IPv6 address. sbuf.append(":FFFF:"); break; // Continue as IPv4 address; } else if (i == 12) { // IPv4-compatible IPv6 address. sbuf.append(':'); break; // Continue as IPv4 address. } else if (i > 0) sbuf.append("::"); } else sbuf.append(':'); if (x != 0 || i >= 14) sbuf.append(Integer.toHexString(x).toUpperCase()); } } for ( ; ; ) { sbuf.append(addr[i] & 0xFF); i++; if (i == len) break; sbuf.append('.'); } return sbuf.toString(); } /** * Returns a hashcode of the InetAddress */ public int hashCode() { // There hashing algorithm is not specified, but a simple experiment // shows that it is equal to the address, as a 32-bit big-endian integer. int hash = 0; int len = addr.length; int i = len > 4 ? len - 4 : 0; for ( ; i < len; i++) hash = (hash << 8) | (addr[i] & 0xFF); return hash; } /** * Compares the InetAddress object with another one. */ public boolean equals (Object obj) { if (obj == null || ! (obj instanceof InetAddress)) return false; // "The Java Class Libraries" 2nd edition says "If a machine has // multiple names instances of InetAddress for different name of // that same machine are not equal. This is because they have // different host names." This violates the description in the // JDK 1.2 API documentation. A little experimentation // shows that the latter is correct. byte[] addr1 = addr; byte[] addr2 = ((InetAddress) obj).addr; if (addr1.length != addr2.length) return false; for (int i = addr1.length; --i >= 0; ) if (addr1[i] != addr2[i]) return false; return true; } /** * Returns then <code>InetAddress</code> as string */ public String toString() { String result; String address = getHostAddress(); if (hostName != null) result = hostName + "/" + address; else result = address; return result; } /** * Returns an InetAddress object given the raw IP address. * * The argument is in network byte order: the highest order byte of the * address is in getAddress()[0]. * * @param addr The IP address to create the InetAddress object from * * @exception UnknownHostException If IP address has illegal length * * @since 1.4 */ public static InetAddress getByAddress(byte[] addr) throws UnknownHostException { if (addr.length != 4 && addr.length != 16) throw new UnknownHostException ("IP address has illegal length"); if (addr.length == 4) return new Inet4Address (addr, null); return new Inet6Address (addr, null); } /** * Creates an InetAddress based on the provided host name and IP address. * No name service is checked for the validity of the address. * * @param host The hostname of the InetAddress object to create * @param addr The IP address to create the InetAddress object from * * @exception UnknownHostException If IP address is of illegal length * * @since 1.4 */ public static InetAddress getByAddress (String host, byte[] addr) throws UnknownHostException { if (addr.length == 4) return new Inet4Address (addr, host); if (addr.length == 16) return new Inet6Address (addr, host); throw new UnknownHostException ("IP address has illegal length"); } /** If host is a valid numeric IP address, return the numeric address. * Otherwise, return null. */ private static native byte[] aton (String host); private static native InetAddress[] lookup (String hostname, InetAddress addr, boolean all); /** * Determines the IP address of a host, given the host's name. * * @exception UnknownHostException If no IP address for the host could * be found * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ public static InetAddress getByName (String hostname) throws UnknownHostException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect (hostname, -1); // Default to current host if necessary if (hostname == null) return getLocalHost(); // Assume that the host string is an IP address byte[] address = aton (hostname); if (address != null) { if (address.length == 4) return new Inet4Address (address, null); else if (address.length == 16) { if ((address[10] == 0xFF) && (address[11] == 0xFF)) { byte[] ip4addr = new byte[4]; ip4addr[0] = address[12]; ip4addr[1] = address[13]; ip4addr[2] = address[14]; ip4addr[3] = address[15]; return new Inet4Address (ip4addr, null); } return new Inet6Address (address, null); } else throw new UnknownHostException ("Address has invalid length"); } // Try to resolve the host by DNS InetAddress[] addresses = getAllByName (hostname); return addresses[0]; } /** * Given the name of a host, returns an array of its IP addresses, * based on the configured name service on the system. * * @exception UnknownHostException If no IP address for the host could * be found * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ public static InetAddress[] getAllByName (String hostname) throws UnknownHostException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(hostname, -1); // Check if hostname is an IP address byte[] address = aton (hostname); if (address != null) { InetAddress[] result = new InetAddress[1]; result[0] = new InetAddress(address, null); return result; } // Try to resolve the hostname by DNS return lookup (hostname, null, true); } static final byte[] zeros = {0,0,0,0}; /* dummy InetAddress, used to bind socket to any (all) network interfaces */ static final InetAddress ANY_IF = new InetAddress(zeros, null); private static final byte[] localhostAddress = { 127, 0, 0, 1 }; private static native String getLocalHostname (); private static InetAddress localhost = null; /** * Returns the local host * * @exception UnknownHostException If no IP address for the host could * be found */ public static InetAddress getLocalHost() throws UnknownHostException { SecurityManager s = System.getSecurityManager(); // Experimentation shows that JDK1.2 does cache the result. // 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)) getLocalHost(s); return localhost; } private static synchronized void getLocalHost(SecurityManager s) throws UnknownHostException { // Check the localhost cache again, now that we've synchronized. if (s == null && localhost != null) return; String hostname = getLocalHostname(); if (s != null) { // "The Java Class Libraries" suggests that if the security // manager disallows getting the local host name, then // we use the loopback host. // However, the JDK 1.2 API claims to throw SecurityException, // which seems to suggest SecurityException is *not* caught. // In this case, experimentation shows that former is correct. try { // This is wrong, if the name returned from getLocalHostname() // is not a fully qualified name. FIXME. s.checkConnect(hostname, -1); } catch (SecurityException ex) { hostname = null; } } if (hostname != null) { try { localhost = new InetAddress(null, null); lookup(hostname, localhost, false); } catch (Exception ex) { } } if (localhost == null) localhost = new InetAddress (localhostAddress, "localhost"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -