⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inetaddress.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    	    if (empty)	      {		if (i == 10 && x == 0xFFFF)		  { // IPv4-mapped IPv6 address.		    sb.append (":FFFF:");		    break;  // Continue as IPv4 address;		  }		else if (i == 12)		  { // IPv4-compatible IPv6 address.		    sb.append (':');		    break;  // Continue as IPv4 address.		  }		else if (i > 0)		  sb.append ("::");	      }	    else	      sb.append (':');	    	    if (x != 0 || i >= 14)	      sb.append (Integer.toHexString (x).toUpperCase());	  }      }        for ( ; ; )      {        sb.append(addr[i] & 0xff);        i++;	        if (i == len)          break;	        sb.append('.');      }    return sb.toString();  }  /**   * Returns a hash value for this address.  Useful for creating hash   * tables.  Overrides Object.hashCode()   *   * @return A hash value for this address.   */  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;  }  /**   * Tests this address for equality against another InetAddress.  The two   * addresses are considered equal if they contain the exact same octets.   * This implementation overrides Object.equals()   *   * @param obj The address to test for equality   *   * @return true if the passed in object's address is equal to this one's,   * false otherwise   */  public boolean equals(Object obj)  {    if (! (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[] addr2 = ((InetAddress) obj).addr;    if (addr.length != addr2.length)      return false;    for (int i = 0; i < addr.length; i++)      if (addr[i] != addr2[i])	return false;    return true;  }  /**   * Converts this address to a String.  This string contains the IP in   * dotted decimal form. For example: "127.0.0.1"  This method is equivalent   * to getHostAddress() and overrides Object.toString()   *   * @return This address in String form   */  public String toString()  {    String addr = getHostAddress();    String host = (hostName != null) ? hostName : "";    return host + "/" + addr;  }  /**   * 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  {    return getByAddress(null, addr);  }  /**   * 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 hostname is a valid numeric IP address, return the numeric address.   * Otherwise, return null.   *   * @param hostname the name of the host   */  private static native byte[] aton(String hostname);  /**   * Looks up all addresses of a given host.   *   * @param hostname the host to lookup   * @param ipaddr the IP address to lookup   * @param all return all known addresses for one host   *   * @return an array with all found addresses   */  private static native InetAddress[] lookup (String hostname,		                              InetAddress ipaddr, boolean all);  /**   * Returns tha family type of an IP address.   *   * @param addr the IP address   *   * @return the family   */  private static native int getFamily (byte[] ipaddr);  /**   * Returns an InetAddress object representing the IP address of the given   * hostname.  This name can be either a hostname such as "www.urbanophile.com"   * or an IP address in dotted decimal format such as "127.0.0.1".  If the   * hostname is null or "", the hostname of the local machine is supplied by   * default.  This method is equivalent to returning the first element in   * the InetAddress array returned from GetAllByName.   *   * @param hostname The name of the desired host, or null for the local    * loopback address.   *   * @return The address of the host as an InetAddress object.   *   * @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  {    // If null or the empty string is supplied, the loopback address    // is returned. Note that this is permitted without a security check.    if (hostname == null || hostname.length() == 0)      return loopback;    SecurityManager s = System.getSecurityManager();    if (s != null)      s.checkConnect(hostname, -1);    // 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 result = new InetAddress(null, null);    lookup (hostname, result, false);    return result;  }  /**   * Returns an array of InetAddress objects representing all the host/ip   * addresses of a given host, given the host's name.  This name can be   * either a hostname such as "www.urbanophile.com" or an IP address in   * dotted decimal format such as "127.0.0.1".  If the value is null, the   * hostname of the local machine is supplied by default.   *   * @param hostname The name of the desired host, or null for the   * local loopback address.   *   * @return All addresses of the host as an array of InetAddress objects.   *   * @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  {    // If null or the empty string is supplied, the loopback address    // is returned. Note that this is permitted without a security check.    if (hostname == null || hostname.length() == 0)      return new InetAddress[] {loopback};    SecurityManager s = System.getSecurityManager();    if (s != null)      s.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);  }  /**   * This native method looks up the hostname of the local machine   * we are on.  If the actual hostname cannot be determined, then the   * value "localhost" will be used.  This native method wrappers the   * "gethostname" function.   *   * @return The local hostname.   */  private static native String getLocalHostname();  /**   * Returns an InetAddress object representing the address of the current   * host.   *   * @return The local host's address   *   * @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.isLoopbackAddress()))      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 && hostname.length() != 0)      {	try	  {	    localhost = new InetAddress (null, null);	    lookup (hostname, localhost, false);	  }	catch (Exception ex)	  {	  }      }    else      throw new UnknownHostException();        if (localhost == null)      localhost = new InetAddress (loopbackAddress, "localhost");  }  /**   * Needed for serialization   */  private void readResolve() throws ObjectStreamException  {    // FIXME: implement this  }  private void readObject(ObjectInputStream ois)    throws IOException, ClassNotFoundException  {    ois.defaultReadObject();    addr = new byte[4];    addr[3] = (byte) address;    for (int i = 2; i >= 0; --i)      addr[i] = (byte) (address >>= 8);    // Ignore family from serialized data.  Since the saved address is 32 bits    // the deserialized object will have an IPv4 address i.e. AF_INET family.    // FIXME: An alternative is to call the aton method on the deserialized    // hostname to get a new address.  The Serialized Form doc is silent    // on how these fields are used.    family = getFamily (addr);  }  private void writeObject(ObjectOutputStream oos) throws IOException  {    // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address    // or a 16 byte IPv6 address.    int len = addr.length;    int i = len - 4;    for (; i < len; i++)      address = address << 8 | (((int) addr[i]) & 0xFF);    oos.defaultWriteObject();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -