Skip to content

Commit 1ed26f8

Browse files
committed
Convert ByteString from/to Int8Array
Closes #268
1 parent 92e0214 commit 1ed26f8

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed

bytestring/api/kotlinx-io-bytestring.klib.api

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ final fun (kotlinx.io.bytestring/ByteStringBuilder).kotlinx.io.bytestring/append
8282
final fun <#A: kotlin.text/Appendable> (kotlin.io.encoding/Base64).kotlinx.io.bytestring/encodeToAppendable(kotlinx.io.bytestring/ByteString, #A, kotlin/Int = ..., kotlin/Int = ...): #A // kotlinx.io.bytestring/encodeToAppendable|[email protected](kotlinx.io.bytestring.ByteString;0:0;kotlin.Int;kotlin.Int){0§<kotlin.text.Appendable>}[0]
8383
final fun kotlinx.io.bytestring/ByteString(kotlin/ByteArray...): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/ByteString|ByteString(kotlin.ByteArray...){}[0]
8484
final inline fun kotlinx.io.bytestring/buildByteString(kotlin/Int = ..., kotlin/Function1<kotlinx.io.bytestring/ByteStringBuilder, kotlin/Unit>): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/buildByteString|buildByteString(kotlin.Int;kotlin.Function1<kotlinx.io.bytestring.ByteStringBuilder,kotlin.Unit>){}[0]
85+
86+
// Targets: [js, wasmJs]
87+
final fun (kotlinx.io.bytestring/ByteString).kotlinx.io.bytestring/toArrayBuffer(): org.khronos.webgl/ArrayBuffer // kotlinx.io.bytestring/toArrayBuffer|[email protected](){}[0]
88+
89+
// Targets: [js, wasmJs]
90+
final fun (kotlinx.io.bytestring/ByteString).kotlinx.io.bytestring/toInt8Array(): org.khronos.webgl/Int8Array // kotlinx.io.bytestring/toInt8Array|[email protected](){}[0]
91+
92+
// Targets: [js, wasmJs]
93+
final fun (org.khronos.webgl/ArrayBuffer).kotlinx.io.bytestring/toByteString(): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/toByteString|[email protected](){}[0]
94+
95+
// Targets: [js, wasmJs]
96+
final fun (org.khronos.webgl/Int8Array).kotlinx.io.bytestring/toByteString(): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/toByteString|[email protected](){}[0]

bytestring/js/src/ByteStringsJs.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io.bytestring
7+
8+
import org.khronos.webgl.ArrayBuffer
9+
import org.khronos.webgl.Int8Array
10+
11+
/**
12+
* Returns a new [Int8Array] instance initialized with bytes copied from [this] ByteString.
13+
*/
14+
public fun ByteString.toInt8Array(): Int8Array = toByteArray().unsafeCast<Int8Array>()
15+
16+
/**
17+
* Returns a new [ArrayBuffer] instance initialized with bytes copied from [this] ByteString.
18+
*/
19+
public fun ByteString.toArrayBuffer(): ArrayBuffer = toInt8Array().buffer
20+
21+
/**
22+
* Return a new [ByteString] holding data copied from [this] Int8Array.
23+
*/
24+
public fun Int8Array.toByteString(): ByteString = ByteString(*unsafeCast<ByteArray>())
25+
26+
/**
27+
* Returns a new [ByteString] holding data copied from [this] ArrayBuffer
28+
*/
29+
public fun ArrayBuffer.toByteString(): ByteString = Int8Array(this).toByteString()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io.bytestring
7+
8+
import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
9+
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
10+
import org.khronos.webgl.ArrayBuffer
11+
import org.khronos.webgl.Int8Array
12+
import org.khronos.webgl.get
13+
import org.khronos.webgl.set
14+
import kotlin.test.Test
15+
import kotlin.test.assertContentEquals
16+
import kotlin.test.assertEquals
17+
import kotlin.test.assertTrue
18+
19+
class ByteStringConversionsTest {
20+
@Test
21+
fun int8ArrayToByteString() {
22+
assertTrue(Int8Array(0).toByteString().isEmpty())
23+
24+
val str = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray()).toByteString()
25+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
26+
}
27+
28+
@Test
29+
fun arrayBufferToByteString() {
30+
assertTrue(ArrayBuffer(0).toByteString().isEmpty())
31+
32+
val str = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray()).buffer.toByteString()
33+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
34+
}
35+
36+
@Test
37+
fun byteStringToInt8Array() {
38+
assertEquals(0, ByteString().toInt8Array().length)
39+
40+
val array = ByteString(1, 2, 3, 4).toInt8Array()
41+
for (idx in 0..<3) {
42+
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
43+
}
44+
}
45+
46+
@Test
47+
fun byteStringToArrayBuffer() {
48+
assertEquals(0, ByteString().toArrayBuffer().byteLength)
49+
50+
val buffer = ByteString(1, 2, 3, 4).toArrayBuffer()
51+
val array = Int8Array(buffer)
52+
for (idx in 0..<3) {
53+
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
54+
}
55+
}
56+
57+
@OptIn(UnsafeByteStringApi::class)
58+
@Test
59+
fun integrityCheck() {
60+
val array = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray())
61+
val str = array.toByteString()
62+
63+
array[0] = 42
64+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
65+
66+
val array2 = str.toInt8Array()
67+
UnsafeByteStringOperations.withByteArrayUnsafe(str) { it[1] = 42 }
68+
assertEquals(2, array2[1])
69+
}
70+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io.bytestring
7+
8+
import org.khronos.webgl.ArrayBuffer
9+
import org.khronos.webgl.Int8Array
10+
import org.khronos.webgl.get
11+
import org.khronos.webgl.set
12+
13+
/**
14+
* Returns a new [Int8Array] instance initialized with bytes copied from [this] ByteString.
15+
*/
16+
public fun ByteString.toInt8Array(): Int8Array {
17+
val res = Int8Array(size)
18+
for (idx in this.indices) {
19+
res[idx] = this[idx]
20+
}
21+
return res
22+
}
23+
24+
/**
25+
* Returns a new [ArrayBuffer] instance initialized with bytes copied from [this] ByteString.
26+
*/
27+
public fun ByteString.toArrayBuffer(): ArrayBuffer = toInt8Array().buffer
28+
29+
/**
30+
* Return a new [ByteString] holding data copied from [this] Int8Array.
31+
*/
32+
public fun Int8Array.toByteString(): ByteString {
33+
if (length == 0) {
34+
return ByteString.EMPTY
35+
}
36+
val builder = ByteStringBuilder(length)
37+
for (idx in 0..<length) {
38+
builder.append(this[idx])
39+
}
40+
return builder.toByteString()
41+
}
42+
43+
/**
44+
* Returns a new [ByteString] holding data copied from [this] ArrayBuffer
45+
*/
46+
public fun ArrayBuffer.toByteString(): ByteString = Int8Array(this).toByteString()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io.bytestring
7+
8+
import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
9+
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
10+
import org.khronos.webgl.ArrayBuffer
11+
import org.khronos.webgl.Int8Array
12+
import org.khronos.webgl.get
13+
import org.khronos.webgl.set
14+
import kotlin.test.Test
15+
import kotlin.test.assertContentEquals
16+
import kotlin.test.assertEquals
17+
import kotlin.test.assertTrue
18+
19+
private fun int8ArrayOf(vararg bytes: Byte): Int8Array {
20+
val array = Int8Array(bytes.size)
21+
for (i in bytes.indices) {
22+
array[i] = bytes[i]
23+
}
24+
return array
25+
}
26+
27+
class ByteStringConversionsTest {
28+
@Test
29+
fun int8ArrayToByteString() {
30+
assertTrue(Int8Array(0).toByteString().isEmpty())
31+
32+
val str = int8ArrayOf(1, 2, 3, 4).toByteString()
33+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
34+
}
35+
36+
@Test
37+
fun arrayBufferToByteString() {
38+
assertTrue(ArrayBuffer(0).toByteString().isEmpty())
39+
40+
val str = int8ArrayOf(1, 2, 3, 4).buffer.toByteString()
41+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
42+
}
43+
44+
@Test
45+
fun byteStringToInt8Array() {
46+
assertEquals(0, ByteString().toInt8Array().length)
47+
48+
val array = ByteString(1, 2, 3, 4).toInt8Array()
49+
for (idx in 0..<3) {
50+
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
51+
}
52+
}
53+
54+
@Test
55+
fun byteStringToArrayBuffer() {
56+
assertEquals(0, ByteString().toArrayBuffer().byteLength)
57+
58+
val buffer = ByteString(1, 2, 3, 4).toArrayBuffer()
59+
val array = Int8Array(buffer)
60+
for (idx in 0..<3) {
61+
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
62+
}
63+
}
64+
65+
@OptIn(UnsafeByteStringApi::class)
66+
@Test
67+
fun integrityCheck() {
68+
val array = int8ArrayOf(1, 2, 3, 4)
69+
val str = array.toByteString()
70+
71+
array[0] = 42
72+
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
73+
74+
val array2 = str.toInt8Array()
75+
UnsafeByteStringOperations.withByteArrayUnsafe(str) { it[1] = 42 }
76+
assertEquals(2, array2[1])
77+
}
78+
}

0 commit comments

Comments
 (0)