Skip to content

Commit

Permalink
Merge pull request #2 from robxyy/robx/nsdata/2022-09-26
Browse files Browse the repository at this point in the history
Thanks!
  • Loading branch information
qiaoyuang authored Sep 29, 2022
2 parents d52108b + aec124b commit 39c3f69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions mmkv-kotlin/src/appleMain/kotlin/com/ctrip/flight/mmkv/Binary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ import platform.posix.memcpy
* @author yaqiao
*/

internal fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply {
usePinned {
memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length)
internal fun NSData.toByteArray(): ByteArray {
val size = length.toInt()
return ByteArray(size).apply {
if (size != 0) {
usePinned {
memcpy(it.addressOf(0), bytes, length)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ class MMKVKotlinTestIos {
val value1 = mmkvImpl.takeObject(MMKVKotlinTest.KEY_NOT_EXIST, NSArray.`class`()!!)
assertEquals(value1, null)
}

@Test
fun emptyNsDataToByteArray() {
@Suppress("CAST_NEVER_SUCCEEDS")
val data = ("" as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData
val bytes = data.toByteArray()
assert(bytes.isEmpty())
}
}

0 comments on commit 39c3f69

Please sign in to comment.