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

Print this page

        

@@ -279,11 +279,11 @@
     InetAddressHolder holder() {
         return holder;
     }
 
     /* Used to store the name service provider */
-    private static transient NameService nameService = null;
+    private static NameService nameService = null;
 
     /* Used to store the best available hostname */
     private transient String canonicalHostName = null;
 
     /** use serialVersionUID from JDK 1.0.2 for interoperability */

@@ -856,72 +856,68 @@
     }
 
     /**
      * NameService provides host and address lookup service
      * 
+     * @since 9
      */
     private interface NameService {
 
         /**
-         * Lookup a host mapping by name. Retrieve the IP addresses
-         * associated with a host
+         * Looks up a host mapping by name. Retrieves the IP addresses
+         * associated with a host.
          * 
          * @param host the specified hostname
-         * @return array of IP addresses for the requested host
+         * @return an array of IP addresses for the given host name
          * @throws UnknownHostException
          *             if no IP address for the {@code host} could be found
          */
         InetAddress[] lookupAllHostAddr(String host)
                 throws UnknownHostException;
 
         /**
-         * Lookup the host corresponding to the IP address provided
+         * Looks up the host corresponding to the given IP address.
          * 
          * @param addr byte array representing an IP address
          * @return {@code String} representing the host name mapping
          * @throws UnknownHostException
          *             if no host found for the specified IP address
          */
         String getHostByAddr(byte[] addr) throws UnknownHostException;
-
     }
 
     /**
      * The default NameService implementation, which delegates to the underlying
      * OS network libraries to resolve host address mappings.
      * 
+     * @since 9
      */
     private static final class PlatformNameService implements NameService {
 
         public InetAddress[] lookupAllHostAddr(String host)
-                throws UnknownHostException {
-
+            throws UnknownHostException
+        {
             return impl.lookupAllHostAddr(host);
-
         }
 
         public String getHostByAddr(byte[] addr) throws UnknownHostException {
-
             return impl.getHostByAddr(addr);
-
         }
-
     }
 
     /**
-     * <p>The HostsFileNameService provides host address mapping
-     * by reading the entries in a hosts file, which is specified in the
-     * property jdk.net.hosts.file 
+     * A HostsFileNameService provides host address mappings by reading the
+     * entries in a hosts file, which is specified by the {@code
+     * jdk.net.hosts.file} system property.
      *
-     * <p>The file format is that which corresponds with the /etc/hosts file
-     * ipaddress host alias list
+     * <p> The file format is that which corresponds to the /etc/hosts file
+     * IP Address host alias list.
      *
-     * <p>When the file lookup is enabled it replaces the call flows to the native
-     * OS ip nameservice lookup calls
+     * <p> When the file lookup is enabled it replaces the default NameService
+     * implementation.
      *
-     * <p>This functionalty doesn't currently support IPV6 mappings
-     * This can be provided whenever it is required
+     * @since 9
      */
     private static final class HostsFileNameService implements NameService {
 
         private final String hostsFile;
         

@@ -933,19 +929,20 @@
             return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "."
                     + Byte.toString(addr[2]) + "." + Byte.toString(addr[3]);
         }
 
         /**
-         * Lookup the host name  corresponding to the IP address provided.
-         * Search the configured host file a host name corresponding to
+         * Looks up the host name corresponding to the IP address provided.
+         * Searches the configured host file for a host name corresponding to
          * the specified IP address.
          *
          * @param addr byte array representing an IP address
          * @return {@code String} representing the host name mapping
          * @throws UnknownHostException
          *             if no host found for the specified IP address
          */
+        @Override
         public String getHostByAddr(byte[] addr) throws UnknownHostException {
             String hostEntry;
             String host = null;
 
             String addrString = addrToString(addr);

@@ -977,27 +974,28 @@
             return host;
         }
 
 
         /**
-         * <p>Lookup a host mapping by name. Retrieve the IP addresses
+         * Looks up a host mapping by name. Retrieves the IP addresses
          * associated with a host.
          *
-         * <p>Search the configured hosts file for the addresses assocaited with
-         * with the specified host name.
+         * <p> Searches the configured hosts file for the addresses associated
+         * with with the specified host name.
          *
          * @param host the specified hostname
          * @return array of IP addresses for the requested host
          * @throws UnknownHostException
          *             if no IP address for the {@code host} could be found
          */
-
+        @Override
         public InetAddress[] lookupAllHostAddr(String host)
-                throws UnknownHostException {
+            throws UnknownHostException
+        {
             String hostEntry;
-            String addrStr = null;
-            InetAddress[] res = null;
+            String addrStr;
+            InetAddress[] res;
             byte addr[] = new byte[4];
             ArrayList<InetAddress> inetAddresses = null;
 
             // lookup the file and create a list InetAddress for the specfied host
             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {

@@ -1040,18 +1038,17 @@
             }
             return filteredEntry;
         }
 
         private void createAddressByteArray(String addrStr, byte[] addr) {
-
             String[] ipAddr = addrStr.split("\\.");
             for (int i = 0; i < 4; i++) {
                 addr[i] = (byte) Integer.parseInt(ipAddr[i]);
             }
         }
 
-        // host to ip address mapping
+        /** Host to IP address mapping. */
         private String extractHostAddr(String hostEntry, String host) {
             String[] mapping = hostEntry.split("\\s+");
             String hostAddr = null;
 
             if (mapping.length >= 2) {

@@ -1063,12 +1060,11 @@
                 }
             }
             return hostAddr;
         }
 
-        // ipaddress to host mapping
-        // use first host alias in list
+        /** IP Address to host mapping. Uses first host alias in list. */
         private String extractHost(String hostEntry, String addrString) {
             String[] mapping = hostEntry.split("\\s+");
             String host = null;
 
             if (mapping.length >= 2) {

@@ -1089,35 +1085,33 @@
         // create name service
         nameService = createNameService();
     }
 
     /**
-     * Create an instance of the NameService interface based on
-     * the setting of the system property jdk.net.hosts.file.
+     * Creates an instance of the NameService interface based on the value of
+     * the {@code jdk.net.hosts.file} system property.
      *
-     * <p>The default NameService is the PlatformNameService, which typically
-     * delegates name and address resolution calls to the underlying
-     * OS network libraries.
-     *
-     * <p>If jdk.net.hosts.file is set, and the file exists then
-     * instantiate a HostsFileNameService
-     * else
-     * instantiate a PlatformNameService.
+     * <p> The default NameService is the PlatformNameService, which typically
+     * delegates name and address resolution calls to the underlying  OS network
+     * libraries.
+     *
+     * <p> A HostsFileNameService is created if the {@code jdk.net.hosts.file}
+     * system property is set. If the specified file doesn't exist, the name or
+     * address lookup will result in an UnknownHostException. Thus, non existent
+     * hosts file is handled as if the file is empty.
      *  
-     * @return the instantiated {@code NameService}
+     * @return a NameService
      */
     private static NameService createNameService() {
-
         String hostsFileName = AccessController
                 .doPrivileged(new GetPropertyAction("jdk.net.hosts.file"));
-        NameService theNameService;
+
         if (hostsFileName != null) {
-            theNameService = new HostsFileNameService(hostsFileName);
+            return new HostsFileNameService(hostsFileName);
         } else {
-            theNameService = new PlatformNameService();
+            return new PlatformNameService();
         }
-        return theNameService;
     }
 
     /**
      * Creates an InetAddress based on the provided host name and IP address.
      * No name service is checked for the validity of the address.