< prev index next >

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

Print this page




 939      * The HostsFileNameService provides host address mapping
 940      * by reading the entries in a hosts file, which is specified by
 941      * {@code jdk.net.hosts.file} system property
 942      *
 943      * <p>The file format is that which corresponds with the /etc/hosts file
 944      * IP Address host alias list.
 945      *
 946      * <p>When the file lookup is enabled it replaces the default NameService
 947      * implementation
 948      *
 949      * @since 9
 950      */
 951     private static final class HostsFileNameService implements NameService {
 952 
 953         private final String hostsFile;
 954 
 955         public HostsFileNameService (String hostsFileName) {
 956             this.hostsFile = hostsFileName;
 957         }
 958 
 959         private  String addrToString(byte addr[]) {
 960           String stringifiedAddress = null;
 961 
 962             if (addr.length == Inet4Address.INADDRSZ) {
 963                 stringifiedAddress = Inet4Address.numericToTextFormat(addr);
 964             } else { // treat as an IPV6 jobby
 965                 byte[] newAddr
 966                     = IPAddressUtil.convertFromIPv4MappedAddress(addr);
 967                 if (newAddr != null) {
 968                    stringifiedAddress = Inet4Address.numericToTextFormat(addr);
 969                 } else {
 970                     stringifiedAddress = Inet6Address.numericToTextFormat(addr);
 971                 }
 972             }
 973             return stringifiedAddress;
 974         }
 975 
 976         /**
 977          * Lookup the host name  corresponding to the IP address provided.
 978          * Search the configured host file a host name corresponding to
 979          * the specified IP address.


1017             return host;
1018         }
1019 
1020         /**
1021          * <p>Lookup a host mapping by name. Retrieve the IP addresses
1022          * associated with a host.
1023          *
1024          * <p>Search the configured hosts file for the addresses assocaited with
1025          * with the specified host name.
1026          *
1027          * @param host the specified hostname
1028          * @return array of IP addresses for the requested host
1029          * @throws UnknownHostException
1030          *             if no IP address for the {@code host} could be found
1031          */
1032         public InetAddress[] lookupAllHostAddr(String host)
1033                 throws UnknownHostException {
1034             String hostEntry;
1035             String addrStr = null;
1036             InetAddress[] res = null;
1037             byte addr[] = new byte[4];
1038             ArrayList<InetAddress> inetAddresses = null;
1039 
1040             // lookup the file and create a list InetAddress for the specfied host
1041             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
1042                 while (hostsFileScanner.hasNextLine()) {
1043                     hostEntry = hostsFileScanner.nextLine();
1044                     if (!hostEntry.startsWith("#")) {
1045                         hostEntry = removeComments(hostEntry);
1046                         if (hostEntry.contains(host)) {
1047                             addrStr = extractHostAddr(hostEntry, host);
1048                             if ((addrStr != null) && (!addrStr.equals(""))) {
1049                                 addr = createAddressByteArray(addrStr);
1050                                 if (inetAddresses == null) {
1051                                     inetAddresses = new ArrayList<>(1);
1052                                 }
1053                                 if (addr != null) {
1054                                     inetAddresses.add(InetAddress.getByAddress(host, addr));
1055                                 }
1056                             }
1057                         }




 939      * The HostsFileNameService provides host address mapping
 940      * by reading the entries in a hosts file, which is specified by
 941      * {@code jdk.net.hosts.file} system property
 942      *
 943      * <p>The file format is that which corresponds with the /etc/hosts file
 944      * IP Address host alias list.
 945      *
 946      * <p>When the file lookup is enabled it replaces the default NameService
 947      * implementation
 948      *
 949      * @since 9
 950      */
 951     private static final class HostsFileNameService implements NameService {
 952 
 953         private final String hostsFile;
 954 
 955         public HostsFileNameService (String hostsFileName) {
 956             this.hostsFile = hostsFileName;
 957         }
 958 
 959         private  String addrToString(byte[] addr) {
 960           String stringifiedAddress = null;
 961 
 962             if (addr.length == Inet4Address.INADDRSZ) {
 963                 stringifiedAddress = Inet4Address.numericToTextFormat(addr);
 964             } else { // treat as an IPV6 jobby
 965                 byte[] newAddr
 966                     = IPAddressUtil.convertFromIPv4MappedAddress(addr);
 967                 if (newAddr != null) {
 968                    stringifiedAddress = Inet4Address.numericToTextFormat(addr);
 969                 } else {
 970                     stringifiedAddress = Inet6Address.numericToTextFormat(addr);
 971                 }
 972             }
 973             return stringifiedAddress;
 974         }
 975 
 976         /**
 977          * Lookup the host name  corresponding to the IP address provided.
 978          * Search the configured host file a host name corresponding to
 979          * the specified IP address.


1017             return host;
1018         }
1019 
1020         /**
1021          * <p>Lookup a host mapping by name. Retrieve the IP addresses
1022          * associated with a host.
1023          *
1024          * <p>Search the configured hosts file for the addresses assocaited with
1025          * with the specified host name.
1026          *
1027          * @param host the specified hostname
1028          * @return array of IP addresses for the requested host
1029          * @throws UnknownHostException
1030          *             if no IP address for the {@code host} could be found
1031          */
1032         public InetAddress[] lookupAllHostAddr(String host)
1033                 throws UnknownHostException {
1034             String hostEntry;
1035             String addrStr = null;
1036             InetAddress[] res = null;
1037             byte[] addr = new byte[4];
1038             ArrayList<InetAddress> inetAddresses = null;
1039 
1040             // lookup the file and create a list InetAddress for the specfied host
1041             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
1042                 while (hostsFileScanner.hasNextLine()) {
1043                     hostEntry = hostsFileScanner.nextLine();
1044                     if (!hostEntry.startsWith("#")) {
1045                         hostEntry = removeComments(hostEntry);
1046                         if (hostEntry.contains(host)) {
1047                             addrStr = extractHostAddr(hostEntry, host);
1048                             if ((addrStr != null) && (!addrStr.equals(""))) {
1049                                 addr = createAddressByteArray(addrStr);
1050                                 if (inetAddresses == null) {
1051                                     inetAddresses = new ArrayList<>(1);
1052                                 }
1053                                 if (addr != null) {
1054                                     inetAddresses.add(InetAddress.getByAddress(host, addr));
1055                                 }
1056                             }
1057                         }


< prev index next >