< prev index next >

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

Print this page




 360      * object could not be created
 361      */
 362     private Object readResolve() throws ObjectStreamException {
 363         // will replace the deserialized 'this' object
 364         return new Inet4Address(holder().getHostName(), holder().getAddress());
 365     }
 366 
 367     /**
 368      * Utility routine to check if the InetAddress is an
 369      * IP multicast address.
 370      * @return a {@code boolean} indicating if the InetAddress is
 371      * an IP multicast address
 372      * @since   1.1
 373      */
 374     public boolean isMulticastAddress() {
 375         return false;
 376     }
 377 
 378     /**
 379      * Utility routine to check if the InetAddress is a wildcard address.
 380      * @return a {@code boolean} indicating if the Inetaddress is
 381      *         a wildcard address.
 382      * @since 1.4
 383      */
 384     public boolean isAnyLocalAddress() {
 385         return false;
 386     }
 387 
 388     /**
 389      * Utility routine to check if the InetAddress is a loopback address.
 390      *
 391      * @return a {@code boolean} indicating if the InetAddress is
 392      * a loopback address; or false otherwise.
 393      * @since 1.4
 394      */
 395     public boolean isLoopbackAddress() {
 396         return false;
 397     }
 398 
 399     /**
 400      * Utility routine to check if the InetAddress is an link local address.


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 assocaited with
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 specfied 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) {


1324             || (host.charAt(0) == ':')) {
1325             byte[] addr = null;
1326             int numericZone = -1;
1327             String ifname = null;
1328             // see if it is IPv4 address
1329             addr = IPAddressUtil.textToNumericFormatV4(host);
1330             if (addr == null) {
1331                 // This is supposed to be an IPv6 literal
1332                 // Check if a numeric or string zone id is present
1333                 int pos;
1334                 if ((pos=host.indexOf ('%')) != -1) {
1335                     numericZone = checkNumericZone (host);
1336                     if (numericZone == -1) { /* remainder of string must be an ifname */
1337                         ifname = host.substring (pos+1);
1338                     }
1339                 }
1340                 if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) {
1341                     throw new UnknownHostException(host + ": invalid IPv6 address");
1342                 }
1343             } else if (ipv6Expected) {
1344                 // Means an IPv4 litteral between brackets!
1345                 throw new UnknownHostException("["+host+"]");
1346             }
1347             InetAddress[] ret = new InetAddress[1];
1348             if(addr != null) {
1349                 if (addr.length == Inet4Address.INADDRSZ) {
1350                     ret[0] = new Inet4Address(null, addr);
1351                 } else {
1352                     if (ifname != null) {
1353                         ret[0] = new Inet6Address(null, addr, ifname);
1354                     } else {
1355                         ret[0] = new Inet6Address(null, addr, numericZone);
1356                     }
1357                 }
1358                 return ret;
1359             }
1360         } else if (ipv6Expected) {
1361             // We were expecting an IPv6 Litteral, but got something else
1362             throw new UnknownHostException("["+host+"]");
1363         }
1364         return getAllByName0(host, reqAddr, true, true);
1365     }
1366 
1367     /**
1368      * Returns the loopback address.
1369      * <p>
1370      * The InetAddress returned will represent the IPv4
1371      * loopback address, 127.0.0.1, or the IPv6 loopback
1372      * address, ::1. The IPv4 loopback address returned
1373      * is only one of many in the form 127.*.*.*
1374      *
1375      * @return  the InetAddress loopback instance.
1376      * @since 1.7
1377      */
1378     public static InetAddress getLoopbackAddress() {
1379         return impl.loopbackAddress();
1380     }
1381 




 360      * object could not be created
 361      */
 362     private Object readResolve() throws ObjectStreamException {
 363         // will replace the deserialized 'this' object
 364         return new Inet4Address(holder().getHostName(), holder().getAddress());
 365     }
 366 
 367     /**
 368      * Utility routine to check if the InetAddress is an
 369      * IP multicast address.
 370      * @return a {@code boolean} indicating if the InetAddress is
 371      * an IP multicast address
 372      * @since   1.1
 373      */
 374     public boolean isMulticastAddress() {
 375         return false;
 376     }
 377 
 378     /**
 379      * Utility routine to check if the InetAddress is a wildcard address.
 380      * @return a {@code boolean} indicating if the InetAddress is
 381      *         a wildcard address.
 382      * @since 1.4
 383      */
 384     public boolean isAnyLocalAddress() {
 385         return false;
 386     }
 387 
 388     /**
 389      * Utility routine to check if the InetAddress is a loopback address.
 390      *
 391      * @return a {@code boolean} indicating if the InetAddress is
 392      * a loopback address; or false otherwise.
 393      * @since 1.4
 394      */
 395     public boolean isLoopbackAddress() {
 396         return false;
 397     }
 398 
 399     /**
 400      * Utility routine to check if the InetAddress is an link local address.


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) {


1324             || (host.charAt(0) == ':')) {
1325             byte[] addr = null;
1326             int numericZone = -1;
1327             String ifname = null;
1328             // see if it is IPv4 address
1329             addr = IPAddressUtil.textToNumericFormatV4(host);
1330             if (addr == null) {
1331                 // This is supposed to be an IPv6 literal
1332                 // Check if a numeric or string zone id is present
1333                 int pos;
1334                 if ((pos=host.indexOf ('%')) != -1) {
1335                     numericZone = checkNumericZone (host);
1336                     if (numericZone == -1) { /* remainder of string must be an ifname */
1337                         ifname = host.substring (pos+1);
1338                     }
1339                 }
1340                 if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) {
1341                     throw new UnknownHostException(host + ": invalid IPv6 address");
1342                 }
1343             } else if (ipv6Expected) {
1344                 // Means an IPv4 literal between brackets!
1345                 throw new UnknownHostException("["+host+"]");
1346             }
1347             InetAddress[] ret = new InetAddress[1];
1348             if(addr != null) {
1349                 if (addr.length == Inet4Address.INADDRSZ) {
1350                     ret[0] = new Inet4Address(null, addr);
1351                 } else {
1352                     if (ifname != null) {
1353                         ret[0] = new Inet6Address(null, addr, ifname);
1354                     } else {
1355                         ret[0] = new Inet6Address(null, addr, numericZone);
1356                     }
1357                 }
1358                 return ret;
1359             }
1360         } else if (ipv6Expected) {
1361             // We were expecting an IPv6 Literal, but got something else
1362             throw new UnknownHostException("["+host+"]");
1363         }
1364         return getAllByName0(host, reqAddr, true, true);
1365     }
1366 
1367     /**
1368      * Returns the loopback address.
1369      * <p>
1370      * The InetAddress returned will represent the IPv4
1371      * loopback address, 127.0.0.1, or the IPv6 loopback
1372      * address, ::1. The IPv4 loopback address returned
1373      * is only one of many in the form 127.*.*.*
1374      *
1375      * @return  the InetAddress loopback instance.
1376      * @since 1.7
1377      */
1378     public static InetAddress getLoopbackAddress() {
1379         return impl.loopbackAddress();
1380     }
1381 


< prev index next >