All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.socket.streams.StreamExtensions.kt Maven / Gradle / Ivy

There is a newer version: 0.4.8
Show newest version
package socket.streams

public fun OutputStream.encodeVariableByteInteger(value: UInt): Int {
    var length = 0
    var x = value
    do {
        var encodedByte = x.rem(128u)
        x /= 128u
        if (x > 0u) {
            encodedByte = encodedByte or 128u
        }
        write(encodedByte.toUByte())
        length++
    } while (x > 0u)
    return length
}

public fun InputStream.decodeVariableByteInteger(): UInt {
    var multiplier = 1u
    var value = 0u
    do {
        val encodedByte = read().toUInt()
        value += (encodedByte and 127u) * multiplier
        if (multiplier > 128u * 128u * 128u) {
            throw Exception("Malformed Variable Byte Integer")
        }
        multiplier *= 128u
    } while ((encodedByte and 128u) != 0u)
    return value
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy