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.
—
Type-safe attribute storage system for associating typed values with keys across different application contexts. The attributes system provides a way to store and retrieve typed values using keys, ensuring type safety at compile time.
Creates a typed key for storing values in an attributes map.
/**
* Creates an attribute key with the specified name and inferred type
* @param name Name of the attribute for diagnostic purposes
* @return AttributeKey instance for the specified type
*/
inline fun <reified T : Any> AttributeKey(name: String): AttributeKey<T>
/**
* Creates an attribute key with explicit type information
* @param name Name of the attribute
* @param type Type information for the attribute value
*/
data class AttributeKey<T : Any>(
val name: String,
private val type: TypeInfo = typeInfo<Any>()
)Usage Examples:
import io.ktor.util.*
// Create typed attribute keys
val userIdKey = AttributeKey<Int>("userId")
val sessionKey = AttributeKey<String>("sessionId")
val configKey = AttributeKey<Map<String, Any>>("config")
// Keys can be reused across different attribute instances
val attributes1 = Attributes()
val attributes2 = Attributes()
attributes1.put(userIdKey, 123)
attributes2.put(userIdKey, 456)Main interface for type-safe attribute storage and retrieval.
/**
* Map of attributes accessible by AttributeKey in a typed manner
*/
interface Attributes {
/** Gets a value or throws exception if not found */
operator fun <T : Any> get(key: AttributeKey<T>): T
/** Gets a value or returns null if not found */
fun <T : Any> getOrNull(key: AttributeKey<T>): T?
/** Checks if an attribute exists */
operator fun contains(key: AttributeKey<*>): Boolean
/** Creates or changes an attribute value */
fun <T : Any> put(key: AttributeKey<T>, value: T)
/** Creates or changes an attribute value (operator form) */
operator fun <T : Any> set(key: AttributeKey<T>, value: T)
/** Removes an attribute */
fun <T : Any> remove(key: AttributeKey<T>)
/** Removes and returns an attribute value, throws if not found */
fun <T : Any> take(key: AttributeKey<T>): T
/** Removes and returns an attribute value, returns null if not found */
fun <T : Any> takeOrNull(key: AttributeKey<T>): T?
/** Gets or computes a value if absent */
fun <T : Any> computeIfAbsent(key: AttributeKey<T>, block: () -> T): T
/** Returns list of all attribute keys */
val allKeys: List<AttributeKey<*>>
}Usage Examples:
import io.ktor.util.*
val attributes = Attributes()
val nameKey = AttributeKey<String>("name")
val ageKey = AttributeKey<Int>("age")
// Store values
attributes.put(nameKey, "Alice")
attributes[ageKey] = 30
// Retrieve values
val name = attributes[nameKey] // Returns "Alice"
val age = attributes.getOrNull(ageKey) // Returns 30 or null
// Check existence
if (nameKey in attributes) {
println("Name is present")
}
// Compute if absent
val defaultAge = attributes.computeIfAbsent(ageKey) { 25 }
// Remove values
val removedName = attributes.take(nameKey) // Returns and removes
attributes.remove(ageKey) // Just removesCreates platform-appropriate attributes instances.
/**
* Creates an attributes instance suitable for the particular platform
* @param concurrent Whether the attributes should be thread-safe
* @return Platform-specific Attributes implementation
*/
fun Attributes(concurrent: Boolean = false): AttributesUsage Examples:
import io.ktor.util.*
// Create standard attributes
val attrs = Attributes()
// Create thread-safe attributes for concurrent access
val concurrentAttrs = Attributes(concurrent = true)
// Use across multiple threads safely
val userKey = AttributeKey<String>("user")
concurrentAttrs.put(userKey, "Alice")Additional utility functions for working with attributes.
/**
* Adds all attributes from another collection, replacing original values if any
* @param other Source attributes to copy from
*/
fun Attributes.putAll(other: Attributes)Usage Examples:
import io.ktor.util.*
val source = Attributes()
val target = Attributes()
val nameKey = AttributeKey<String>("name")
val ageKey = AttributeKey<Int>("age")
source.put(nameKey, "Bob")
source.put(ageKey, 25)
// Copy all attributes from source to target
target.putAll(source)
println(target[nameKey]) // "Bob"
println(target[ageKey]) // 25The type system provides runtime type information for generic operations.
Represents type information for runtime type operations.
/**
* Provides type information for the specified generic type
* @return TypeInfo instance representing the type T
*/
inline fun <reified T> typeInfo(): TypeInfo
/**
* Type information holder for runtime type operations
*/
class TypeInfoUsage Examples:
import io.ktor.util.reflect.*
// Get type information for different types
val stringType = typeInfo<String>()
val listType = typeInfo<List<String>>()
val mapType = typeInfo<Map<String, Int>>()
// Use with attribute keys for explicit typing
val explicitKey = AttributeKey<List<String>>("items", typeInfo<List<String>>())Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-utils-jvm