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.
—
Specialized collection types including case-insensitive maps, concurrent collections, and caching utilities for efficient data handling in Ktor applications.
Collections that handle string keys in a case-insensitive manner, useful for HTTP headers and similar use cases.
/**
* Create an instance of case-insensitive mutable map
*/
fun <Value : Any> caseInsensitiveMap(): MutableMap<String, Value>
/**
* A map with case-insensitive String keys
*/
class CaseInsensitiveMap<Value : Any> : MutableMap<String, Value> {
override val size: Int
override fun containsKey(key: String): Boolean
override fun containsValue(value: Value): Boolean
override fun get(key: String): Value?
override fun isEmpty(): Boolean
override fun clear()
override fun put(key: String, value: Value): Value?
override fun putAll(from: Map<out String, Value>)
override fun remove(key: String): Value?
override val keys: MutableSet<String>
override val entries: MutableSet<MutableMap.MutableEntry<String, Value>>
override val values: MutableCollection<Value>
}Usage Examples:
import io.ktor.util.*
// Create case-insensitive map
val headers = caseInsensitiveMap<String>()
// Keys are case-insensitive
headers["Content-Type"] = "application/json"
headers["content-type"] = "text/plain" // Overwrites previous value
headers["CONTENT-TYPE"] = "text/html" // Overwrites again
println(headers["Content-Type"]) // "text/html"
println(headers["content-type"]) // "text/html"
println(headers["CONTENT-TYPE"]) // "text/html"
// Direct instantiation
val customMap = CaseInsensitiveMap<Int>()
customMap["Hello"] = 1
customMap["HELLO"] = 2
customMap["hello"] = 3
println(customMap.size) // 1 (all keys are the same)
println(customMap["Hello"]) // 3Thread-safe collection implementations for concurrent access patterns (expect/actual implementations).
/**
* Ktor concurrent map implementation (expect/actual)
*/
expect class ConcurrentMap<Key, Value>(
initialCapacity: Int = INITIAL_CAPACITY
) : MutableMap<Key, Value> {
/** Computes block and inserts result in map. The block will be evaluated at most once. */
fun computeIfAbsent(key: Key, block: () -> Value): Value
/** Removes key from map if it is mapped to value */
fun remove(key: Key, value: Value): Boolean
// Standard MutableMap operations
override fun remove(key: Key): Value?
override fun clear()
override fun put(key: Key, value: Value): Value?
override fun putAll(from: Map<out Key, Value>)
override val entries: MutableSet<MutableMap.MutableEntry<Key, Value>>
override val keys: MutableSet<Key>
override val values: MutableCollection<Value>
override fun containsKey(key: Key): Boolean
override fun containsValue(value: Value): Boolean
override fun get(key: Key): Value?
override fun isEmpty(): Boolean
override val size: Int
}Usage Examples:
import io.ktor.util.collections.*
// Create concurrent map with custom initial capacity
val cache = ConcurrentMap<String, ExpensiveObject>(64)
// Thread-safe lazy computation
val result = cache.computeIfAbsent("key1") {
// This block will be executed at most once, even with concurrent access
ExpensiveObject.create()
}
// Thread-safe conditional removal
val wasRemoved = cache.remove("key1", result) // Only removes if mapped to 'result'
// Standard map operations are thread-safe
cache["key2"] = AnotherObject()
val value = cache["key2"]
// Use in concurrent scenarios
val concurrentCache = ConcurrentMap<Int, String>()
// Multiple threads can safely access
Thread {
concurrentCache.computeIfAbsent(1) { "Thread 1 value" }
}.start()
Thread {
concurrentCache.computeIfAbsent(1) { "Thread 2 value" }
}.start()
// Only one of the blocks will executeUtilities for working with sets including platform-specific immutability support.
/**
* Freeze selected set. May do nothing on some platforms (expect/actual)
*/
expect fun <T> Set<T>.unmodifiable(): Set<T>Usage Examples:
import io.ktor.util.*
// Create mutable set and make it unmodifiable
val mutableSet = mutableSetOf("apple", "banana", "cherry")
val immutableSet = mutableSet.unmodifiable()
// immutableSet is now read-only (platform-dependent implementation)
// Attempts to modify will throw exceptions on platforms that support it
// Use with collections that need to be frozen
val configuredOptions = setOf("option1", "option2", "option3").unmodifiable()Additional collection utilities available specifically on the JVM platform.
/**
* Create a new instance of thread safe LRU cache (JVM only)
*/
fun <K, V> createLRUCache(
supplier: (K) -> V,
close: (V) -> Unit,
maxSize: Int
): Map<K, V>Usage Examples:
import io.ktor.util.*
// Create LRU cache with supplier and cleanup functions (JVM only)
val imageCache = createLRUCache<String, BufferedImage>(
supplier = { path -> loadImageFromDisk(path) },
close = { image -> image.flush() },
maxSize = 100
)
// Cache automatically manages size and calls cleanup
val image1 = imageCache["image1.png"] // Loads from disk
val image2 = imageCache["image1.png"] // Returns cached version
val image3 = imageCache["image2.png"] // Loads from disk
// When cache exceeds maxSize, oldest entries are removed and close() is calledInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-utils-jvm