From e60b6472baccec5422df13ac63d28b6ab016e47e Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sat, 30 Nov 2024 12:03:16 +0100 Subject: [PATCH] Remove ByteArrayPropertyWithLength and update handling Removed the ByteArrayPropertyWithLength class and refactored related methods to simplify the handling of string and blob properties without maxLength constraints. This change improves code maintainability by eliminating redundant checks and ensures that property operations are more straightforward and error-free. Additionally, updated the Kotlin version and optimized the build configuration for better dependency management. --- build.gradle.kts | 84 ++----------------- .../nl/astraeus/tba/ByteArrayHandler.kt | 34 ++++++-- .../nl/astraeus/tba/ByteArrayProperties.kt | 34 ++------ .../kotlin/nl/astraeus/tba/SlicedByteArray.kt | 58 ++----------- .../kotlin/nl/astraeus/tba/TypedByteArray.kt | 4 + .../nl/astraeus/tba/TypedByteArrayTest.kt | 7 ++ 6 files changed, 62 insertions(+), 159 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 1ca7cb5..8b8f5c5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,23 +1,21 @@ plugins { - kotlin("multiplatform") version "2.0.20" + kotlin("multiplatform") version "2.1.0" `maven-publish` signing id("org.jetbrains.dokka") version "1.5.31" } group = "nl.astraeus" -version = "0.1.1-SNAPSHOT" +version = "0.2.0-SNAPSHOT" repositories { mavenCentral() } kotlin { - jvm { - withJava() - } + jvm() js { - browser {} + browser() } sourceSets { @@ -113,76 +111,6 @@ signing { sign(publishing.publications) } -tasks.named("publishJsPublicationToMavenLocal") { - dependsOn(tasks.named("signJvmPublication")) +tasks.withType { + dependsOn(tasks.withType()) } - -tasks.named("publishJvmPublicationToMavenLocal") { - dependsOn(tasks.named("signJsPublication")) -} - -tasks.named("publishJvmPublicationToMavenLocal") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishJsPublicationToMavenLocal") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToMavenLocal") { - dependsOn(tasks.named("signJvmPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToMavenLocal") { - dependsOn(tasks.named("signJsPublication")) -} - - -tasks.named("publishJsPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signJvmPublication")) -} - -tasks.named("publishJvmPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signJsPublication")) -} - -tasks.named("publishJvmPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishJsPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signJvmPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToMavenLocalRepository") { - dependsOn(tasks.named("signJsPublication")) -} - - -tasks.named("publishJsPublicationToGiteaRepository") { - dependsOn(tasks.named("signJvmPublication")) -} - -tasks.named("publishJvmPublicationToGiteaRepository") { - dependsOn(tasks.named("signJsPublication")) -} - -tasks.named("publishJvmPublicationToGiteaRepository") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishJsPublicationToGiteaRepository") { - dependsOn(tasks.named("signKotlinMultiplatformPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToGiteaRepository") { - dependsOn(tasks.named("signJvmPublication")) -} - -tasks.named("publishKotlinMultiplatformPublicationToGiteaRepository") { - dependsOn(tasks.named("signJsPublication")) -} \ No newline at end of file diff --git a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt index 6630dad..6df4d5e 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt @@ -25,11 +25,22 @@ class MutableByteArrayHandler( fun setFloat(index: Int, value: Float) = buffer.setFloat(index, value) fun setDouble(index: Int, value: Double) = buffer.setDouble(index, value) - fun setString(index: Int, value: String, maxLength: Int) = buffer.setString(index, value, maxLength) + fun setString(index: Int, value: String) { + val bytes = value.encodeToByteArray() + buffer.setShort(index, bytes.size.toShort()) + buffer.setBlob(index + 2, SlicedByteArray.wrap(bytes)) + } - fun setClob(index: Int, value: String, maxLength: Int) = buffer.setClob(index, value, maxLength) + fun setClob(index: Int, value: String) { + val bytes = value.encodeToByteArray() + buffer.setInt(index, bytes.size) + buffer.setBlob(index + 4, SlicedByteArray.wrap(bytes)) + } - fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) = buffer.setBlob(index, bytes, maxLength) + fun setBlob(index: Int, bytes: SlicedByteArray) { + buffer.setInt(index, bytes.length) + buffer.setBlob(index + 4, bytes) + } } open class ByteArrayHandler( @@ -64,11 +75,22 @@ open class ByteArrayHandler( fun getFloat(index: Int): Float = buffer.getFloat(index) fun getDouble(index: Int): Double = buffer.getDouble(index) - fun getString(index: Int) = buffer.getString(index) + fun getString(index: Int): String { + val length = buffer.getShort(index) + val bytes = buffer.getBlob(index + 2, length.toInt()) + return bytes.decodeToString() + } - fun getClob(index: Int): String = buffer.getClob(index) + fun getClob(index: Int): String { + val length = buffer.getInt(index) + val bytes = buffer.getBlob(index + 4, length.toInt()) + return bytes.decodeToString() + } - fun getBlob(index: Int, length: Int): SlicedByteArray = buffer.getBlob(index, length) + fun getBlob(index: Int): SlicedByteArray { + val length = buffer.getInt(index) + return buffer.getBlob(index + 4, length) + } fun slice(range: IntRange): ByteArrayHandler { return ByteArrayHandler( diff --git a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt index 01c0ec1..ca9569f 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt @@ -16,22 +16,6 @@ abstract class ByteArrayProperty( } } -abstract class ByteArrayPropertyWithLength( - name: String -) : ByteArrayProperty(name) { - var maxLength: Int? = null - - protected fun getMaxLength(thisRef: TypedByteArray): Int { - if (maxLength == null) { - val type = thisRef.typeMap[name] ?: throw IllegalArgumentException( - "Type $name not found in typemap in $thisRef" - ) - maxLength = type.size - type.type.bytesUsedInternally - } - return maxLength!! - } -} - class BooleanProperty( name: String ) : ByteArrayProperty(name) { @@ -118,20 +102,20 @@ class DoubleProperty( class StringProperty( name: String, -) : ByteArrayPropertyWithLength(name) { +) : ByteArrayProperty(name) { override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): String { return thisRef.data.getString(getIndex(thisRef)) } override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: String) { - thisRef.data.setString(getIndex(thisRef), value, getMaxLength(thisRef)) + thisRef.data.setString(getIndex(thisRef), value) } } class CachedStringProperty( name: String, -) : ByteArrayPropertyWithLength(name) { +) : ByteArrayProperty(name) { var cachedValue: String? = null override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): String { @@ -142,33 +126,33 @@ class CachedStringProperty( } override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: String) { - thisRef.data.setString(getIndex(thisRef), value, getMaxLength(thisRef)) + thisRef.data.setString(getIndex(thisRef), value) cachedValue = value } } class ClobProperty( name: String -) : ByteArrayPropertyWithLength(name) { +) : ByteArrayProperty(name) { override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): String { return thisRef.data.getClob(getIndex(thisRef)) } override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: String) { - thisRef.data.setClob(getIndex(thisRef), value, getMaxLength(thisRef)) + thisRef.data.setClob(getIndex(thisRef), value) } } class BlobProperty( name: String -) : ByteArrayPropertyWithLength(name) { +) : ByteArrayProperty(name) { override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): SlicedByteArray { - val result = thisRef.data.getBlob(getIndex(thisRef), getMaxLength(thisRef)) + val result = thisRef.data.getBlob(getIndex(thisRef)) return result } override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: SlicedByteArray) { - thisRef.data.setBlob(getIndex(thisRef), value, getMaxLength(thisRef)) + thisRef.data.setBlob(getIndex(thisRef), value) } } diff --git a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt index 6b07949..f41d9c7 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt @@ -82,17 +82,10 @@ class SlicedByteArray( fun setDouble(index: Int, value: Double) = setLong(index, value.toRawBits()) fun getDouble(index: Int): Double = Double.fromBits(getLong(index)) - fun getClob(index: Int): String { - val length = getInt(index) - check(offset + index + length <= offset + this.length) { "Index out of bounds" } - val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length + 1).decodeToString() - return str - } - fun getBlob(index: Int, length: Int): SlicedByteArray { check(index + length <= this.length) { "Index out of bounds ($index, $length, ${this.length}" } - val str = SlicedByteArray(data, offset + index + 4, length) + val str = SlicedByteArray(data, offset + index, length) return str } @@ -114,55 +107,20 @@ class SlicedByteArray( fun toByteArray(): ByteArray = data.copyOfRange(offset, offset + length) - fun getString(index: Int): String { - val length = getShort(offset + index) - val str = data.decodeToString(offset + index + 2, offset + index + 2 + length.toInt()) - return str - } - - fun getString(start: Int, length: Int): String = data.decodeToString(offset + start, offset + start + length) - - fun setString(index: Int, value: String, maxLength: Int) { - val bytes = value.encodeToByteArray() - if (bytes.size > maxLength) { - throw IllegalArgumentException("String is too long") - } - this.setShort(offset + index, bytes.size.toShort()) - bytes.copyInto(data, offset + index + 2) - data.fill( - 0, - offset + index + 2 + bytes.size, - offset + index + maxLength - ) - } - - fun setClob(index: Int, value: String, maxLength: Int) { - val bytes = value.encodeToByteArray() - if (offset + index + bytes.size + 4 > maxLength) { - throw IllegalArgumentException("String is too long") - } - this.setInt(offset + index, bytes.size) - bytes.copyInto(data, offset + index + 4) - data.fill( - 0, - offset + index + 4 + bytes.size, - offset + index + maxLength - ) - } - - fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) { - if (bytes.size > maxLength) { + fun setBlob(index: Int, bytes: SlicedByteArray) { + if (index + bytes.size > length) { throw IllegalArgumentException("Blob is too long") } - this.setInt(offset + index, bytes.size) - bytes.copyInto(data, offset + index + 4, bytes.size) + bytes.copyInto(data, offset + index, bytes.size) data.fill( 0, - offset + index + 4 + bytes.size, - offset + index + maxLength + offset + index + bytes.size, + offset + length ) } + fun decodeToString(): String = data.decodeToString(offset, offset + length) + companion object { fun wrap( data: ByteArray, diff --git a/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt index 1e2c835..d382968 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt @@ -10,6 +10,10 @@ open class TypedByteArray( val indexMap = mutableMapOf() init { + check(definition.size <= data.buffer.length) { + "Size of data can not be smaller then size of definition" + } + var index = 0 for (type in definition.types) { check (!typeMap.containsKey(type.name)) { "Duplicate type name ${type.name}" } diff --git a/src/commonTest/kotlin/nl/astraeus/tba/TypedByteArrayTest.kt b/src/commonTest/kotlin/nl/astraeus/tba/TypedByteArrayTest.kt index 40f746d..7b7ca5a 100644 --- a/src/commonTest/kotlin/nl/astraeus/tba/TypedByteArrayTest.kt +++ b/src/commonTest/kotlin/nl/astraeus/tba/TypedByteArrayTest.kt @@ -49,4 +49,11 @@ class TypedByteArrayTest { assertEquals(42.toByte(), person2.age) assertEquals(180.toShort(), person2.length) } + + @Test + fun testByteArrayToBig() { + val bytes: ByteArray = ByteArray(1000) + + val person = Person(bytes) + } }