From 232d8fe92a442491f687ca11900a1d4810b8a9fa Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sat, 14 Dec 2024 11:19:22 +0100 Subject: [PATCH] Add constructor to SlicedByteArray and update version to 0.2.6 Introduce a new constructor in `SlicedByteArray` for initializing from a ByteArray. Updated `build.gradle.kts` to bump the version to 0.2.6, and made minor adjustments to `readme.md` to improve clarity and consistency. --- build.gradle.kts | 2 +- readme.md | 18 +++++++++--------- .../kotlin/nl/astraeus/tba/SlicedByteArray.kt | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 23bd0a7..c6efc8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "nl.astraeus" -version = "0.2.5" +version = "0.2.6" repositories { mavenCentral() diff --git a/readme.md b/readme.md index d15e0a8..44e190c 100644 --- a/readme.md +++ b/readme.md @@ -11,26 +11,26 @@ to convert the bytes to the property and back when the property is accessed. The This is what a definition could look like: ```kotlin -class Person : TypedByteArray( +class Person() : TypedByteArray( Type("id", DataType.LONG), Type("name", DataType.STRING, 100), Type("age", DataType.INT), Type("rate", DataType.DOUBLE), Type("description", DataType.CLOB, 100000) ) { - var id by LongProperty("id") - var name by StringProperty("name") - var age by IntProperty("age") - var rate by DoubleProperty("rate") - var description by ClobProperty("description") - + var id by long("id") + var name by string("name") + var age by int("age") + var rate by double("rate") + var description by clob("description") + constructor(name: String, age: Int) : this() { this.name = name this.age = age this.rate = 1.0 this.description = "Nothing here yet." } - + constructor(data: ByteArray): this() { check(data.size == definition.size) { "Invalid data size: ${data.size} != ${definition.size}" @@ -42,7 +42,7 @@ class Person : TypedByteArray( ``` The different between STRING and CLOB is the maximum size (32kb vs 2gb). For any type where the length isn't fixed we need to provide max size. -When such a type is updated, for example we update a string with a shorter string the remaining bytes are zeroed to make compression as efficient as possible. +When such a type is updated, for example we update a string with a shorter string, the remaining bytes are zeroed to make compression as efficient as possible. Strings are stored as UTF-8 bytes, there is also a CachedStringProperty which will keep a copy of the string so it isn't converted to a native string every time it's accessed. diff --git a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt index 21fcf49..13cfdcc 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt @@ -8,6 +8,8 @@ class SlicedByteArray( val size: Int get() = length + constructor(array: ByteArray) : this(array, 0, array.size) + constructor(size: Int) : this(ByteArray(size), 0, size) operator fun get(index: Int): Byte {