blob: 8e6c69a97edb623d77dd3f55bf172c9a5f6c61ea [file] [log] [blame]
Paul Duffina2b34f82018-10-11 15:21:46 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import libcore.net.InetAddressUtils;
20
21import java.net.InetAddress;
22
23/**
24 * Utility methods for {@link InetAddress} implementations.
25 */
26public class InetAddresses {
27
28 private InetAddresses() {}
29
30 /**
31 * Checks to see if the {@code address} is a numeric address (such as {@code "192.0.2.1"} or
32 * {@code "2001:db8::1:2"}).
33 *
34 * <p>A numeric address is either an IPv4 address containing exactly 4 decimal numbers or an
35 * IPv6 numeric address. IPv4 addresses that consist of either hexadecimal or octal digits or
36 * do not have exactly 4 numbers are not treated as numeric.
37 *
38 * <p>This method will never do a DNS lookup.
39 *
40 * @param address the address to parse.
41 * @return true if the supplied address is numeric, false otherwise.
42 */
43 public static boolean isNumericAddress(String address) {
44 return InetAddressUtils.isNumericAddress(address);
45 }
46
47 /**
48 * Returns an InetAddress corresponding to the given numeric address (such
49 * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}).
50 *
51 * <p>See {@link #isNumericAddress(String)} (String)} for a definition as to what constitutes a
52 * numeric address.
53 *
54 * <p>This method will never do a DNS lookup.
55 *
56 * @param address the address to parse, must be numeric.
57 * @return an {@link InetAddress} instance corresponding to the address.
58 * @throws IllegalArgumentException if {@code address} is not a numeric address.
59 */
60 public static InetAddress parseNumericAddress(String address) {
61 return InetAddressUtils.parseNumericAddress(address);
62 }
63}