From 5b8df2ff20a637be1d7844b256f93b4c472378d9 Mon Sep 17 00:00:00 2001 From: sergeych Date: Fri, 31 Mar 2023 21:14:02 +0100 Subject: [PATCH] WrongCollectionSize exception added and more docs --- .../kotlin/net.sergeych.bipack/BipackEncoder.kt | 2 +- .../kotlin/net.sergeych.bipack/annotations.kt | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt index 89e4f8d..4396fa8 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt @@ -63,7 +63,7 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() { if (fixedSize < 0) encodeUInt(collectionSize.toUInt()) else if (collectionSize != fixedSize) { - throw IllegalArgumentException("collection size is $collectionSize while fixed size of $fixedSize is required") + throw WrongCollectionSize("collection size is $collectionSize while fixed size of $fixedSize is required") } return this } diff --git a/src/commonMain/kotlin/net.sergeych.bipack/annotations.kt b/src/commonMain/kotlin/net.sergeych.bipack/annotations.kt index 3808179..370a8d2 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/annotations.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/annotations.kt @@ -44,16 +44,24 @@ annotation class CrcProtected * it is the conly way to tell the serialized to use more compact unsigned variable length encoding. */ @SerialInfo -@Target(AnnotationTarget.FIELD,AnnotationTarget.PROPERTY) +@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) 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 + * collection size. As for nowonly collection types (e.g. ByteArray, Listm etc) are supported. + * Note that if the actual collection size differs from [size], [BipackEncoder] will throw + * [WrongCollectionSize] while encoding it. + */ @SerialInfo -@Target(AnnotationTarget.FIELD,AnnotationTarget.PROPERTY) +@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) annotation class FixedSize(val size: Int) open class InvalidFrameException(reason: String) : Exception(reason) class InvalidFrameHeaderException(reason: String = "Frame header does not match") : InvalidFrameException(reason) class InvalidFrameCRCException : InvalidFrameException("Checksum CRC32 failed") +class WrongCollectionSize(reason: String) : Exception(reason)