Optimized how IP (as IP address) InetAddress resolution works for IPv4/6
This commit is contained in:
parent
0bb5b251fe
commit
8669cab86c
|
@ -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.3"
|
const val version = "1.4"
|
||||||
|
|
||||||
// set as project.ext
|
// set as project.ext
|
||||||
const val name = "NetworkUtils"
|
const val name = "NetworkUtils"
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
package dorkbox.netUtil
|
package dorkbox.netUtil
|
||||||
|
|
||||||
import dorkbox.netUtil.Common.logger
|
import dorkbox.netUtil.Common.logger
|
||||||
import java.net.*
|
import java.net.Inet4Address
|
||||||
|
import java.net.Inet6Address
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.NetworkInterface
|
||||||
|
import java.net.SocketException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that holds a number of network-related constants, also from:
|
* A class that holds a number of network-related constants, also from:
|
||||||
|
@ -213,7 +218,7 @@ object IP {
|
||||||
val sb: StringBuilder
|
val sb: StringBuilder
|
||||||
|
|
||||||
sb = if (addr.isUnresolved) {
|
sb = if (addr.isUnresolved) {
|
||||||
val hostname = getHostname(addr)
|
val hostname = addr.hostString
|
||||||
newSocketAddressStringBuilder(hostname, port, !IPv6.isValid(hostname))
|
newSocketAddressStringBuilder(hostname, port, !IPv6.isValid(hostname))
|
||||||
} else {
|
} else {
|
||||||
val address = addr.address
|
val address = addr.address
|
||||||
|
@ -257,19 +262,9 @@ object IP {
|
||||||
*/
|
*/
|
||||||
fun getByName(ip: String): InetAddress? {
|
fun getByName(ip: String): InetAddress? {
|
||||||
return if (IPv4.isValid(ip)) {
|
return if (IPv4.isValid(ip)) {
|
||||||
IPv4.getByName(ip)
|
IPv4.getByNameUnsafe(ip)
|
||||||
} else {
|
} else {
|
||||||
IPv6.getByName(ip)
|
IPv6.getByName(ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns [InetSocketAddress.getHostString] if Java >= 7,
|
|
||||||
* or [InetSocketAddress.getHostName] otherwise.
|
|
||||||
* @param addr The address
|
|
||||||
* @return the host string
|
|
||||||
*/
|
|
||||||
fun getHostname(addr: InetSocketAddress): String {
|
|
||||||
return addr.hostString
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -727,6 +727,19 @@ object IPv4 {
|
||||||
return address
|
return address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the [Inet4Address] representation of a [String] IP address.
|
||||||
|
*
|
||||||
|
* This method will treat all IPv4 type addresses as "IPv4 mapped" (see [.getByName])
|
||||||
|
*
|
||||||
|
* @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 {
|
||||||
|
val asBytes = toBytes(ip)
|
||||||
|
return Inet4Address.getByAddress(ip, asBytes) as Inet4Address
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the [Inet4Address] representation of a [String] IP address.
|
* Returns the [Inet4Address] representation of a [String] IP address.
|
||||||
*
|
*
|
||||||
|
@ -737,8 +750,7 @@ object IPv4 {
|
||||||
*/
|
*/
|
||||||
fun getByName(ip: String): Inet4Address? {
|
fun getByName(ip: String): Inet4Address? {
|
||||||
return if (isValid(ip)) {
|
return if (isValid(ip)) {
|
||||||
val asBytes = toBytes(ip)
|
return getByNameUnsafe(ip)
|
||||||
return Inet4Address.getByAddress(ip, asBytes) as Inet4Address
|
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue