First draft

This commit is contained in:
Mygod
2020-06-02 20:04:05 -04:00
parent 0366fc6bc6
commit 6ab763013b
14 changed files with 474 additions and 213 deletions

View File

@@ -1,14 +1,17 @@
package be.mygod.vpnhotspot.net
import android.net.MacAddress
import android.os.Parcelable
import androidx.annotation.RequiresApi
import kotlinx.android.parcel.Parcelize
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Compat support class for [MacAddress].
*/
inline class MacAddressCompat(val addr: Long) {
@Parcelize
inline class MacAddressCompat(val addr: Long) : Parcelable {
companion object {
private const val ETHER_ADDR_LEN = 6
/**
@@ -28,17 +31,44 @@ inline class MacAddressCompat(val addr: Long) {
return addr.joinToString(":") { "%02x".format(it) }
}
fun fromString(addr: String) = MacAddressCompat(ByteBuffer.allocate(8).run {
/**
* Creates a MacAddress from the given byte array representation.
* A valid byte array representation for a MacAddress is a non-null array of length 6.
*
* @param addr a byte array representation of a MAC address.
* @return the MacAddress corresponding to the given byte array representation.
* @throws IllegalArgumentException if the given byte array is not a valid representation.
*/
fun fromBytes(addr: ByteArray): MacAddressCompat {
require(addr.size == ETHER_ADDR_LEN) { addr.joinToString() + " was not a valid MAC address" }
return ByteBuffer.allocate(Long.SIZE_BYTES).run {
put(addr)
rewind()
MacAddressCompat(long)
}
}
/**
* Creates a MacAddress from the given String representation. A valid String representation
* for a MacAddress is a series of 6 values in the range [0,ff] printed in hexadecimal
* and joined by ':' characters.
*
* @param addr a String representation of a MAC address.
* @return the MacAddress corresponding to the given String representation.
* @throws IllegalArgumentException if the given String is not a valid representation.
*/
fun fromString(addr: String) = ByteBuffer.allocate(Long.SIZE_BYTES).run {
order(ByteOrder.LITTLE_ENDIAN)
mark()
try {
put(addr.split(':').map { Integer.parseInt(it, 16).toByte() }.toByteArray())
} catch (e: NumberFormatException) {
throw IllegalArgumentException(e)
}
reset()
long
})
rewind()
MacAddressCompat(long)
}
@RequiresApi(28)
fun MacAddress.toCompat() = fromBytes(toByteArray())
}
fun toList() = ByteBuffer.allocate(8).run {