Skip to content

Commit

Permalink
Use a better implementation for UpdatableByteArrayInputStream
Browse files Browse the repository at this point in the history
Co-authored-by: viztea <[email protected]>
  • Loading branch information
DRSchlaubi and viztea committed Nov 6, 2024
1 parent cd550be commit 2b66951
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions gateway/src/jvmMain/kotlin/ZstdDecompressor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package dev.kord.gateway
import com.github.luben.zstd.ZstdInputStream
import io.ktor.websocket.*
import java.io.ByteArrayInputStream
import java.io.InputStream

internal actual fun ZstdDecompressor() = object : Decompressor {

private val input = UpdatableByteArrayInputStream()
private val zstdStream = ZstdInputStream(input).apply { continuous = true }

override fun Frame.decompress(): String {
input.updateDelegate(data)
input.update(data)
return zstdStream.readBytes().decodeToString()
}

Expand All @@ -20,14 +19,10 @@ internal actual fun ZstdDecompressor() = object : Decompressor {
}
}

private class UpdatableByteArrayInputStream : InputStream() {
private var delegate: ByteArrayInputStream? = null

private val d: InputStream get() = delegate ?: error("No data available")

override fun read(): Int = d.read()

fun updateDelegate(bytes: ByteArray) {
delegate = ByteArrayInputStream(bytes)
private class UpdatableByteArrayInputStream : ByteArrayInputStream(ByteArray(0)) {
fun update(newBuf: ByteArray) {
this.pos = 0
this.buf = newBuf
this.count = newBuf.size
}
}

0 comments on commit 2b66951

Please sign in to comment.