use bytearray library
This commit is contained in:
@@ -1,119 +0,0 @@
|
||||
package com.infendro.hash.util
|
||||
|
||||
import com.infendro.hash.util.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
internal fun MutableCollection<Byte>.addAll(
|
||||
bytes: ByteArray,
|
||||
): Boolean {
|
||||
return addAll(bytes.toList())
|
||||
}
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN,
|
||||
LITTLE_ENDIAN,
|
||||
}
|
||||
|
||||
internal fun UInt.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(4) { i ->
|
||||
val offset = (3 - i) * 8
|
||||
(this shr offset).toByte()
|
||||
}
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> bytes
|
||||
LITTLE_ENDIAN -> bytes.reversedArray()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ULong.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(8) { i ->
|
||||
val offset = (7 - i) * 8
|
||||
(this shr offset).toByte()
|
||||
}
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> bytes
|
||||
LITTLE_ENDIAN -> bytes.reversedArray()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ByteArray.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt {
|
||||
if (size != 4) throw Exception()
|
||||
|
||||
val bytes = when (order) {
|
||||
BIG_ENDIAN -> this
|
||||
LITTLE_ENDIAN -> this.reversedArray()
|
||||
}
|
||||
|
||||
return bytes.fold(0U) { acc, byte ->
|
||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Collection<Byte>.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt {
|
||||
return toByteArray().toUInt(order)
|
||||
}
|
||||
|
||||
internal fun ByteArray.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
if (size != 8) throw Exception()
|
||||
|
||||
val bytes = when (order) {
|
||||
BIG_ENDIAN -> this
|
||||
LITTLE_ENDIAN -> this.reversedArray()
|
||||
}
|
||||
|
||||
return bytes.fold(0UL) { acc, byte ->
|
||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Collection<Byte>.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
return toByteArray().toULong(order)
|
||||
}
|
||||
|
||||
internal fun ByteArray.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray {
|
||||
if (size % 4 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(4)
|
||||
.map { it.toUInt(order) }
|
||||
.toUIntArray()
|
||||
}
|
||||
|
||||
internal fun Collection<Byte>.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray {
|
||||
return toByteArray().toUIntArray(order)
|
||||
}
|
||||
|
||||
internal fun ByteArray.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
if (size % 8 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(8)
|
||||
.map { it.toULong(order) }
|
||||
.toULongArray()
|
||||
}
|
||||
|
||||
internal fun Collection<Byte>.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
return toByteArray().toULongArray(order)
|
||||
}
|
||||
Reference in New Issue
Block a user