-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed ByteArrayParcel implementation
- Loading branch information
Showing
8 changed files
with
114 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
parcelable-core/src/commonMain/kotlin/com/chrynan/parcelable/core/NumberUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.chrynan.parcelable.core | ||
|
||
import kotlin.experimental.and | ||
|
||
/** | ||
* Converts this [Int] value into a [ByteArray] representation. | ||
*/ | ||
internal fun Int.toByteArray(): ByteArray { | ||
val result = ByteArray(Int.SIZE_BYTES) | ||
var value = this | ||
|
||
for (i in Int.SIZE_BYTES - 1 downTo 0) { | ||
result[i] = (value and 0xFF).toByte() | ||
value = value shr Byte.SIZE_BITS | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Converts this [Long] value into a [ByteArray] representation. | ||
*/ | ||
internal fun Long.toByteArray(): ByteArray { | ||
val result = ByteArray(Long.SIZE_BYTES) | ||
var value = this | ||
|
||
for (i in 0 until Long.SIZE_BYTES) { | ||
result[Long.SIZE_BYTES - i - 1] = (value and 0xFF).toByte() | ||
value = value ushr Byte.SIZE_BITS | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Converts this [ByteArray] representation of a [Long] value into a [Long] value. | ||
*/ | ||
internal fun ByteArray.toLongValue(): Long { | ||
require(this.size == Long.SIZE_BYTES) { "ByteArray must have a size of 8 to convert to a Long." } | ||
|
||
var result = 0L | ||
|
||
for (i in 0 until Long.SIZE_BYTES) { | ||
result = result shl Byte.SIZE_BITS | ||
result = (result or (this[i].toLong() and 0xFF)) | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Converts this [ByteArray] representation of an [Int] value into an [Int] value. | ||
* | ||
* **Note:** This function is not safe for use outside this library as it doesn't expose, make guarantees about, or | ||
* provide a way to change the endianness ordering. | ||
*/ | ||
internal fun ByteArray.toIntValue(): Int { | ||
var result = 0 | ||
|
||
for (i in 0 until Int.SIZE_BYTES) { | ||
result = result shl Byte.SIZE_BITS | ||
result = (result or (this[i] and 0xFF.toByte()).toInt()) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
parcelable-core/src/commonTest/kotlin/com.chrynan.parcelable.core/NumberUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.chrynan.parcelable.core | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class NumberUtilsTest { | ||
|
||
@Test | ||
fun longConversionShouldWork() { | ||
val longValue = 500_000L | ||
|
||
val byteArray = longValue.toByteArray() | ||
|
||
assertEquals(expected = longValue, actual = byteArray.toLongValue()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters