-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: abstract out reading ByteArrays
This closes #13
- Loading branch information
Showing
7 changed files
with
177 additions
and
75 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
16 changes: 16 additions & 0 deletions
16
...lin/com.ensarsarajcic.kotlinx.serialization.msgpack/extensions/MsgPackExtensionDecoder.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.ensarsarajcic.kotlinx.serialization.msgpack.extensions | ||
|
||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.AbstractDecoder | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
class MsgPackExtensionDecoder( | ||
override val serializersModule: SerializersModule | ||
) : AbstractDecoder() { | ||
|
||
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = 0 | ||
|
||
override fun decodeValue(): Any { | ||
return super.decodeValue() | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../com.ensarsarajcic.kotlinx.serialization.msgpack/extensions/MsgPackExtensionSerializer.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,20 @@ | ||
package com.ensarsarajcic.kotlinx.serialization.msgpack.extensions | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
class MsgPackExtensionSerializer<T> : KSerializer<T> { | ||
|
||
override fun deserialize(decoder: Decoder): T { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override val descriptor: SerialDescriptor | ||
get() = TODO("Not yet implemented") | ||
|
||
override fun serialize(encoder: Encoder, value: T) { | ||
TODO("Not yet implemented") | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...onMain/kotlin/com.ensarsarajcic.kotlinx.serialization.msgpack/stream/MsgPackDataBuffer.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,29 @@ | ||
package com.ensarsarajcic.kotlinx.serialization.msgpack.stream | ||
|
||
internal class MsgPackDataBuffer( | ||
private val byteArray: ByteArray | ||
) { | ||
private var index = 0 | ||
|
||
fun skip(bytes: Int) { | ||
index += bytes | ||
} | ||
|
||
fun peek(): Byte = byteArray.getOrNull(index) ?: throw Exception("End of stream") | ||
|
||
// Increases index only if next byte is not null | ||
fun nextByteOrNull(): Byte? = byteArray.getOrNull(index)?.also { index++ } | ||
|
||
fun requireNextByte(): Byte = nextByteOrNull() ?: throw Exception("End of stream") | ||
|
||
fun takeNext(next: Int): ByteArray { | ||
require(next > 0) { "Number of bytes to take must be greater than 0!" } | ||
val result = ByteArray(next) | ||
(0 until next).forEach { | ||
result[it] = requireNextByte() | ||
} | ||
return result | ||
} | ||
} | ||
|
||
internal fun ByteArray.toMsgPackBuffer() = MsgPackDataBuffer(this) |
Oops, something went wrong.