Removed fromString, changed to toAddress
parent
540d134762
commit
d8ec62c680
|
@ -148,7 +148,8 @@ object IP {
|
|||
*
|
||||
*
|
||||
* If this method cannot find a non-loopback address using this selection algorithm, it will fall back to
|
||||
* calling and returning the result of JDK method `InetAddress.getLocalHost`.
|
||||
* making an HTTP call and checking the address, and if that fails, by then calling and returning the result
|
||||
* of JDK method `InetAddress.getLocalHost`.
|
||||
*
|
||||
*
|
||||
* @throws UnknownHostException If the LAN address of the machine cannot be found.
|
||||
|
@ -323,7 +324,6 @@ object IP {
|
|||
* [rfc 5952 section 4](http://tools.ietf.org/html/rfc5952#section-4)
|
||||
*
|
||||
*
|
||||
*
|
||||
* The output does not include Scope ID.
|
||||
*
|
||||
* @param ip [InetAddress] to be converted to an address string
|
||||
|
@ -402,11 +402,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 fromString(ip: String): InetAddress? {
|
||||
fun toAddress(ip: String): InetAddress? {
|
||||
return if (IPv4.isValid(ip)) {
|
||||
IPv4.fromStringUnsafe(ip)
|
||||
IPv4.toAddressUnsafe(ip)
|
||||
} else {
|
||||
IPv6.fromString(ip)
|
||||
IPv6.toAddress(ip)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -318,24 +318,6 @@ object IPv4 {
|
|||
return intArrayOf(a, b, c, ipv4WordToInt(ip, i + 1, ip.length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet4Address based on an ip string. No error handling is performed here.
|
||||
*/
|
||||
fun toAddress(ip: String): Inet4Address {
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet4Address
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet4Address based on an ip string. No error handling is performed here.
|
||||
*/
|
||||
fun toAddressOrNull(ip: String): Inet4Address? {
|
||||
if (!isValid(ip)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet4Address
|
||||
}
|
||||
|
||||
private fun decimalDigit(str: CharSequence, pos: Int): Int {
|
||||
return str[pos] - '0'
|
||||
}
|
||||
|
@ -836,7 +818,7 @@ 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 fromStringUnsafe(ip: String): Inet4Address {
|
||||
fun toAddressUnsafe(ip: String): Inet4Address {
|
||||
val asBytes = toBytes(ip)
|
||||
return Inet4Address.getByAddress(null, asBytes) as Inet4Address
|
||||
}
|
||||
|
@ -849,9 +831,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 fromString(ip: String): Inet4Address? {
|
||||
fun toAddress(ip: String): Inet4Address? {
|
||||
return if (isValid(ip)) {
|
||||
return fromStringUnsafe(ip)
|
||||
return toAddressUnsafe(ip)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
|
|
@ -288,8 +288,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 fromString(ip: String): Inet6Address? {
|
||||
return fromString(ip, true)
|
||||
fun toAddress(ip: String): Inet6Address? {
|
||||
return toAddress(ip, true)
|
||||
}
|
||||
|
||||
|
||||
|
@ -307,7 +307,7 @@ object IPv6 {
|
|||
* </ul>
|
||||
* @return [Inet6Address] representation of the [ip] or [null] if not a valid IP address.
|
||||
*/
|
||||
fun fromString(ip: String, ipv4Mapped: Boolean): Inet6Address? {
|
||||
fun toAddress(ip: String, ipv4Mapped: Boolean): Inet6Address? {
|
||||
val bytes = getIPv6ByName(ip, ipv4Mapped) ?: return null
|
||||
return try {
|
||||
Inet6Address.getByAddress(null, bytes, -1)
|
||||
|
@ -536,7 +536,7 @@ object IPv6 {
|
|||
*/
|
||||
fun toBytesOrNull(ipAddress: String): ByteArray? {
|
||||
if (isValid(ipAddress)) {
|
||||
return fromString(ipAddress)?.address
|
||||
return toAddress(ipAddress)?.address
|
||||
}
|
||||
|
||||
return null
|
||||
|
@ -556,7 +556,7 @@ object IPv6 {
|
|||
fixedIp = fixedIp.substring(0, percentPos)
|
||||
}
|
||||
|
||||
return fromString(fixedIp)?.address ?: ByteArray(32)
|
||||
return toAddress(fixedIp)?.address ?: ByteArray(32)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -585,24 +585,6 @@ object IPv6 {
|
|||
return BigInteger(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet6Address based on an ipAddressString.
|
||||
*/
|
||||
fun toAddressOrNull(ip: String): Inet6Address? {
|
||||
if (!isValid(ip)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return toAddress(ip)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet6Address based on an ipAddressString. No error handling is performed here.
|
||||
*/
|
||||
fun toAddress(ip: String): Inet6Address {
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet6Address
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the [String] representation of an [InetAddress].
|
||||
*
|
||||
|
|
|
@ -618,7 +618,7 @@ class IpValidationTests {
|
|||
for (host in validIpV6Hosts.keys) {
|
||||
Assert.assertTrue(host, IPv6.isValid(host))
|
||||
if (host[0] != '[' && !host.contains("%")) {
|
||||
Assert.assertNotNull(host, IPv6.fromString(host, true))
|
||||
Assert.assertNotNull(host, IPv6.toAddress(host, true))
|
||||
var hostMod = "[$host]"
|
||||
Assert.assertTrue(hostMod, IPv6.isValid(hostMod))
|
||||
hostMod = "$host%"
|
||||
|
@ -637,7 +637,7 @@ class IpValidationTests {
|
|||
}
|
||||
for (host in invalidIpV6Hosts.keys) {
|
||||
Assert.assertFalse(host, IPv6.isValid(host))
|
||||
Assert.assertNull(host, IPv6.fromString(host))
|
||||
Assert.assertNull(host, IPv6.toAddress(host))
|
||||
var hostMod = "[$host]"
|
||||
Assert.assertFalse(hostMod, IPv6.isValid(hostMod))
|
||||
hostMod = "$host%"
|
||||
|
@ -706,7 +706,7 @@ class IpValidationTests {
|
|||
@Test
|
||||
fun testIpv4MappedIp6GetByName() {
|
||||
for ((srcIp, dstIp) in ipv4MappedToIPv6AddressStrings) {
|
||||
val inet6Address: Inet6Address? = IPv6.fromString(srcIp, true)
|
||||
val inet6Address: Inet6Address? = IPv6.toAddress(srcIp, true)
|
||||
Assert.assertNotNull("$srcIp, $dstIp", inet6Address)
|
||||
assertEquals(srcIp, dstIp, IPv6.toString(inet6Address!!, true))
|
||||
}
|
||||
|
@ -715,10 +715,10 @@ class IpValidationTests {
|
|||
@Test
|
||||
fun testInvalidIpv4MappedIp6GetByName() {
|
||||
for (host in invalidIpV4Hosts.keys) {
|
||||
Assert.assertNull(host, IPv4.fromString(host))
|
||||
Assert.assertNull(host, IPv4.toAddress(host))
|
||||
}
|
||||
for (host in invalidIpV6Hosts.keys) {
|
||||
Assert.assertNull(host, IPv6.fromString(host, true))
|
||||
Assert.assertNull(host, IPv6.toAddress(host, true))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue