From b1a49be45e5a93af8b9a6cdd505bd2aeabe6e0d7 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 18 Sep 2020 03:01:51 +0200 Subject: [PATCH] Changed getByName -> fromString (to fit with the rest of the API naming conventions). "" host for InetAddress is now (correctly), null --- build.gradle.kts | 2 +- src/dorkbox/netUtil/IP.kt | 6 +++--- src/dorkbox/netUtil/IPv4.kt | 10 +++++----- src/dorkbox/netUtil/IPv6.kt | 12 ++++++------ test/dorkbox/netUtil/NetUtilTest.kt | 10 +++++----- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 66bb4fe..75a1899 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,7 +43,7 @@ object Extras { // set for the project const val description = "Utilities for managing network configurations, IP/MAC address conversion, and ping (via OS native commands)" const val group = "com.dorkbox" - const val version = "1.5" + const val version = "2.0" // set as project.ext const val name = "NetworkUtils" diff --git a/src/dorkbox/netUtil/IP.kt b/src/dorkbox/netUtil/IP.kt index 1d53b6a..63f8454 100644 --- a/src/dorkbox/netUtil/IP.kt +++ b/src/dorkbox/netUtil/IP.kt @@ -256,11 +256,11 @@ object IP { * @param ip [CharSequence] IP address to be converted to a [InetAddress] * @return [InetAddress] representation of the `ip` or `null` if not a valid IP address. */ - fun getByName(ip: String): InetAddress? { + fun fromString(ip: String): InetAddress? { return if (IPv4.isValid(ip)) { - IPv4.getByNameUnsafe(ip) + IPv4.fromStringUnsafe(ip) } else { - IPv6.getByName(ip) + IPv6.fromString(ip) } } } diff --git a/src/dorkbox/netUtil/IPv4.kt b/src/dorkbox/netUtil/IPv4.kt index 7f9c4f8..2f279a6 100644 --- a/src/dorkbox/netUtil/IPv4.kt +++ b/src/dorkbox/netUtil/IPv4.kt @@ -100,7 +100,7 @@ object IPv4 { */ val WILDCARD: Inet4Address by lazy { // Create IPv4 address, this will ALWAYS work - InetAddress.getByAddress("", byteArrayOf(0, 0, 0, 0)) as Inet4Address + InetAddress.getByAddress(null, byteArrayOf(0, 0, 0, 0)) as Inet4Address } /** @@ -782,9 +782,9 @@ object IPv4 { * @param ip [String] IP address to be converted to a [Inet4Address] * @return [Inet4Address] representation of the `ip` or `null` if not a valid IP address. */ - fun getByNameUnsafe(ip: String): Inet4Address { + fun fromStringUnsafe(ip: String): Inet4Address { val asBytes = toBytes(ip) - return Inet4Address.getByAddress(ip, asBytes) as Inet4Address + return Inet4Address.getByAddress(null, asBytes) as Inet4Address } /** @@ -795,9 +795,9 @@ object IPv4 { * @param ip [String] IP address to be converted to a [Inet4Address] * @return [Inet4Address] representation of the `ip` or `null` if not a valid IP address. */ - fun getByName(ip: String): Inet4Address? { + fun fromString(ip: String): Inet4Address? { return if (isValid(ip)) { - return getByNameUnsafe(ip) + return fromStringUnsafe(ip) } else { null } diff --git a/src/dorkbox/netUtil/IPv6.kt b/src/dorkbox/netUtil/IPv6.kt index a3ab24e..d6f29f1 100644 --- a/src/dorkbox/netUtil/IPv6.kt +++ b/src/dorkbox/netUtil/IPv6.kt @@ -135,7 +135,7 @@ object IPv6 { */ val WILDCARD: Inet6Address by lazy { // Create IPv6 address, this will ALWAYS work - InetAddress.getByAddress("", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) as Inet6Address + InetAddress.getByAddress(null, byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) as Inet6Address } /** @@ -272,8 +272,8 @@ object IPv6 { * @param ip [CharSequence] IP address to be converted to a [Inet6Address] * @return [Inet6Address] representation of the `ip` or `null` if not a valid IP address. */ - fun getByName(ip: String): Inet6Address? { - return getByName(ip, true) + fun fromString(ip: String): Inet6Address? { + return fromString(ip, true) } @@ -291,7 +291,7 @@ object IPv6 { * * @return [Inet6Address] representation of the [ip] or [null] if not a valid IP address. */ - fun getByName(ip: String, ipv4Mapped: Boolean): Inet6Address? { + fun fromString(ip: String, ipv4Mapped: Boolean): Inet6Address? { val bytes = getIPv6ByName(ip, ipv4Mapped) ?: return null return try { Inet6Address.getByAddress(null, bytes, -1) @@ -520,7 +520,7 @@ object IPv6 { */ fun toBytesOrNull(ipAddress: String): ByteArray? { if (isValid(ipAddress)) { - return getByName(ipAddress)?.address + return fromString(ipAddress)?.address } return null @@ -539,7 +539,7 @@ object IPv6 { fixedIp = fixedIp.substring(0, percentPos) } - return getByName(fixedIp)?.address ?: ByteArray(32) + return fromString(fixedIp)?.address ?: ByteArray(32) } diff --git a/test/dorkbox/netUtil/NetUtilTest.kt b/test/dorkbox/netUtil/NetUtilTest.kt index b7bc035..76d6d3c 100644 --- a/test/dorkbox/netUtil/NetUtilTest.kt +++ b/test/dorkbox/netUtil/NetUtilTest.kt @@ -608,7 +608,7 @@ class NetUtilTest { for (host in validIpV6Hosts.keys) { Assert.assertTrue(host, IPv6.isValid(host)) if (host[0] != '[' && !host.contains("%")) { - Assert.assertNotNull(host, IPv6.getByName(host, true)) + Assert.assertNotNull(host, IPv6.fromString(host, true)) var hostMod = "[$host]" Assert.assertTrue(hostMod, IPv6.isValid(hostMod)) hostMod = "$host%" @@ -627,7 +627,7 @@ class NetUtilTest { } for (host in invalidIpV6Hosts.keys) { Assert.assertFalse(host, IPv6.isValid(host)) - Assert.assertNull(host, IPv6.getByName(host)) + Assert.assertNull(host, IPv6.fromString(host)) var hostMod = "[$host]" Assert.assertFalse(hostMod, IPv6.isValid(hostMod)) hostMod = "$host%" @@ -696,7 +696,7 @@ class NetUtilTest { @Test fun testIpv4MappedIp6GetByName() { for ((srcIp, dstIp) in ipv4MappedToIPv6AddressStrings) { - val inet6Address: Inet6Address? = IPv6.getByName(srcIp, true) + val inet6Address: Inet6Address? = IPv6.fromString(srcIp, true) Assert.assertNotNull("$srcIp, $dstIp", inet6Address) assertEquals(srcIp, dstIp, IPv6.toString(inet6Address!!, true)) } @@ -705,10 +705,10 @@ class NetUtilTest { @Test fun testInvalidIpv4MappedIp6GetByName() { for (host in invalidIpV4Hosts.keys) { - Assert.assertNull(host, IPv4.getByName(host)) + Assert.assertNull(host, IPv4.fromString(host)) } for (host in invalidIpV6Hosts.keys) { - Assert.assertNull(host, IPv6.getByName(host, true)) + Assert.assertNull(host, IPv6.fromString(host, true)) } }