From 69fa23a7a93d3b459c79964c0db891fafd419675 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Fri, 13 Dec 2024 22:00:00 +0100 Subject: [PATCH] Fix MutableByteArrayHandler placement and update readme info Relocated MutableByteArrayHandler implementation to correct position in ByteArrayHandler file for better organization. Updated readme to clarify behavior when updating variable-length types like strings for improved compression efficiency. --- readme.md | 2 +- .../nl/astraeus/tba/ByteArrayHandler.kt | 91 ++++++++++--------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/readme.md b/readme.md index 35fb1cd..d15e0a8 100644 --- a/readme.md +++ b/readme.md @@ -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 for example a string is updated 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/ByteArrayHandler.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt index f748ad7..d4bf5b0 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt @@ -1,50 +1,5 @@ package nl.astraeus.tba -class MutableByteArrayHandler( - buffer: SlicedByteArray -) : ByteArrayHandler(buffer) { - - constructor( - buffer: ByteArray, - offset: Int = 0, - length: Int = buffer.size - ) : this( - SlicedByteArray.wrap(buffer, offset, length) - ) - - operator fun set(index: Int, value: Byte) { - buffer[index] = value - } - - fun setShort(index: Int, value: Short) = buffer.setShort(index, value) - - fun setInt(index: Int, value: Int) = buffer.setInt(index, value) - - fun setLong(index: Int, value: Long) = buffer.setLong(index, value) - - 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) { - val bytes = value.encodeToByteArray() - check(bytes.size + 2 <= maxLength) { "String is too long" } - buffer.setShort(index, bytes.size.toShort()) - buffer.setBlob(index + 2, SlicedByteArray.wrap(bytes), maxLength - 2) - } - - fun setClob(index: Int, value: String, maxLength: Int) { - val bytes = value.encodeToByteArray() - check(bytes.size + 4 <= maxLength) { "Clob is too long" } - buffer.setInt(index, bytes.size) - buffer.setBlob(index + 4, SlicedByteArray.wrap(bytes), maxLength - 4) - } - - fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) { - check(bytes.size <= maxLength) { "Blob is too long" } - buffer.setBlob(index, bytes, maxLength) - } -} - open class ByteArrayHandler( var buffer: SlicedByteArray ) { @@ -103,3 +58,49 @@ open class ByteArrayHandler( return MutableByteArrayHandler(buffer = SlicedByteArray(buffer.data, 0, buffer.data.size)) } } + +class MutableByteArrayHandler( + buffer: SlicedByteArray +) : ByteArrayHandler(buffer) { + + constructor( + buffer: ByteArray, + offset: Int = 0, + length: Int = buffer.size + ) : this( + SlicedByteArray.wrap(buffer, offset, length) + ) + + operator fun set(index: Int, value: Byte) { + buffer[index] = value + } + + fun setShort(index: Int, value: Short) = buffer.setShort(index, value) + + fun setInt(index: Int, value: Int) = buffer.setInt(index, value) + + fun setLong(index: Int, value: Long) = buffer.setLong(index, value) + + 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) { + val bytes = value.encodeToByteArray() + check(bytes.size + 2 <= maxLength) { "String is too long" } + buffer.setShort(index, bytes.size.toShort()) + buffer.setBlob(index + 2, SlicedByteArray.wrap(bytes), maxLength - 2) + } + + fun setClob(index: Int, value: String, maxLength: Int) { + val bytes = value.encodeToByteArray() + check(bytes.size + 4 <= maxLength) { "Clob is too long" } + buffer.setInt(index, bytes.size) + buffer.setBlob(index + 4, SlicedByteArray.wrap(bytes), maxLength - 4) + } + + fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) { + check(bytes.size <= maxLength) { "Blob is too long" } + buffer.setBlob(index, bytes, maxLength) + } +} +