
commonMain.maryk.core.protobuf.calculateKeyAndContentLength.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-jvm Show documentation
Show all versions of core-jvm Show documentation
Maryk is a Kotlin Multiplatform library which helps you to store, query and send data in a structured way over multiple platforms. The data store stores any value with a version, so it is possible to request only the changed data or live listen for updates.
The newest version!
package maryk.core.protobuf
import maryk.core.extensions.bytes.calculateVarByteLength
import maryk.core.protobuf.WireType.LENGTH_DELIMITED
/**
* Calculate key and content length for ProtoBuf encoding
* Pass [wireType], [index] and [contentLengthCalculator] to calculate length
* Values will be cached in [cacher]
*/
internal fun calculateKeyAndContentLength(
wireType: WireType,
index: UInt,
cacher: WriteCacheWriter,
contentLengthCalculator: () -> Int
): Int {
var totalByteLength = 0
totalByteLength += ProtoBuf.calculateKeyLength(index)
if (wireType == LENGTH_DELIMITED) {
// Take care length container is first cached before value is calculated
// Otherwise byte lengths contained by value could be cached before
// This way order is maintained
val container = ByteLengthContainer()
cacher.addLengthToCache(container)
// calculate field length
contentLengthCalculator().let {
container.length = it
totalByteLength += it
totalByteLength += it.calculateVarByteLength()
}
} else {
// calculate field length
totalByteLength += contentLengthCalculator()
}
return totalByteLength
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy