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.
npx @tessl/cli install tessl/maven-io-ktor--ktor-utils-jvm@3.2.0Ktor Utils JVM is a comprehensive utility library for the Ktor asynchronous web framework, providing essential functionality for cryptographic operations, date handling, platform-specific utilities, logging infrastructure, pipeline processing, I/O operations, network address handling, and various collection utilities. It serves as a foundational component for Ktor applications running on the JVM platform.
implementation("io.ktor:ktor-utils-jvm:3.2.0")import io.ktor.util.*
import io.ktor.util.pipeline.*
import io.ktor.util.logging.*
import io.ktor.util.date.*
import io.ktor.util.collections.*import io.ktor.util.*
import io.ktor.util.pipeline.*
// Create attributes for type-safe storage
val key = AttributeKey<String>("example")
val attributes = Attributes()
attributes.put(key, "value")
// Base64 encoding/decoding
val encoded = "Hello World".encodeBase64()
val decoded = encoded.decodeBase64String()
// Pipeline execution
val pipeline = Pipeline<String, Unit>()
pipeline.execute(Unit, "subject")
// Network address handling
val address = NetworkAddress("localhost", 8080)Ktor Utils JVM is organized around several key architectural components:
Type-safe attribute storage system for associating typed values with keys across application contexts.
data class AttributeKey<T : Any>(val name: String, private val type: TypeInfo = typeInfo<Any>())
interface Attributes {
fun <T : Any> get(key: AttributeKey<T>): T
fun <T : Any> getOrNull(key: AttributeKey<T>): T?
fun <T : Any> put(key: AttributeKey<T>, value: T)
fun <T : Any> remove(key: AttributeKey<T>)
fun <T : Any> computeIfAbsent(key: AttributeKey<T>, block: () -> T): T
val allKeys: List<AttributeKey<*>>
}
fun Attributes(concurrent: Boolean = false): AttributesCryptographic operations, encoding/decoding utilities, and secure random generation.
fun String.encodeBase64(): String
fun ByteArray.encodeBase64(): String
fun String.decodeBase64String(): String
fun String.decodeBase64Bytes(): ByteArray
fun hex(bytes: ByteArray): String
fun hex(s: String): ByteArray
fun sha1(bytes: ByteArray): ByteArray
fun generateNonce(): StringString handling utilities including case-insensitive operations and HTML processing.
interface StringValues {
val caseInsensitiveName: Boolean
operator fun get(name: String): String?
fun getAll(name: String): List<String>?
fun names(): Set<String>
fun entries(): Set<Map.Entry<String, List<String>>>
operator fun contains(name: String): Boolean
fun isEmpty(): Boolean
}
interface StringValuesBuilder {
operator fun set(name: String, value: String)
fun append(name: String, value: String)
fun appendAll(stringValues: StringValues)
fun remove(name: String)
fun build(): StringValues
}
fun valuesOf(vararg pairs: Pair<String, List<String>>, caseInsensitiveKey: Boolean = false): StringValues
fun String.escapeHTML(): StringSpecialized collection types including case-insensitive maps, concurrent collections, and caching utilities.
fun <V> caseInsensitiveMap(): MutableMap<String, V>
fun caseInsensitiveSet(): MutableSet<String>
interface ConcurrentMap<K, V> : MutableMap<K, V>
class CopyOnWriteHashMap<K, V> : MutableMap<K, V>
class LRUCache<K, V>(val maxSize: Int) : MutableMap<K, V>Asynchronous execution pipeline with configurable phases for request/response processing.
open class Pipeline<TSubject : Any, TContext : Any>(vararg phases: PipelinePhase) {
val attributes: Attributes
val developmentMode: Boolean
val isEmpty: Boolean
suspend fun execute(context: TContext, subject: TSubject): TSubject
fun addPhase(phase: PipelinePhase)
fun intercept(phase: PipelinePhase, block: PipelineInterceptor<TSubject, TContext>)
}
class PipelinePhase(val name: String)
class PipelineContext<TSubject : Any, TContext : Any>
typealias PipelineInterceptor<TSubject, TContext> = suspend PipelineContext<TSubject, TContext>.(TSubject) -> UnitNetwork address handling, file channel operations, and stream adapters for efficient I/O.
typealias NetworkAddress = java.net.SocketAddress
fun NetworkAddress(hostname: String, port: Int): NetworkAddress
val NetworkAddress.hostname: String
val NetworkAddress.port: Int
// File channel operations
fun File.readChannel(): ByteReadChannel
fun File.writeChannel(append: Boolean = false): ByteWriteChannel
// Stream adapters
fun ByteReadChannel.toInputStream(): InputStream
fun ByteWriteChannel.toOutputStream(): OutputStream
// NIO utilities
fun ReadableByteChannel.readChannel(): ByteReadChannel
fun WritableByteChannel.writeChannel(): ByteWriteChannelType conversion service with customizable converters for data transformation.
interface ConversionService {
fun <T> fromValues(values: List<String>, type: TypeInfo): T
fun <T> toValues(value: T?): List<String>
}
interface DataConversion {
fun <T> convert(value: String, type: TypeInfo): T
}GMT date handling with HTTP date format support and timezone operations.
class GMTDate(
val seconds: Int,
val minutes: Int,
val hours: Int,
val dayOfMonth: Int,
val month: Month,
val year: Int,
val dayOfWeek: WeekDay,
val dayOfYear: Int,
val timestamp: Long
)
enum class Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }
enum class WeekDay { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
fun GMTDate(timestamp: Long): GMTDate
fun GMTDate.toHttpDate(): String
fun String.fromHttpDate(): GMTDateMulti-level logging interface with lazy evaluation for performance optimization.
interface Logger {
fun trace(message: String)
fun debug(message: String)
fun info(message: String)
fun warn(message: String)
fun error(message: String)
fun error(message: String, exception: Throwable)
}
class KtorSimpleLogger(name: String, val level: LogLevel = LogLevel.INFO) : Logger
fun KtorSimpleLogger(name: String): LoggerContent encoding and compression utilities including GZip and Deflate support.
interface Encoder {
fun encode(source: ByteReadChannel): ByteReadChannel
}
interface Decoder {
fun decode(source: ByteReadChannel): ByteReadChannel
}
abstract class ContentEncoder : Encoder {
abstract val name: String
}
object GZipEncoder : ContentEncoder
object DeflateEncoder : ContentEncoder// Type information for reflection
class TypeInfo
inline fun <reified T> typeInfo(): TypeInfo
// Hash function interface
interface HashFunction {
fun digest(data: ByteArray): ByteArray
}
// Nonce management
interface NonceManager {
suspend fun newNonce(): String
suspend fun verifyNonce(nonce: String): Boolean
}