|
|
|
|
@@ -28,7 +28,7 @@ open class TypedByteArray(
|
|
|
|
|
/**
|
|
|
|
|
* The definition of the byte array structure.
|
|
|
|
|
*/
|
|
|
|
|
val definition: ByteArrayDefinition = ByteArrayDefinition(*types)
|
|
|
|
|
private var definition: ByteArrayDefinition = ByteArrayDefinition(*types)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The handler for the underlying byte array data.
|
|
|
|
|
@@ -47,14 +47,38 @@ open class TypedByteArray(
|
|
|
|
|
val indexMap = mutableMapOf<String, Int>()
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
createTypeAndIndexMap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun definitionSize() = definition.size
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the internal structures with the provided types. This method reinitializes
|
|
|
|
|
* the necessary definitions, data handlers, and mapping structures based on the input types.
|
|
|
|
|
*
|
|
|
|
|
* @param types The list of types to update the internal data structures. Each type specifies
|
|
|
|
|
* its name, data type, and size. At least one type should be provided.
|
|
|
|
|
*
|
|
|
|
|
* WARNING: This method is not thread-safe and should be used with extreme caution.
|
|
|
|
|
* Updating types at runtime can lead to data corruption and inconsistencies if not handled properly.
|
|
|
|
|
*/
|
|
|
|
|
fun updateTypes(vararg types: Type) {
|
|
|
|
|
definition = ByteArrayDefinition(*types)
|
|
|
|
|
data = MutableByteArrayHandler(SlicedByteArray(definition.size))
|
|
|
|
|
indexMap.clear()
|
|
|
|
|
typeMap.clear()
|
|
|
|
|
createTypeAndIndexMap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun createTypeAndIndexMap() {
|
|
|
|
|
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}" }
|
|
|
|
|
check (!indexMap.containsKey(type.name)) { "Duplicate type name ${type.name}" }
|
|
|
|
|
check(!typeMap.containsKey(type.name)) { "Duplicate type name ${type.name}" }
|
|
|
|
|
check(!indexMap.containsKey(type.name)) { "Duplicate type name ${type.name}" }
|
|
|
|
|
|
|
|
|
|
typeMap[type.name] = type
|
|
|
|
|
indexMap[type.name] = index
|
|
|
|
|
|