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.
This commit is contained in:
2024-11-30 12:03:16 +01:00
parent 1827704109
commit e60b6472ba
6 changed files with 62 additions and 159 deletions

View File

@@ -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<Task>("publishJsPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signJvmPublication"))
tasks.withType<PublishToMavenRepository> {
dependsOn(tasks.withType<Sign>())
}
tasks.named<Task>("publishJvmPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signJsPublication"))
}
tasks.named<Task>("publishJvmPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishJsPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signJvmPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signJsPublication"))
}
tasks.named<Task>("publishJsPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signJvmPublication"))
}
tasks.named<Task>("publishJvmPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signJsPublication"))
}
tasks.named<Task>("publishJvmPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishJsPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signJvmPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signJsPublication"))
}
tasks.named<Task>("publishJsPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signJvmPublication"))
}
tasks.named<Task>("publishJvmPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signJsPublication"))
}
tasks.named<Task>("publishJvmPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishJsPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signJvmPublication"))
}
tasks.named<Task>("publishKotlinMultiplatformPublicationToGiteaRepository") {
dependsOn(tasks.named<Task>("signJsPublication"))
}

View File

@@ -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(

View File

@@ -16,22 +16,6 @@ abstract class ByteArrayProperty<T>(
}
}
abstract class ByteArrayPropertyWithLength<T>(
name: String
) : ByteArrayProperty<T>(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<Boolean>(name) {
@@ -118,20 +102,20 @@ class DoubleProperty(
class StringProperty(
name: String,
) : ByteArrayPropertyWithLength<String>(name) {
) : ByteArrayProperty<String>(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<String>(name) {
) : ByteArrayProperty<String>(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<String>(name) {
) : ByteArrayProperty<String>(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<SlicedByteArray>(name) {
) : ByteArrayProperty<SlicedByteArray>(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)
}
}

View File

@@ -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,

View File

@@ -10,6 +10,10 @@ open class TypedByteArray(
val indexMap = mutableMapOf<String, Int>()
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}" }

View File

@@ -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)
}
}