replace IllegalArgumentException with require
This commit is contained in:
@@ -5,8 +5,7 @@ import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
fun ByteArray.toInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Int {
|
||||
if (size != 4)
|
||||
throw IllegalArgumentException()
|
||||
require(size == 4)
|
||||
|
||||
return normalize(order)
|
||||
.fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) }
|
||||
@@ -27,8 +26,7 @@ fun UByteArray.toUInt(
|
||||
fun ByteArray.toIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): IntArray {
|
||||
if (size % 4 != 0)
|
||||
throw IllegalArgumentException()
|
||||
require(size % 4 == 0)
|
||||
|
||||
return asList()
|
||||
.chunked(4) { it.toByteArray().toInt(order) }
|
||||
@@ -94,8 +92,7 @@ fun UIntArray.toUByteArray(
|
||||
fun ByteArray.toLong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Long {
|
||||
if (size != 8)
|
||||
throw IllegalArgumentException()
|
||||
require(size == 8)
|
||||
|
||||
return normalize(order)
|
||||
.fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) }
|
||||
@@ -116,8 +113,7 @@ fun UByteArray.toULong(
|
||||
fun ByteArray.toLongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): LongArray {
|
||||
if (size % 8 != 0)
|
||||
throw IllegalArgumentException()
|
||||
require(size % 8 == 0)
|
||||
|
||||
return asList()
|
||||
.chunked(8) { it.toByteArray().toLong(order) }
|
||||
|
||||
@@ -7,8 +7,7 @@ import kotlin.experimental.xor
|
||||
infix fun ByteArray.and(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size)
|
||||
throw IllegalArgumentException()
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] and that[i]
|
||||
@@ -22,8 +21,7 @@ infix fun UByteArray.and(
|
||||
infix fun ByteArray.or(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size)
|
||||
throw IllegalArgumentException()
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] or that[i]
|
||||
@@ -37,8 +35,7 @@ infix fun UByteArray.or(
|
||||
infix fun ByteArray.xor(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size)
|
||||
throw IllegalArgumentException()
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] xor that[i]
|
||||
|
||||
Reference in New Issue
Block a user