Changed getByName -> fromString (to fit with the rest of the API naming conventions). "" host for InetAddress is now (correctly), null

This commit is contained in:
nathan 2020-09-18 03:01:51 +02:00
parent 0ea4ed19b2
commit b1a49be45e
5 changed files with 20 additions and 20 deletions

View File

@ -43,7 +43,7 @@ object Extras {
// set for the project // set for the project
const val description = "Utilities for managing network configurations, IP/MAC address conversion, and ping (via OS native commands)" 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 group = "com.dorkbox"
const val version = "1.5" const val version = "2.0"
// set as project.ext // set as project.ext
const val name = "NetworkUtils" const val name = "NetworkUtils"

View File

@ -256,11 +256,11 @@ object IP {
* @param ip [CharSequence] IP address to be converted to a [InetAddress] * @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. * @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)) { return if (IPv4.isValid(ip)) {
IPv4.getByNameUnsafe(ip) IPv4.fromStringUnsafe(ip)
} else { } else {
IPv6.getByName(ip) IPv6.fromString(ip)
} }
} }
} }

View File

@ -100,7 +100,7 @@ object IPv4 {
*/ */
val WILDCARD: Inet4Address by lazy { val WILDCARD: Inet4Address by lazy {
// Create IPv4 address, this will ALWAYS work // 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] * @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. * @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) 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] * @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. * @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 if (isValid(ip)) {
return getByNameUnsafe(ip) return fromStringUnsafe(ip)
} else { } else {
null null
} }

View File

@ -135,7 +135,7 @@ object IPv6 {
*/ */
val WILDCARD: Inet6Address by lazy { val WILDCARD: Inet6Address by lazy {
// Create IPv6 address, this will ALWAYS work // 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] * @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. * @return [Inet6Address] representation of the `ip` or `null` if not a valid IP address.
*/ */
fun getByName(ip: String): Inet6Address? { fun fromString(ip: String): Inet6Address? {
return getByName(ip, true) return fromString(ip, true)
} }
@ -291,7 +291,7 @@ object IPv6 {
* </ul> * </ul>
* @return [Inet6Address] representation of the [ip] or [null] if not a valid IP address. * @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 val bytes = getIPv6ByName(ip, ipv4Mapped) ?: return null
return try { return try {
Inet6Address.getByAddress(null, bytes, -1) Inet6Address.getByAddress(null, bytes, -1)
@ -520,7 +520,7 @@ object IPv6 {
*/ */
fun toBytesOrNull(ipAddress: String): ByteArray? { fun toBytesOrNull(ipAddress: String): ByteArray? {
if (isValid(ipAddress)) { if (isValid(ipAddress)) {
return getByName(ipAddress)?.address return fromString(ipAddress)?.address
} }
return null return null
@ -539,7 +539,7 @@ object IPv6 {
fixedIp = fixedIp.substring(0, percentPos) fixedIp = fixedIp.substring(0, percentPos)
} }
return getByName(fixedIp)?.address ?: ByteArray(32) return fromString(fixedIp)?.address ?: ByteArray(32)
} }

View File

@ -608,7 +608,7 @@ class NetUtilTest {
for (host in validIpV6Hosts.keys) { for (host in validIpV6Hosts.keys) {
Assert.assertTrue(host, IPv6.isValid(host)) Assert.assertTrue(host, IPv6.isValid(host))
if (host[0] != '[' && !host.contains("%")) { if (host[0] != '[' && !host.contains("%")) {
Assert.assertNotNull(host, IPv6.getByName(host, true)) Assert.assertNotNull(host, IPv6.fromString(host, true))
var hostMod = "[$host]" var hostMod = "[$host]"
Assert.assertTrue(hostMod, IPv6.isValid(hostMod)) Assert.assertTrue(hostMod, IPv6.isValid(hostMod))
hostMod = "$host%" hostMod = "$host%"
@ -627,7 +627,7 @@ class NetUtilTest {
} }
for (host in invalidIpV6Hosts.keys) { for (host in invalidIpV6Hosts.keys) {
Assert.assertFalse(host, IPv6.isValid(host)) Assert.assertFalse(host, IPv6.isValid(host))
Assert.assertNull(host, IPv6.getByName(host)) Assert.assertNull(host, IPv6.fromString(host))
var hostMod = "[$host]" var hostMod = "[$host]"
Assert.assertFalse(hostMod, IPv6.isValid(hostMod)) Assert.assertFalse(hostMod, IPv6.isValid(hostMod))
hostMod = "$host%" hostMod = "$host%"
@ -696,7 +696,7 @@ class NetUtilTest {
@Test @Test
fun testIpv4MappedIp6GetByName() { fun testIpv4MappedIp6GetByName() {
for ((srcIp, dstIp) in ipv4MappedToIPv6AddressStrings) { for ((srcIp, dstIp) in ipv4MappedToIPv6AddressStrings) {
val inet6Address: Inet6Address? = IPv6.getByName(srcIp, true) val inet6Address: Inet6Address? = IPv6.fromString(srcIp, true)
Assert.assertNotNull("$srcIp, $dstIp", inet6Address) Assert.assertNotNull("$srcIp, $dstIp", inet6Address)
assertEquals(srcIp, dstIp, IPv6.toString(inet6Address!!, true)) assertEquals(srcIp, dstIp, IPv6.toString(inet6Address!!, true))
} }
@ -705,10 +705,10 @@ class NetUtilTest {
@Test @Test
fun testInvalidIpv4MappedIp6GetByName() { fun testInvalidIpv4MappedIp6GetByName() {
for (host in invalidIpV4Hosts.keys) { for (host in invalidIpV4Hosts.keys) {
Assert.assertNull(host, IPv4.getByName(host)) Assert.assertNull(host, IPv4.fromString(host))
} }
for (host in invalidIpV6Hosts.keys) { for (host in invalidIpV6Hosts.keys) {
Assert.assertNull(host, IPv6.getByName(host, true)) Assert.assertNull(host, IPv6.fromString(host, true))
} }
} }