blob: 8404441de66922a05a2b0b8f82e92890c403259e [file] [log] [blame]
Remi NGUYEN VANe1b04f62021-03-18 23:27:19 +09001/*
2 * Copyright (C) 2021 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 android.util.Log;
20
21import java.lang.reflect.InvocationTargetException;
22import java.net.InetAddress;
23import java.net.UnknownHostException;
24
25/**
26 * Compatibility utility for InetAddress core platform APIs.
27 *
28 * Connectivity has access to such APIs, but they are not part of the module_current stubs yet
29 * (only core_current). Most stable core platform APIs are included manually in the connectivity
30 * build rules, but because InetAddress is also part of the base java SDK that is earlier on the
31 * classpath, the extra core platform APIs are not seen.
32 *
33 * TODO (b/183097033): remove this utility as soon as core_current is part of module_current
34 * @hide
35 */
36public class InetAddressCompat {
37
38 /**
39 * @see InetAddress#clearDnsCache()
40 */
41 public static void clearDnsCache() {
42 try {
43 InetAddress.class.getMethod("clearDnsCache").invoke(null);
44 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
45 Log.wtf(InetAddressCompat.class.getSimpleName(), "Error clearing DNS cache", e);
46 }
47 }
48
49 /**
50 * @see InetAddress#getAllByNameOnNet(String, int)
51 */
52 public static InetAddress[] getAllByNameOnNet(String host, int netId) throws
53 UnknownHostException {
54 try {
55 return (InetAddress[]) InetAddress.class.getMethod("getAllByNameOnNet",
56 String.class, int.class).invoke(null, host, netId);
57 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
58 Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e);
59 throw new IllegalStateException("Error querying via getAllNameOnNet", e);
60 }
61 }
62
63 /**
64 * @see InetAddress#getByNameOnNet(String, int)
65 */
66 public static InetAddress getByNameOnNet(String host, int netId) throws
67 UnknownHostException {
68 try {
69 return (InetAddress) InetAddress.class.getMethod("getByNameOnNet",
70 String.class, int.class).invoke(null, host, netId);
71 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
72 Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e);
73 throw new IllegalStateException("Error querying via getByNameOnNet", e);
74 }
75 }
76}