This commit is contained in:
2025-12-12 21:20:44 +01:00
parent be9a8a4369
commit 01327ef4b2
12 changed files with 197 additions and 179 deletions

View File

@@ -1,38 +1,22 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
enum class ByteOrder {
BIG_ENDIAN {
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes
}
},
LITTLE_ENDIAN {
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes.reversedArray()
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes.reversedArray()
}
};
internal abstract fun normalize(
bytes: ByteArray,
): ByteArray
internal abstract fun normalize(
bytes: UByteArray,
): UByteArray
BIG_ENDIAN, LITTLE_ENDIAN;
}
fun ByteArray.normalize(
order: ByteOrder,
): ByteArray = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> reversedArray()
}
fun UByteArray.normalize(
order: ByteOrder,
): UByteArray = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> reversedArray()
}