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.
—
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.
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
}
}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)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)
}
}Identity - No-op encoder for compatibilityIdentityEncoder - Named content encoder for HTTP "identity" encodingGZipEncoder - Platform-specific GZip compressionDeflateEncoder - Platform-specific Deflate compressionInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-utils-jvm