1 /* 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 17 package android.net; 18 19 import android.annotation.NonNull; 20 21 import libcore.net.InetAddressUtils; 22 23 import java.net.InetAddress; 24 25 /** 26 * Utility methods for {@link InetAddress} implementations. 27 */ 28 public class InetAddresses { 29 InetAddresses()30 private InetAddresses() {} 31 32 /** 33 * Checks to see if the {@code address} is a numeric address (such as {@code "192.0.2.1"} or 34 * {@code "2001:db8::1:2"}). 35 * 36 * <p>A numeric address is either an IPv4 address containing exactly 4 decimal numbers or an 37 * IPv6 numeric address. IPv4 addresses that consist of either hexadecimal or octal digits or 38 * do not have exactly 4 numbers are not treated as numeric. 39 * 40 * <p>This method will never do a DNS lookup. 41 * 42 * @param address the address to parse. 43 * @return true if the supplied address is numeric, false otherwise. 44 */ isNumericAddress(@onNull String address)45 public static boolean isNumericAddress(@NonNull String address) { 46 return InetAddressUtils.isNumericAddress(address); 47 } 48 49 /** 50 * Returns an InetAddress corresponding to the given numeric address (such 51 * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}). 52 * 53 * <p>See {@link #isNumericAddress(String)} (String)} for a definition as to what constitutes a 54 * numeric address. 55 * 56 * <p>This method will never do a DNS lookup. 57 * 58 * @param address the address to parse, must be numeric. 59 * @return an {@link InetAddress} instance corresponding to the address. 60 * @throws IllegalArgumentException if {@code address} is not a numeric address. 61 */ parseNumericAddress(@onNull String address)62 public static @NonNull InetAddress parseNumericAddress(@NonNull String address) { 63 return InetAddressUtils.parseNumericAddress(address); 64 } 65 } 66