Skip to content

Commit f06bd14

Browse files
authored
Merge pull request #914 from splendo/feature/bluetooth-serializer
Bluetooth Serializer
2 parents e0260a5 + 5dd6165 commit f06bd14

File tree

89 files changed

+14553
-1265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+14553
-1265
lines changed

base/api/android/base.api

Lines changed: 1246 additions & 117 deletions
Large diffs are not rendered by default.

base/api/jvm/base.api

Lines changed: 1293 additions & 117 deletions
Large diffs are not rendered by default.

base/src/commonMain/kotlin/bytes/ByteArrayBuilder.kt

Lines changed: 442 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2025 Splendo Consulting B.V. The Netherlands
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
package com.splendo.kaluga.base.bytes
19+
20+
/**
21+
* Shifts this value left by the bitCount number of bits.
22+
* @param bitCount the number of bits to shift by.
23+
* @return the shifted value.
24+
*/
25+
infix fun Byte.shl(bitCount: Int) = toUByte().shl(bitCount).toByte()
26+
27+
/**
28+
* Shifts this value right by the bitCount number of bits.
29+
* @param bitCount the number of bits to shift by.
30+
* @return the shifted value.
31+
*/
32+
infix fun Byte.shr(bitCount: Int) = toUByte().shr(bitCount).toByte()
33+
34+
/**
35+
* Checks whether the bit at [index] is set in this [Byte]
36+
* @param index the index of the bit to check.
37+
* @return `true` if the bit is set, `false` otherwise.
38+
*/
39+
fun Byte.isBitSet(index: Number) = toUByte().isBitSet(index)
40+
41+
/**
42+
* Creates a [Byte] that is equal to this [Byte] except the bit at [index] is set
43+
* @param index the index of the bit to set.
44+
* @return the new [Byte]
45+
*/
46+
fun Byte.setBit(index: Number) = toUByte().setBit(index).toByte()
47+
48+
/**
49+
* Checks whether the bit at [index] is set in this [ByteArray]
50+
* @param index the index of the bit to check.
51+
* @throws [IndexOutOfBoundsException] If the index is out of bounds of this array, except in Kotlin/JS where the behavior is unspecified.
52+
* @return `true` if the bit is set, `false` otherwise.
53+
*/
54+
fun ByteArray.isBitSet(index: Number) = index.toInt().let { intIndex ->
55+
this[intIndex / Byte.SIZE_BITS]
56+
.toUByte()
57+
.isBitSet(intIndex - (Byte.SIZE_BITS * (intIndex / Byte.SIZE_BITS)))
58+
}
59+
60+
/**
61+
* Creates a [ByteArray] that is equal to this array except the bit at [index] is set
62+
* @param index the index of the bit to set.
63+
* @return the new [ByteArray]
64+
*/
65+
fun ByteArray.setBit(index: Number): ByteArray = index.toInt().let { intIndex ->
66+
foldIndexed(byteArrayOf()) { arrayIndex, acc, bytes ->
67+
acc + if (arrayIndex == intIndex / Byte.SIZE_BITS) {
68+
bytes.setBit(intIndex - (Byte.SIZE_BITS * arrayIndex))
69+
} else {
70+
bytes
71+
}
72+
}
73+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2025 Splendo Consulting B.V. The Netherlands
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
package com.splendo.kaluga.base.bytes
19+
20+
/**
21+
* Byte order used for encoding and decoding primary types into a [ByteArray]
22+
*/
23+
enum class ByteOrder {
24+
/**
25+
* The most significant byte will be added to the front of the byte array.
26+
*/
27+
MOST_SIGNIFICANT_FIRST,
28+
29+
/**
30+
* The least significant byte will be added to the front of the byte array.
31+
*/
32+
LEAST_SIGNIFICANT_FIRST,
33+
}
34+
35+
internal fun ByteOrder.octetIndex(index: Int, bitsCount: Int) = when (this) {
36+
ByteOrder.LEAST_SIGNIFICANT_FIRST -> index
37+
ByteOrder.MOST_SIGNIFICANT_FIRST -> bitsCount / Byte.SIZE_BITS - index - 1
38+
}
39+
40+
internal fun ByteOrder.shift(index: Int, bitsCount: Int) = octetIndex(index, bitsCount) * Byte.SIZE_BITS

0 commit comments

Comments
 (0)