Fix off-by-one error
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package nl.astraeus.tba
|
||||
|
||||
class MutableByteArrayHandler(
|
||||
initialSize: Int = 1024,
|
||||
buffer: SlicedByteArray = SlicedByteArray.wrap(ByteArray(initialSize), 0, initialSize),
|
||||
) : ByteArrayHandler(initialSize, buffer) {
|
||||
buffer: SlicedByteArray
|
||||
) : ByteArrayHandler(buffer) {
|
||||
|
||||
constructor(buffer: ByteArray, range: IntRange = buffer.indices) : this(
|
||||
buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first)
|
||||
constructor(
|
||||
buffer: ByteArray,
|
||||
offset: Int = 0,
|
||||
length: Int = buffer.size
|
||||
) : this(
|
||||
SlicedByteArray.wrap(buffer, offset, length)
|
||||
)
|
||||
|
||||
operator fun set(index: Int, value: Byte) {
|
||||
@@ -30,12 +33,11 @@ class MutableByteArrayHandler(
|
||||
}
|
||||
|
||||
open class ByteArrayHandler(
|
||||
size: Int = 1024,
|
||||
var buffer: SlicedByteArray = SlicedByteArray.wrap(ByteArray(size), 0, size),
|
||||
var buffer: SlicedByteArray
|
||||
) {
|
||||
|
||||
constructor(buffer: ByteArray, range: IntRange = buffer.indices) : this(
|
||||
buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first)
|
||||
buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first + 1)
|
||||
)
|
||||
|
||||
operator fun get(index: Int): Byte {
|
||||
@@ -70,7 +72,7 @@ open class ByteArrayHandler(
|
||||
|
||||
fun slice(range: IntRange): ByteArrayHandler {
|
||||
return ByteArrayHandler(
|
||||
buffer = SlicedByteArray(this.buffer.data, range.first, range.last - range.first)
|
||||
buffer = SlicedByteArray(this.buffer.data, range.first, range.last - range.first + 1)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ class SlicedByteArray(
|
||||
val size: Int
|
||||
get() = length
|
||||
|
||||
constructor(size: Int) : this(ByteArray(size), 0, size)
|
||||
|
||||
operator fun get(index: Int): Byte {
|
||||
return data[offset + index]
|
||||
}
|
||||
@@ -83,7 +85,7 @@ class SlicedByteArray(
|
||||
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).decodeToString()
|
||||
val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length + 1).decodeToString()
|
||||
return str
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package nl.astraeus.tba
|
||||
|
||||
open class TypedByteArray(
|
||||
val definition: ByteArrayDefinition,
|
||||
var data: MutableByteArrayHandler = MutableByteArrayHandler(definition.size),
|
||||
var data: MutableByteArrayHandler = MutableByteArrayHandler(SlicedByteArray(definition.size)),
|
||||
) {
|
||||
val typeMap = mutableMapOf<String, Type>()
|
||||
val indexMap = mutableMapOf<String, Int>()
|
||||
@@ -22,6 +22,7 @@ open class TypedByteArray(
|
||||
constructor(vararg types: Type): this(ByteArrayDefinition(*types))
|
||||
|
||||
constructor(data: ByteArray): this(ByteArrayDefinition()) {
|
||||
check(data.size == definition.size) { "Data size ${data.size} does not match definition size ${definition.size}" }
|
||||
this.data = MutableByteArrayHandler(buffer = data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package nl.astraeus.tba
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class MutableByteArrayHandlerTest {
|
||||
|
||||
@Test
|
||||
fun testSimpleTypedByteArray() {
|
||||
val mbah = MutableByteArrayHandler(SlicedByteArray(10))
|
||||
|
||||
assertEquals(10, mbah.buffer.size)
|
||||
assertEquals(10, mbah.buffer.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleTypedByteArray2() {
|
||||
val mbah = MutableByteArrayHandler(ByteArray(468))
|
||||
|
||||
assertEquals(468, mbah.buffer.size)
|
||||
assertEquals(468, mbah.buffer.length)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user