support for fixed size ints (all), but only BE
This commit is contained in:
1 parent
86eb5e9d7f
commit
641188e92b
5 files changed
+102
-24
No files matched your search
@@ -35,7 +35,9 @@ class BipackDecoder(
|
||||
if (fixedNumber) input.readI32()
|
||||
else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber()
|
||||
|
||||
override fun decodeLong(): Long = if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber()
|
||||
override fun decodeLong(): Long =
|
||||
if( fixedNumber ) input.readI64()
|
||||
else if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber()
|
||||
override fun decodeFloat(): Float = input.readFloat()
|
||||
override fun decodeDouble(): Double = input.readDouble()
|
||||
override fun decodeChar(): Char = Char(input.readNumber<UInt>().toInt())
|
||||
|
||||
@@ -44,10 +44,13 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() {
|
||||
else output.writeNumber(value)
|
||||
|
||||
fun encodeUInt(value: UInt) = output.writeNumber(value)
|
||||
override fun encodeLong(value: Long) = if (nextIsUnsigned)
|
||||
output.writeNumber(value.toULong())
|
||||
else
|
||||
output.writeNumber(value)
|
||||
override fun encodeLong(value: Long) =
|
||||
if (fixedNumber)
|
||||
output.writeI64(value)
|
||||
else if (nextIsUnsigned)
|
||||
output.writeNumber(value.toULong())
|
||||
else
|
||||
output.writeNumber(value)
|
||||
|
||||
override fun encodeFloat(value: Float) = output.writeFloat(value)
|
||||
override fun encodeDouble(value: Double) = output.writeDouble(value)
|
||||
|
||||
@@ -48,16 +48,47 @@ annotation class CrcProtected
|
||||
annotation class Unsigned
|
||||
|
||||
/**
|
||||
* Use it with collection of fixed size, like hash digest, key bits and so on, by not storing collection
|
||||
* size. It effectively reduced packed size to at least one byte. depending on the actual
|
||||
* Fixed size collection of a given size. __Use it only with collections!__
|
||||
*
|
||||
* Use it with collection of fixed size, to read/write exact number of items (for example, bytes),
|
||||
* like hash digest, key bits and so on. Does not stores/loades the collection
|
||||
* size what reduces packed size to at least one byte. depending on the actual
|
||||
* collection size. As for nowonly collection types (e.g. ByteArray, List<T>m etc) are supported.
|
||||
* Note that if the actual collection size differs from [size], [BipackEncoder] will throw
|
||||
* [WrongCollectionSize] while encoding it.
|
||||
* [WrongCollectionSize] while encoding it. For example:
|
||||
*
|
||||
* ~~~
|
||||
* @Serializable
|
||||
* class Foo(
|
||||
* @FixedSize(32)
|
||||
* thirtyTwoBytes: ByteArray
|
||||
* )
|
||||
*/
|
||||
@SerialInfo
|
||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
|
||||
annotation class FixedSize(val size: Int)
|
||||
|
||||
/**
|
||||
* Fixed-size number, big-endian. Could be used only with field of following types:
|
||||
*
|
||||
* - Int, UInt: 4 bytes
|
||||
* - Short, UShort: 2 bytes
|
||||
* - Long, ULong: 8 bytes.
|
||||
*
|
||||
* It should not be used with Byte or UByte as their size is always 1 byte ;)
|
||||
*
|
||||
* Example:
|
||||
* ~~~
|
||||
* @Serializable
|
||||
* class Foo(
|
||||
* @Fixed
|
||||
* val eightBytesLongInt: Long
|
||||
* )
|
||||
*
|
||||
* // so:
|
||||
* assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(Foo(0x100000002)).encodeToHex())
|
||||
* ~~~
|
||||
*/
|
||||
@SerialInfo
|
||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
|
||||
annotation class Fixed
|
||||
|
||||
Reference in new issue
Block a user