Created
February 6, 2019 08:57
-
-
Save TheDIM47/0abfbd2871cda6a8f218b9f9af1910f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress | |
import java.nio.ByteBuffer | |
object InetUtils { | |
private[this] val Radix = 16 | |
// Fast Hex converters | |
private[this] val HexChars: Array[Char] = "0123456789ABCDEF".toCharArray | |
// to lowercase hex chars | |
@inline def byteToHex(b: Byte): String = | |
byteToHexU(new Array[Char](2), 0, b).mkString | |
// fill buffer starting with startIndex with two Hex chars | |
@inline def byteToHexL(buf: Array[Char], startIndex: Int, b: Byte): Array[Char] = { | |
buf(startIndex) = Character.forDigit((b >> 4) & 0x0F, Radix) | |
buf(startIndex + 1) = Character.forDigit(b & 0x0F, Radix) | |
buf | |
} | |
// fill buffer starting with startIndex with two Hex chars in UpperCase | |
@inline def byteToHexU(buf: Array[Char], startIndex: Int, b: Byte): Array[Char] = { | |
buf(startIndex) = HexChars((b >> 4) & 0x0F) | |
buf(startIndex + 1) = HexChars(b & 0x0F) | |
buf | |
} | |
@inline def hexToByte(s: String): Byte = ( | |
(Character.digit(s.charAt(0), Radix) << 4) + | |
Character.digit(s.charAt(1), Radix) | |
).toByte | |
// IP-address ops | |
@inline def ipToInt(ip: String): Int = | |
ByteBuffer.wrap(InetAddress.getByName(ip).getAddress).getInt | |
@inline def intToIp(ip: Int): String = Seq( | |
((ip >> 24) & 0xFF).toShort, | |
((ip >> 16) & 0xFF).toShort, | |
((ip >> 8) & 0xFF).toShort, | |
((ip >> 0) & 0xFF).toShort | |
).mkString(".") | |
// MAC-address ops - MAC TO Long | |
@inline def macToLong(mac: String): Long = { | |
val bytes = mac.split(":").map(hexToByte) | |
val b8 = new Array[Byte](8) | |
bytes.copyToArray(b8, Math.max(0, 8 - bytes.length), 8) | |
ByteBuffer.wrap(b8).getLong | |
} | |
// MAC-address ops - Long To MAC | |
@inline def longToMac(mac: Long): String = longToMac48(mac) | |
// Fastest LongToMac-48 version | |
@inline def longToMac48(address: Long): String = { | |
val buf = new Array[Char](17) | |
byteToHexU(buf, 0, ((address >> 40) & 0xFF).toByte) | |
buf(2) = ':' | |
byteToHexU(buf, 3, ((address >> 32) & 0xFF).toByte) | |
buf(5) = ':' | |
byteToHexU(buf, 6, ((address >> 24) & 0xFF).toByte) | |
buf(8) = ':' | |
byteToHexU(buf, 9, ((address >> 16) & 0xFF).toByte) | |
buf(11) = ':' | |
byteToHexU(buf, 12, ((address >> 8) & 0xFF).toByte) | |
buf(14) = ':' | |
byteToHexU(buf, 15, ((address >> 0) & 0xFF).toByte) | |
new String(buf) | |
} | |
// Fastest LongToMac-64 version | |
@inline def longToMac64(address: Long): String = { | |
val buf = new Array[Char](23) | |
byteToHexU(buf, 0, ((address >> 56) & 0xFF).toByte) | |
buf(2) = ':' | |
byteToHexU(buf, 3, ((address >> 48) & 0xFF).toByte) | |
buf(5) = ':' | |
byteToHexU(buf, 6, ((address >> 40) & 0xFF).toByte) | |
buf(8) = ':' | |
byteToHexU(buf, 9, ((address >> 32) & 0xFF).toByte) | |
buf(11) = ':' | |
byteToHexU(buf, 12, ((address >> 24) & 0xFF).toByte) | |
buf(14) = ':' | |
byteToHexU(buf, 15, ((address >> 16) & 0xFF).toByte) | |
buf(17) = ':' | |
byteToHexU(buf, 18, ((address >> 8) & 0xFF).toByte) | |
buf(20) = ':' | |
byteToHexU(buf, 21, ((address >> 0) & 0xFF).toByte) | |
new String(buf) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment