< prev index next >

src/java.base/share/classes/java/net/InetAddress.java

Print this page
8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
Reviewed-by: alanb


 273          * Specifies the address family type, for instance, '1' for IPv4
 274          * addresses, and '2' for IPv6 addresses.
 275          */
 276         int family;
 277 
 278         int getFamily() {
 279             return family;
 280         }
 281     }
 282 
 283     /* Used to store the serializable fields of InetAddress */
 284     final transient InetAddressHolder holder;
 285 
 286     InetAddressHolder holder() {
 287         return holder;
 288     }
 289 
 290     /* Used to store the name service provider */
 291     private static transient NameService nameService = null;
 292 
 293     /* Used to store the best available hostname */



 294     private transient String canonicalHostName = null;
 295 
 296     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 297     private static final long serialVersionUID = 3286316764910316507L;
 298 
 299     /*
 300      * Load net library into runtime, and perform initializations.
 301      */
 302     static {
 303         String str = java.security.AccessController.doPrivileged(
 304                 new GetPropertyAction("java.net.preferIPv6Addresses"));
 305         if (str == null) {
 306             preferIPv6Address = PREFER_IPV4_VALUE;
 307         } else if (str.equalsIgnoreCase("true")) {
 308             preferIPv6Address = PREFER_IPV6_VALUE;
 309         } else if (str.equalsIgnoreCase("false")) {
 310             preferIPv6Address = PREFER_IPV4_VALUE;
 311         } else if (str.equalsIgnoreCase("system")) {
 312             preferIPv6Address = PREFER_SYSTEM_VALUE;
 313         } else {


 605      * Best effort method, meaning we may not be able to return
 606      * the FQDN depending on the underlying system configuration.
 607      *
 608      * <p>If there is a security manager, this method first
 609      * calls its {@code checkConnect} method
 610      * with the hostname and {@code -1}
 611      * as its arguments to see if the calling code is allowed to know
 612      * the hostname for this IP address, i.e., to connect to the host.
 613      * If the operation is not allowed, it will return
 614      * the textual representation of the IP address.
 615      *
 616      * @return  the fully qualified domain name for this IP address,
 617      *    or if the operation is not allowed by the security check,
 618      *    the textual representation of the IP address.
 619      *
 620      * @see SecurityManager#checkConnect
 621      *
 622      * @since 1.4
 623      */
 624     public String getCanonicalHostName() {
 625         if (canonicalHostName == null) {
 626             canonicalHostName =

 627                 InetAddress.getHostFromNameService(this, true);
 628         }
 629         return canonicalHostName;
 630     }
 631 
 632     /**
 633      * Returns the hostname for this address.
 634      *
 635      * <p>If there is a security manager, this method first
 636      * calls its {@code checkConnect} method
 637      * with the hostname and {@code -1}
 638      * as its arguments to see if the calling code is allowed to know
 639      * the hostname for this IP address, i.e., to connect to the host.
 640      * If the operation is not allowed, it will return
 641      * the textual representation of the IP address.
 642      *
 643      * @return  the host name for this IP address, or if the operation
 644      *    is not allowed by the security check, the textual
 645      *    representation of the IP address.
 646      *
 647      * @param check make security check if true
 648      *
 649      * @see SecurityManager#checkConnect




 273          * Specifies the address family type, for instance, '1' for IPv4
 274          * addresses, and '2' for IPv6 addresses.
 275          */
 276         int family;
 277 
 278         int getFamily() {
 279             return family;
 280         }
 281     }
 282 
 283     /* Used to store the serializable fields of InetAddress */
 284     final transient InetAddressHolder holder;
 285 
 286     InetAddressHolder holder() {
 287         return holder;
 288     }
 289 
 290     /* Used to store the name service provider */
 291     private static transient NameService nameService = null;
 292 
 293     /**
 294      * Used to store the best available hostname.
 295      * Lazily initialized via a data race; safe because Strings are immutable.
 296      */
 297     private transient String canonicalHostName = null;
 298 
 299     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 300     private static final long serialVersionUID = 3286316764910316507L;
 301 
 302     /*
 303      * Load net library into runtime, and perform initializations.
 304      */
 305     static {
 306         String str = java.security.AccessController.doPrivileged(
 307                 new GetPropertyAction("java.net.preferIPv6Addresses"));
 308         if (str == null) {
 309             preferIPv6Address = PREFER_IPV4_VALUE;
 310         } else if (str.equalsIgnoreCase("true")) {
 311             preferIPv6Address = PREFER_IPV6_VALUE;
 312         } else if (str.equalsIgnoreCase("false")) {
 313             preferIPv6Address = PREFER_IPV4_VALUE;
 314         } else if (str.equalsIgnoreCase("system")) {
 315             preferIPv6Address = PREFER_SYSTEM_VALUE;
 316         } else {


 608      * Best effort method, meaning we may not be able to return
 609      * the FQDN depending on the underlying system configuration.
 610      *
 611      * <p>If there is a security manager, this method first
 612      * calls its {@code checkConnect} method
 613      * with the hostname and {@code -1}
 614      * as its arguments to see if the calling code is allowed to know
 615      * the hostname for this IP address, i.e., to connect to the host.
 616      * If the operation is not allowed, it will return
 617      * the textual representation of the IP address.
 618      *
 619      * @return  the fully qualified domain name for this IP address,
 620      *    or if the operation is not allowed by the security check,
 621      *    the textual representation of the IP address.
 622      *
 623      * @see SecurityManager#checkConnect
 624      *
 625      * @since 1.4
 626      */
 627     public String getCanonicalHostName() {
 628         String value = canonicalHostName;
 629         if (value == null)
 630             canonicalHostName = value =
 631                 InetAddress.getHostFromNameService(this, true);
 632         return value;

 633     }
 634 
 635     /**
 636      * Returns the hostname for this address.
 637      *
 638      * <p>If there is a security manager, this method first
 639      * calls its {@code checkConnect} method
 640      * with the hostname and {@code -1}
 641      * as its arguments to see if the calling code is allowed to know
 642      * the hostname for this IP address, i.e., to connect to the host.
 643      * If the operation is not allowed, it will return
 644      * the textual representation of the IP address.
 645      *
 646      * @return  the host name for this IP address, or if the operation
 647      *    is not allowed by the security check, the textual
 648      *    representation of the IP address.
 649      *
 650      * @param check make security check if true
 651      *
 652      * @see SecurityManager#checkConnect


< prev index next >