CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-utils-jvm

Ktor utilities library for JVM platform containing common utility functions, cryptographic operations, date handling, logging utilities, pipeline functionality, I/O adapters, encoding/decoding utilities, network address handling, and various platform-specific implementations for the Ktor framework.

Pending
Overview
Eval results
Files

compression.mddocs/

Compression & Encoding

Content encoding and compression utilities including GZip and Deflate support. Provides efficient compression and decompression capabilities for web content and data processing with coroutine support.

Capabilities

Encoder Interface

Base interface for content encoding and decoding operations with coroutine context support.

/**
 * Content encoder interface supporting both encoding and decoding operations
 */
interface Encoder {
    /** Launch coroutine to encode source bytes */
    fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext = EmptyCoroutineContext
    ): ByteReadChannel
    
    /** Launch coroutine to encode source bytes for writing */
    fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext = EmptyCoroutineContext
    ): ByteWriteChannel
    
    /** Launch coroutine to decode source bytes */
    fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext = EmptyCoroutineContext
    ): ByteReadChannel
}

Usage Examples:

import io.ktor.util.*
import kotlin.coroutines.EmptyCoroutineContext

// Use identity encoder (no-op)
val sourceChannel: ByteReadChannel = // ... source data
val encoded = Identity.encode(sourceChannel, EmptyCoroutineContext)
val decoded = Identity.decode(encoded, EmptyCoroutineContext)

// Custom encoder implementation
class CustomEncoder : Encoder {
    override fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel {
        // Custom encoding logic
        return source
    }
    
    override fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext
    ): ByteWriteChannel {
        // Custom encoding for write channel
        return source
    }
    
    override fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel {
        // Custom decoding logic
        return source
    }
}

ContentEncoder Interface

Interface for content encoders with HTTP header naming support and compression prediction.

/**
 * A request/response content encoder with HTTP header support
 */
interface ContentEncoder : Encoder {
    /** Encoder identifier to use in HTTP headers */
    val name: String
    
    /** Provides estimation for compressed length or null if impossible */
    fun predictCompressedLength(contentLength: Long): Long?
}

Usage Examples:

import io.ktor.util.*

// Use built-in identity encoder
val identityEncoder: ContentEncoder = IdentityEncoder
println("Encoder name: ${identityEncoder.name}") // "identity"

// Check compression prediction
val originalSize = 1024L
val predictedSize = identityEncoder.predictCompressedLength(originalSize)
println("Predicted size: $predictedSize") // 1024 (no compression)

// Use encoder
val sourceChannel: ByteReadChannel = // ... source data
val processed = identityEncoder.encode(sourceChannel)

Built-in Content Encoders

Platform-specific implementations of compression algorithms (expect/actual pattern).

/**
 * Identity encoder that doesn't modify content
 */
object IdentityEncoder : ContentEncoder {
    override val name: String
    override fun predictCompressedLength(contentLength: Long): Long
    
    override fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
    
    override fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext
    ): ByteWriteChannel
    
    override fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
}

/**
 * GZip content encoder (expect/actual implementation)
 */
object GZipEncoder : ContentEncoder {
    override val name: String
    
    override fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
    
    override fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext
    ): ByteWriteChannel
    
    override fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
}

/**
 * Deflate content encoder (expect/actual implementation)
 */
object DeflateEncoder : ContentEncoder {
    override val name: String
    
    override fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
    
    override fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext
    ): ByteWriteChannel
    
    override fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
}

/**
 * Empty encoder that doesn't modify content
 */
object Identity : Encoder {
    override fun encode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
    
    override fun encode(
        source: ByteWriteChannel,
        coroutineContext: CoroutineContext
    ): ByteWriteChannel
    
    override fun decode(
        source: ByteReadChannel,
        coroutineContext: CoroutineContext
    ): ByteReadChannel
}

Usage Examples:

import io.ktor.util.*
import kotlin.coroutines.EmptyCoroutineContext

// GZip compression
val originalData: ByteReadChannel = // ... source data
val gzipCompressed = GZipEncoder.encode(originalData, EmptyCoroutineContext)
val gzipDecompressed = GZipEncoder.decode(gzipCompressed, EmptyCoroutineContext)

// Deflate compression
val deflateCompressed = DeflateEncoder.encode(originalData, EmptyCoroutineContext)
val deflateDecompressed = DeflateEncoder.decode(deflateCompressed, EmptyCoroutineContext)

// Use in HTTP context
val responseData = "Large response content...".toByteArray()
val responseChannel = ByteReadChannel(responseData)

val encodedResponse = when (acceptedEncodings.contains("gzip")) {
    true -> {
        headers["Content-Encoding"] = GZipEncoder.name // "gzip"
        GZipEncoder.encode(responseChannel)
    }
    false -> {
        headers["Content-Encoding"] = IdentityEncoder.name // "identity"
        IdentityEncoder.encode(responseChannel)
    }
}

Implementation Notes

  • Platform Support: The Encoder and ContentEncoder interfaces are available on all platforms (Common)
  • Expect/Actual Pattern: GZipEncoder and DeflateEncoder have platform-specific implementations
  • Coroutine Context: All encoding/decoding operations support optional coroutine context for structured concurrency
  • HTTP Integration: ContentEncoder interface is designed for HTTP content encoding headers
  • Built-in Encoders:
    • Identity - No-op encoder for compatibility
    • IdentityEncoder - Named content encoder for HTTP "identity" encoding
    • GZipEncoder - Platform-specific GZip compression
    • DeflateEncoder - Platform-specific Deflate compression

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-utils-jvm

docs

attributes.md

collections.md

compression.md

conversion.md

crypto.md

datetime.md

index.md

io.md

logging.md

pipeline.md

strings.md

tile.json