< prev index next >

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

Print this page




1151      * @return a NameService
1152      */
1153     private static NameService createNameService() {
1154 
1155         String hostsFileName =
1156                 GetPropertyAction.privilegedGetProperty("jdk.net.hosts.file");
1157         NameService theNameService;
1158         if (hostsFileName != null) {
1159             theNameService = new HostsFileNameService(hostsFileName);
1160         } else {
1161             theNameService = new PlatformNameService();
1162         }
1163         return theNameService;
1164     }
1165 
1166     /**
1167      * Creates an InetAddress based on the provided host name and IP address.
1168      * No name service is checked for the validity of the address.
1169      *
1170      * <p> The host name can either be a machine name, such as
1171      * "{@code java.sun.com}", or a textual representation of its IP
1172      * address.
1173      * <p> No validity checking is done on the host name either.
1174      *
1175      * <p> If addr specifies an IPv4 address an instance of Inet4Address
1176      * will be returned; otherwise, an instance of Inet6Address
1177      * will be returned.
1178      *
1179      * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
1180      * must be 16 bytes long
1181      *
1182      * @param host the specified host
1183      * @param addr the raw IP address in network byte order
1184      * @return  an InetAddress object created from the raw IP address.
1185      * @exception  UnknownHostException  if IP address is of illegal length
1186      * @since 1.4
1187      */
1188     public static InetAddress getByAddress(String host, byte[] addr)
1189         throws UnknownHostException {
1190         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
1191             if (host.charAt(host.length()-1) == ']') {


1196             if (addr.length == Inet4Address.INADDRSZ) {
1197                 return new Inet4Address(host, addr);
1198             } else if (addr.length == Inet6Address.INADDRSZ) {
1199                 byte[] newAddr
1200                     = IPAddressUtil.convertFromIPv4MappedAddress(addr);
1201                 if (newAddr != null) {
1202                     return new Inet4Address(host, newAddr);
1203                 } else {
1204                     return new Inet6Address(host, addr);
1205                 }
1206             }
1207         }
1208         throw new UnknownHostException("addr is of illegal length");
1209     }
1210 
1211 
1212     /**
1213      * Determines the IP address of a host, given the host's name.
1214      *
1215      * <p> The host name can either be a machine name, such as
1216      * "{@code java.sun.com}", or a textual representation of its
1217      * IP address. If a literal IP address is supplied, only the
1218      * validity of the address format is checked.
1219      *
1220      * <p> For {@code host} specified in literal IPv6 address,
1221      * either the form defined in RFC 2732 or the literal IPv6 address
1222      * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
1223      * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
1224      * scoped addresses.
1225      *
1226      * <p> If the host is {@code null} or {@code host.length()} is equal
1227      * to zero, then an {@code InetAddress} representing an address of the
1228      * loopback interface is returned.
1229      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
1230      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
1231      * section&nbsp;2.5.3.
1232      *
1233      * <p> If there is a security manager, and {@code host} is not {@code null}
1234      * or {@code host.length() } is not equal to zero, the security manager's
1235      * {@code checkConnect} method is called with the hostname and {@code -1}
1236      * as its arguments to determine if the operation is allowed.


1242      *               for a global IPv6 address.
1243      * @exception  SecurityException if a security manager exists
1244      *             and its checkConnect method doesn't allow the operation
1245      */
1246     public static InetAddress getByName(String host)
1247         throws UnknownHostException {
1248         return InetAddress.getAllByName(host)[0];
1249     }
1250 
1251     // called from deployment cache manager
1252     private static InetAddress getByName(String host, InetAddress reqAddr)
1253         throws UnknownHostException {
1254         return InetAddress.getAllByName(host, reqAddr)[0];
1255     }
1256 
1257     /**
1258      * Given the name of a host, returns an array of its IP addresses,
1259      * based on the configured name service on the system.
1260      *
1261      * <p> The host name can either be a machine name, such as
1262      * "{@code java.sun.com}", or a textual representation of its IP
1263      * address. If a literal IP address is supplied, only the
1264      * validity of the address format is checked.
1265      *
1266      * <p> For {@code host} specified in <i>literal IPv6 address</i>,
1267      * either the form defined in RFC 2732 or the literal IPv6 address
1268      * format defined in RFC 2373 is accepted. A literal IPv6 address may
1269      * also be qualified by appending a scoped zone identifier or scope_id.
1270      * The syntax and usage of scope_ids is described
1271      * <a href="Inet6Address.html#scoped">here</a>.
1272      *
1273      * <p> If the host is {@code null} or {@code host.length()} is equal
1274      * to zero, then an {@code InetAddress} representing an address of the
1275      * loopback interface is returned.
1276      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
1277      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
1278      * section&nbsp;2.5.3. </p>
1279      *
1280      * <p> If there is a security manager, and {@code host} is not {@code null}
1281      * or {@code host.length() } is not equal to zero, the security manager's
1282      * {@code checkConnect} method is called with the hostname and {@code -1}




1151      * @return a NameService
1152      */
1153     private static NameService createNameService() {
1154 
1155         String hostsFileName =
1156                 GetPropertyAction.privilegedGetProperty("jdk.net.hosts.file");
1157         NameService theNameService;
1158         if (hostsFileName != null) {
1159             theNameService = new HostsFileNameService(hostsFileName);
1160         } else {
1161             theNameService = new PlatformNameService();
1162         }
1163         return theNameService;
1164     }
1165 
1166     /**
1167      * Creates an InetAddress based on the provided host name and IP address.
1168      * No name service is checked for the validity of the address.
1169      *
1170      * <p> The host name can either be a machine name, such as
1171      * "{@code www.example.com}", or a textual representation of its IP
1172      * address.
1173      * <p> No validity checking is done on the host name either.
1174      *
1175      * <p> If addr specifies an IPv4 address an instance of Inet4Address
1176      * will be returned; otherwise, an instance of Inet6Address
1177      * will be returned.
1178      *
1179      * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
1180      * must be 16 bytes long
1181      *
1182      * @param host the specified host
1183      * @param addr the raw IP address in network byte order
1184      * @return  an InetAddress object created from the raw IP address.
1185      * @exception  UnknownHostException  if IP address is of illegal length
1186      * @since 1.4
1187      */
1188     public static InetAddress getByAddress(String host, byte[] addr)
1189         throws UnknownHostException {
1190         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
1191             if (host.charAt(host.length()-1) == ']') {


1196             if (addr.length == Inet4Address.INADDRSZ) {
1197                 return new Inet4Address(host, addr);
1198             } else if (addr.length == Inet6Address.INADDRSZ) {
1199                 byte[] newAddr
1200                     = IPAddressUtil.convertFromIPv4MappedAddress(addr);
1201                 if (newAddr != null) {
1202                     return new Inet4Address(host, newAddr);
1203                 } else {
1204                     return new Inet6Address(host, addr);
1205                 }
1206             }
1207         }
1208         throw new UnknownHostException("addr is of illegal length");
1209     }
1210 
1211 
1212     /**
1213      * Determines the IP address of a host, given the host's name.
1214      *
1215      * <p> The host name can either be a machine name, such as
1216      * "{@code www.example.com}", or a textual representation of its
1217      * IP address. If a literal IP address is supplied, only the
1218      * validity of the address format is checked.
1219      *
1220      * <p> For {@code host} specified in literal IPv6 address,
1221      * either the form defined in RFC 2732 or the literal IPv6 address
1222      * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
1223      * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
1224      * scoped addresses.
1225      *
1226      * <p> If the host is {@code null} or {@code host.length()} is equal
1227      * to zero, then an {@code InetAddress} representing an address of the
1228      * loopback interface is returned.
1229      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
1230      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
1231      * section&nbsp;2.5.3.
1232      *
1233      * <p> If there is a security manager, and {@code host} is not {@code null}
1234      * or {@code host.length() } is not equal to zero, the security manager's
1235      * {@code checkConnect} method is called with the hostname and {@code -1}
1236      * as its arguments to determine if the operation is allowed.


1242      *               for a global IPv6 address.
1243      * @exception  SecurityException if a security manager exists
1244      *             and its checkConnect method doesn't allow the operation
1245      */
1246     public static InetAddress getByName(String host)
1247         throws UnknownHostException {
1248         return InetAddress.getAllByName(host)[0];
1249     }
1250 
1251     // called from deployment cache manager
1252     private static InetAddress getByName(String host, InetAddress reqAddr)
1253         throws UnknownHostException {
1254         return InetAddress.getAllByName(host, reqAddr)[0];
1255     }
1256 
1257     /**
1258      * Given the name of a host, returns an array of its IP addresses,
1259      * based on the configured name service on the system.
1260      *
1261      * <p> The host name can either be a machine name, such as
1262      * "{@code www.example.com}", or a textual representation of its IP
1263      * address. If a literal IP address is supplied, only the
1264      * validity of the address format is checked.
1265      *
1266      * <p> For {@code host} specified in <i>literal IPv6 address</i>,
1267      * either the form defined in RFC 2732 or the literal IPv6 address
1268      * format defined in RFC 2373 is accepted. A literal IPv6 address may
1269      * also be qualified by appending a scoped zone identifier or scope_id.
1270      * The syntax and usage of scope_ids is described
1271      * <a href="Inet6Address.html#scoped">here</a>.
1272      *
1273      * <p> If the host is {@code null} or {@code host.length()} is equal
1274      * to zero, then an {@code InetAddress} representing an address of the
1275      * loopback interface is returned.
1276      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC&nbsp;3330</a>
1277      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
1278      * section&nbsp;2.5.3. </p>
1279      *
1280      * <p> If there is a security manager, and {@code host} is not {@code null}
1281      * or {@code host.length() } is not equal to zero, the security manager's
1282      * {@code checkConnect} method is called with the hostname and {@code -1}


< prev index next >