< prev index next >

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

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()


 992             String addrString = addrToString(addr);
 993             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
 994                 while (hostsFileScanner.hasNextLine()) {
 995                     hostEntry = hostsFileScanner.nextLine();
 996                     if (!hostEntry.startsWith("#")) {
 997                         hostEntry = removeComments(hostEntry);
 998                         if (hostEntry.contains(addrString)) {
 999                             host = extractHost(hostEntry, addrString);
1000                             if (host != null) {
1001                                 break;
1002                             }
1003                         }
1004                     }
1005                 }
1006             } catch (FileNotFoundException e) {
1007                 throw new UnknownHostException("Unable to resolve address "
1008                         + addrString + " as hosts file " + hostsFile
1009                         + " not found ");
1010             }
1011 
1012             if ((host == null) || (host.equals("")) || (host.equals(" "))) {
1013                 throw new UnknownHostException("Requested address "
1014                         + addrString
1015                         + " resolves to an invalid entry in hosts file "
1016                         + hostsFile);
1017             }
1018             return host;
1019         }
1020 
1021         /**
1022          * <p>Lookup a host mapping by name. Retrieve the IP addresses
1023          * associated with a host.
1024          *
1025          * <p>Search the configured hosts file for the addresses associated
1026          * with the specified host name.
1027          *
1028          * @param host the specified hostname
1029          * @return array of IP addresses for the requested host
1030          * @throws UnknownHostException
1031          *             if no IP address for the {@code host} could be found
1032          */
1033         public InetAddress[] lookupAllHostAddr(String host)
1034                 throws UnknownHostException {
1035             String hostEntry;
1036             String addrStr = null;
1037             InetAddress[] res = null;
1038             byte addr[] = new byte[4];
1039             ArrayList<InetAddress> inetAddresses = null;
1040 
1041             // lookup the file and create a list InetAddress for the specified host
1042             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
1043                 while (hostsFileScanner.hasNextLine()) {
1044                     hostEntry = hostsFileScanner.nextLine();
1045                     if (!hostEntry.startsWith("#")) {
1046                         hostEntry = removeComments(hostEntry);
1047                         if (hostEntry.contains(host)) {
1048                             addrStr = extractHostAddr(hostEntry, host);
1049                             if ((addrStr != null) && (!addrStr.equals(""))) {
1050                                 addr = createAddressByteArray(addrStr);
1051                                 if (inetAddresses == null) {
1052                                     inetAddresses = new ArrayList<>(1);
1053                                 }
1054                                 if (addr != null) {
1055                                     inetAddresses.add(InetAddress.getByAddress(host, addr));
1056                                 }
1057                             }
1058                         }
1059                     }
1060                 }
1061             } catch (FileNotFoundException e) {
1062                 throw new UnknownHostException("Unable to resolve host " + host
1063                         + " as hosts file " + hostsFile + " not found ");
1064             }
1065 
1066             if (inetAddresses != null) {
1067                 res = inetAddresses.toArray(new InetAddress[inetAddresses.size()]);
1068             } else {
1069                 throw new UnknownHostException("Unable to resolve host " + host




 992             String addrString = addrToString(addr);
 993             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
 994                 while (hostsFileScanner.hasNextLine()) {
 995                     hostEntry = hostsFileScanner.nextLine();
 996                     if (!hostEntry.startsWith("#")) {
 997                         hostEntry = removeComments(hostEntry);
 998                         if (hostEntry.contains(addrString)) {
 999                             host = extractHost(hostEntry, addrString);
1000                             if (host != null) {
1001                                 break;
1002                             }
1003                         }
1004                     }
1005                 }
1006             } catch (FileNotFoundException e) {
1007                 throw new UnknownHostException("Unable to resolve address "
1008                         + addrString + " as hosts file " + hostsFile
1009                         + " not found ");
1010             }
1011 
1012             if ((host == null) || (host.isEmpty()) || (host.equals(" "))) {
1013                 throw new UnknownHostException("Requested address "
1014                         + addrString
1015                         + " resolves to an invalid entry in hosts file "
1016                         + hostsFile);
1017             }
1018             return host;
1019         }
1020 
1021         /**
1022          * <p>Lookup a host mapping by name. Retrieve the IP addresses
1023          * associated with a host.
1024          *
1025          * <p>Search the configured hosts file for the addresses associated
1026          * with the specified host name.
1027          *
1028          * @param host the specified hostname
1029          * @return array of IP addresses for the requested host
1030          * @throws UnknownHostException
1031          *             if no IP address for the {@code host} could be found
1032          */
1033         public InetAddress[] lookupAllHostAddr(String host)
1034                 throws UnknownHostException {
1035             String hostEntry;
1036             String addrStr = null;
1037             InetAddress[] res = null;
1038             byte addr[] = new byte[4];
1039             ArrayList<InetAddress> inetAddresses = null;
1040 
1041             // lookup the file and create a list InetAddress for the specified host
1042             try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
1043                 while (hostsFileScanner.hasNextLine()) {
1044                     hostEntry = hostsFileScanner.nextLine();
1045                     if (!hostEntry.startsWith("#")) {
1046                         hostEntry = removeComments(hostEntry);
1047                         if (hostEntry.contains(host)) {
1048                             addrStr = extractHostAddr(hostEntry, host);
1049                             if ((addrStr != null) && (!addrStr.isEmpty())) {
1050                                 addr = createAddressByteArray(addrStr);
1051                                 if (inetAddresses == null) {
1052                                     inetAddresses = new ArrayList<>(1);
1053                                 }
1054                                 if (addr != null) {
1055                                     inetAddresses.add(InetAddress.getByAddress(host, addr));
1056                                 }
1057                             }
1058                         }
1059                     }
1060                 }
1061             } catch (FileNotFoundException e) {
1062                 throw new UnknownHostException("Unable to resolve host " + host
1063                         + " as hosts file " + hostsFile + " not found ");
1064             }
1065 
1066             if (inetAddresses != null) {
1067                 res = inetAddresses.toArray(new InetAddress[inetAddresses.size()]);
1068             } else {
1069                 throw new UnknownHostException("Unable to resolve host " + host


< prev index next >