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