This commit is contained in:
22
src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt
Normal file
22
src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN {
|
||||
override fun normalize(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return bytes
|
||||
}
|
||||
},
|
||||
LITTLE_ENDIAN {
|
||||
override fun normalize(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return bytes.reversedArray()
|
||||
}
|
||||
};
|
||||
|
||||
internal abstract fun normalize(
|
||||
bytes: ByteArray,
|
||||
): ByteArray
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
fun MutableCollection<Byte>.addAll(
|
||||
bytes: ByteArray,
|
||||
): Boolean {
|
||||
return addAll(bytes)
|
||||
}
|
||||
49
src/commonMain/kotlin/com/infendro/bytearray/Int.kt
Normal file
49
src/commonMain/kotlin/com/infendro/bytearray/Int.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Int.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(4) { i ->
|
||||
val offset = (3 - i) * 8
|
||||
(this ushr offset).toByte()
|
||||
}
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Int {
|
||||
if (size != 4) throw Exception()
|
||||
|
||||
val bytes = order.normalize(this)
|
||||
return bytes.fold(0) { acc, byte ->
|
||||
(acc shl 8) or (byte.toInt() and 0xFF)
|
||||
}
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Int {
|
||||
return toByteArray()
|
||||
.toInt(order)
|
||||
}
|
||||
|
||||
fun ByteArray.toIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): IntArray {
|
||||
if (size % 4 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(4)
|
||||
.map { it.toInt(order) }
|
||||
.toIntArray()
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): IntArray {
|
||||
return toByteArray()
|
||||
.toIntArray(order)
|
||||
}
|
||||
49
src/commonMain/kotlin/com/infendro/bytearray/Long.kt
Normal file
49
src/commonMain/kotlin/com/infendro/bytearray/Long.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Long.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(8) { i ->
|
||||
val offset = (7 - i) * 8
|
||||
(this ushr offset).toByte()
|
||||
}
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toLong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Long {
|
||||
if (size != 8) throw Exception()
|
||||
|
||||
val bytes = order.normalize(this)
|
||||
return bytes.fold(0L) { acc, byte ->
|
||||
(acc shl 8) or (byte.toLong() and 0xFFL)
|
||||
}
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toLong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Long {
|
||||
return toByteArray()
|
||||
.toLong(order)
|
||||
}
|
||||
|
||||
fun ByteArray.toLongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): LongArray {
|
||||
if (size % 8 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(8)
|
||||
.map { it.toLong(order) }
|
||||
.toLongArray()
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toLongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): LongArray {
|
||||
return toByteArray()
|
||||
.toLongArray(order)
|
||||
}
|
||||
49
src/commonMain/kotlin/com/infendro/bytearray/UInt.kt
Normal file
49
src/commonMain/kotlin/com/infendro/bytearray/UInt.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun UInt.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(4) { i ->
|
||||
val offset = (3 - i) * 8
|
||||
(this shr offset).toByte()
|
||||
}
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt {
|
||||
if (size != 4) throw Exception()
|
||||
|
||||
val bytes = order.normalize(this)
|
||||
return bytes.fold(0U) { acc, byte ->
|
||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
||||
}
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt {
|
||||
return toByteArray()
|
||||
.toUInt(order)
|
||||
}
|
||||
|
||||
fun ByteArray.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray {
|
||||
if (size % 4 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(4)
|
||||
.map { it.toUInt(order) }
|
||||
.toUIntArray()
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray {
|
||||
return toByteArray()
|
||||
.toUIntArray(order)
|
||||
}
|
||||
49
src/commonMain/kotlin/com/infendro/bytearray/ULong.kt
Normal file
49
src/commonMain/kotlin/com/infendro/bytearray/ULong.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun ULong.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(8) { i ->
|
||||
val offset = (7 - i) * 8
|
||||
(this shr offset).toByte()
|
||||
}
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
if (size != 8) throw Exception()
|
||||
|
||||
val bytes = order.normalize(this)
|
||||
return bytes.fold(0UL) { acc, byte ->
|
||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
||||
}
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
return toByteArray()
|
||||
.toULong(order)
|
||||
}
|
||||
|
||||
fun ByteArray.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
if (size % 8 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(8)
|
||||
.map { it.toULong(order) }
|
||||
.toULongArray()
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
return toByteArray()
|
||||
.toULongArray(order)
|
||||
}
|
||||
Reference in New Issue
Block a user