diff options
author | Samuel Holland <samuel@sholland.org> | 2018-06-19 00:43:49 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-06-19 21:59:44 -0500 |
commit | b9991e4229b49e08beebbae5519cfed5de99c051 (patch) | |
tree | 4a233404b0b910539b149fbbda97f689ec31d6bc /app/src/main/java/com/wireguard/config/InetAddresses.java | |
parent | util: Extract non-Android utility interfaces (diff) | |
download | wireguard-android-b9991e4229b49e08beebbae5519cfed5de99c051.tar.xz wireguard-android-b9991e4229b49e08beebbae5519cfed5de99c051.zip |
config: Refactor IPCidr and use of InetAddress
Use a canonically-named utility class to tack on methods to the existing
InetAddress class. Rename IPCidr to InetNetwork so it better matches
InetAddress and is more pronouceable :) While here, simplify the
constructor and toString() functions, and properly implement hashCode().
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config/InetAddresses.java')
-rw-r--r-- | app/src/main/java/com/wireguard/config/InetAddresses.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/config/InetAddresses.java b/app/src/main/java/com/wireguard/config/InetAddresses.java new file mode 100644 index 00000000..d351d53c --- /dev/null +++ b/app/src/main/java/com/wireguard/config/InetAddresses.java @@ -0,0 +1,42 @@ +/* + * Copyright © 2018 Samuel Holland <samuel@sholland.org> + * Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +package com.wireguard.config; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.InetAddress; + +public final class InetAddresses { + private static final Method PARSER_METHOD; + + static { + try { + // This method is only present on Android. + PARSER_METHOD = InetAddress.class.getMethod("parseNumericAddress", String.class); + } catch (final NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + private InetAddresses() { + // Prevent instantiation. + } + + @NonNull + public static InetAddress parse(@Nullable final String address) { + if (address == null || address.isEmpty()) + throw new IllegalArgumentException("Empty address"); + try { + return (InetAddress) PARSER_METHOD.invoke(null, address); + } catch (final IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } +} |