refactor lib structure

This commit is contained in:
2025-12-13 14:49:00 +01:00
parent 3ab2bea4f6
commit f5d559e8fe
28 changed files with 1017 additions and 1301 deletions

View File

@@ -0,0 +1,181 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) }
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int = asByteArray().toInt(order)
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = toInt(order).toUInt()
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = asByteArray().toUInt(order)
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return asList()
.chunked(4) { it.toByteArray().toInt(order) }
.toIntArray()
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray = asByteArray().toIntArray(order)
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = toIntArray(order).asUIntArray()
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = asByteArray().toUIntArray(order)
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toInt().toByteArray(order)
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toInt().toUByteArray(order)
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asIntArray().toByteArray(order)
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asIntArray().toUByteArray(order)
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) }
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long = asByteArray().toLong(order)
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = toLong(order).toULong()
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = asByteArray().toULong(order)
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return asList()
.chunked(8) { it.toByteArray().toLong(order) }
.toLongArray()
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray = asByteArray().toLongArray(order)
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = toLongArray(order).asULongArray()
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = asByteArray().toULongArray(order)
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toLong().toByteArray(order)
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toLong().toUByteArray(order)
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).asList()
}
return bytes.toByteArray()
}
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asLongArray().toByteArray(order)
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asLongArray().toUByteArray(order)