StreamingCompressor

Stateful streaming compressor that processes data incrementally. Useful for compressing data that arrives in chunks (e.g., from network).

Usage:

val compressor = StreamingCompressor.create()
try {
while (hasMoreData) {
val chunk = receiveChunk()
compressor.compress(chunk) { compressedChunk ->
send(compressedChunk)
}
}
compressor.finish { finalChunk ->
send(finalChunk)
}
} finally {
compressor.close()
}

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The buffer allocator used by this compressor.

Functions

Link copied to clipboard
expect abstract fun close()
Link copied to clipboard
abstract fun compress(input: ReadBuffer, onOutput: (ReadBuffer) -> Unit)

Compresses input data, invoking the callback for each output chunk. May invoke callback zero or more times depending on buffering.

Link copied to clipboard
abstract fun finish(onOutput: (ReadBuffer) -> Unit)

Finishes compression, flushing any buffered data. Must be called after all input has been provided.

Link copied to clipboard
abstract fun flush(onOutput: (ReadBuffer) -> Unit)

Flushes buffered data using Z_SYNC_FLUSH, producing complete deflate blocks. Unlike finish, the stream remains open for more data.

Link copied to clipboard
abstract fun reset()

Resets the compressor to initial state for reuse. More efficient than creating a new compressor.

Link copied to clipboard
inline fun <R> StreamingCompressor.use(noinline onOutput: (ReadBuffer) -> Unit, block: (compress: (ReadBuffer) -> Unit) -> R): R

Convenience function that handles compress, finish, and close automatically. All output chunks (from both compress and finish) go to the same callback.

Link copied to clipboard
inline suspend fun <R> StreamingCompressor.useSuspending(noinline onOutput: (ReadBuffer) -> Unit, block: suspend (compress: (ReadBuffer) -> Unit) -> R): R

Suspending version of use for use with suspending I/O. Uses the efficient synchronous compressor but allows suspend calls in the block.