Set correct length on blob, fix off by one error

This commit is contained in:
2024-09-06 16:43:09 +02:00
parent 85c3dd8292
commit 35f9be0504
3 changed files with 10 additions and 9 deletions

View File

@@ -66,7 +66,7 @@ open class ByteArrayHandler(
fun getClob(index: Int): String = buffer.getClob(index)
fun getBlob(index: Int): SlicedByteArray = buffer.getBlob(index)
fun getBlob(index: Int, length: Int): SlicedByteArray = buffer.getBlob(index, length)
fun slice(range: IntRange): ByteArrayHandler {
return ByteArrayHandler(

View File

@@ -163,7 +163,8 @@ class BlobProperty(
name: String
) : ByteArrayPropertyWithLength<SlicedByteArray>(name) {
override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): SlicedByteArray {
return thisRef.data.getBlob(getIndex(thisRef))
val result = thisRef.data.getBlob(getIndex(thisRef), getMaxLength(thisRef))
return result
}
override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: SlicedByteArray) {

View File

@@ -24,7 +24,7 @@ class SlicedByteArray(
}
fun getShort(index: Int): Short {
check(offset + index + 1 < offset + length) { "Index out of bounds" }
check(offset + index + 1 < offset + length) { "Index out of bounds, ${offset + index + 1} > ${offset + length} ($offset, $index, $length)" }
return ((data[offset + index].toInt() shl 8) or (data[offset + index + 1].toInt() and 0xff)).toShort()
}
@@ -82,14 +82,14 @@ class SlicedByteArray(
fun getClob(index: Int): String {
val length = getInt(index)
check(offset + index + length < offset + this.length) { "Index out of bounds" }
check(offset + index + length <= offset + this.length) { "Index out of bounds" }
val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length).decodeToString()
return str
}
fun getBlob(index: Int): SlicedByteArray {
val length = getInt(index)
check(offset + index + length < offset + this.length) { "Index out of bounds" }
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)
return str
}
@@ -143,7 +143,7 @@ class SlicedByteArray(
throw IllegalArgumentException("Blob is too long")
}
this.setInt(offset + index, bytes.size)
bytes.copyInto(data, offset + index + 4, maxLength - 4)
bytes.copyInto(data, offset + index + 4, maxLength)
}
companion object {
@@ -152,7 +152,7 @@ class SlicedByteArray(
offset: Int = 0,
length: Int = data.size
): SlicedByteArray {
return SlicedByteArray(data, offset,length)
return SlicedByteArray(data, offset, length)
}
}
}