encoder and decoder now support extendable formats.

This commit is contained in:
sergeych committed 2023-03-12 22:48:21 +01:00
1 parent 6e65a216c5
commit 63b231975b
13 files changed
+288 -23

No files matched your search

@@ -6,16 +6,41 @@ interface DataSink {
fun writeByte(data: Int) = writeByte(data.toByte())
open fun writeUByte(data: UByte) {
fun writeUByte(data: UByte) {
writeByte(data.toByte())
}
fun writeDouble(value: Double) {
writeI64(value.toRawBits())
}
fun writeFloat(value: Float) {
writeI32(value.toRawBits())
}
@Suppress("unused")
open fun writeBytes(data: ByteArray) {
fun writeBytes(data: ByteArray) {
for(d in data) writeByte(d)
}
}
inline fun <reified T:Any>DataSink.writeNumber(value: T) {
when(value) {
is Float -> writeFloat(value)
is Double -> writeDouble(value)
else -> Smartint.encode(value, this)
}
}
fun DataSink.writeI32(value: Int) {
writeBytes(intToBytes(value))
}
fun DataSink.writeI64(value: Long) {
writeBytes(longToBytes(value))
}
class ArrayDataSink : DataSink {
private val result = mutableListOf<Byte>()