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) == ']') {
1192 host = host.substring(1, host.length() -1);
1193 }
1194 }
1195 if (addr != null) {
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
1284 *
1285 * @param host the name of the host, or {@code null}.
1286 * @return an array of all the IP addresses for a given host name.
1287 *
1288 * @exception UnknownHostException if no IP address for the
1289 * {@code host} could be found, or if a scope_id was specified
1290 * for a global IPv6 address.
1291 * @exception SecurityException if a security manager exists and its
1292 * {@code checkConnect} method doesn't allow the operation.
1293 *
1294 * @see SecurityManager#checkConnect
1295 */
1296 public static InetAddress[] getAllByName(String host)
1297 throws UnknownHostException {
1298 return getAllByName(host, null);
1299 }
1300
1301 private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
1302 throws UnknownHostException {
1303
1304 if (host == null || host.length() == 0) {
1305 InetAddress[] ret = new InetAddress[1];
1306 ret[0] = impl.loopbackAddress();
1307 return ret;
1308 }
1309
1310 boolean ipv6Expected = false;
1311 if (host.charAt(0) == '[') {
1312 // This is supposed to be an IPv6 literal
1313 if (host.length() > 2 && host.charAt(host.length()-1) == ']') {
1314 host = host.substring(1, host.length() -1);
1315 ipv6Expected = true;
1316 } else {
1317 // This was supposed to be a IPv6 address, but it's not!
1318 throw new UnknownHostException(host + ": invalid IPv6 address");
1319 }
1320 }
1321
1322 // if host is an IP address, we won't do further lookup
1323 if (Character.digit(host.charAt(0), 16) != -1
1324 || (host.charAt(0) == ':')) {
|
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.isEmpty() && host.charAt(0) == '[') {
1191 if (host.charAt(host.length()-1) == ']') {
1192 host = host.substring(1, host.length() -1);
1193 }
1194 }
1195 if (addr != null) {
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
1284 *
1285 * @param host the name of the host, or {@code null}.
1286 * @return an array of all the IP addresses for a given host name.
1287 *
1288 * @exception UnknownHostException if no IP address for the
1289 * {@code host} could be found, or if a scope_id was specified
1290 * for a global IPv6 address.
1291 * @exception SecurityException if a security manager exists and its
1292 * {@code checkConnect} method doesn't allow the operation.
1293 *
1294 * @see SecurityManager#checkConnect
1295 */
1296 public static InetAddress[] getAllByName(String host)
1297 throws UnknownHostException {
1298 return getAllByName(host, null);
1299 }
1300
1301 private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
1302 throws UnknownHostException {
1303
1304 if (host == null || host.isEmpty()) {
1305 InetAddress[] ret = new InetAddress[1];
1306 ret[0] = impl.loopbackAddress();
1307 return ret;
1308 }
1309
1310 boolean ipv6Expected = false;
1311 if (host.charAt(0) == '[') {
1312 // This is supposed to be an IPv6 literal
1313 if (host.length() > 2 && host.charAt(host.length()-1) == ']') {
1314 host = host.substring(1, host.length() -1);
1315 ipv6Expected = true;
1316 } else {
1317 // This was supposed to be a IPv6 address, but it's not!
1318 throw new UnknownHostException(host + ": invalid IPv6 address");
1319 }
1320 }
1321
1322 // if host is an IP address, we won't do further lookup
1323 if (Character.digit(host.charAt(0), 16) != -1
1324 || (host.charAt(0) == ':')) {
|