Package io.ktor.util

Types

AlwaysFailNonceManager

object AlwaysFailNonceManager : NonceManager

Stub implementation that always fails. Will be removed so no public signatures should rely on it

AttributeKey

class AttributeKey<T>

Specifies a key for an attribute in Attributes

Attributes

interface Attributes

Map of attributes accessible by AttributeKey in a typed manner

CaseInsensitiveMap

class CaseInsensitiveMap<Value> : MutableMap<String, Value>

A map with case insensitive String keys

CaseInsensitiveSet

class CaseInsensitiveSet : MutableSet<String>

ConversionService

interface ConversionService

Data conversion service that does serialization and deserialization to/from list of strings

CopyOnWriteHashMap

class CopyOnWriteHashMap<K : Any, V : Any>

This is an internal implementation for copy-on-write concurrent map. It is very limited since it is not intended as general purpose implementation.

DefaultConversionService

object DefaultConversionService : ConversionService

The default conversion service that supports only basic types and enums

Digest

interface Digest

Stateful digest class specified to calculate digest.

DispatcherWithShutdown

class DispatcherWithShutdown : CoroutineDispatcher

Specialized dispatcher useful for graceful shutdown

Encoder

interface Encoder

Content encoder.

GenerateOnlyNonceManager

object GenerateOnlyNonceManager : NonceManager

This implementation does only generate nonce values but doesn’t validate them. This is recommended for testing only.

Hash

object Hash

Identity

object Identity : Encoder

Empty Encoder

Lock

expect class Lock

NonceManager

interface NonceManager

Represents a nonce manager. It’s responsibility is to produce nonce values and verify nonce values from untrusted sources that they are provided by this manager. This is usually required in web environment to mitigate CSRF attacks. Depending on it’s underlying implementation it could be stateful or stateless. Note that there is usually some timeout for nonce values to reduce memory usage and to avoid replay attacks. Nonce length is unspecified.

PlatformUtils

expect object PlatformUtils

StatelessHmacNonceManager

class StatelessHmacNonceManager : NonceManager

Stateless nonce manager implementation with HMAC verification and timeout. Every nonce provided by this manager consist of a random part, timestamp and HMAC.

StringValues

interface StringValues

Provides data structure for associating a String with a List of Strings

StringValuesBuilder

open class StringValuesBuilder

StringValuesImpl

open class StringValuesImpl : StringValues

StringValuesSingleImpl

open class StringValuesSingleImpl : StringValues

Annotations

InternalAPI

annotation class InternalAPI

API marked with this annotation is ktor internal and it is not intended to be used outside. It could be modified or removed without any notice. Using it outside of ktor could cause undefined behaviour and/or any strange effects.

KtorExperimentalAPI

annotation class KtorExperimentalAPI

API marked with this annotation is experimental and is not guaranteed to be stable.

Exceptions

DataConversionException

class DataConversionException : Exception

Thrown when failed to convert value

Extensions for External Classes

java.io.File

java.nio.ByteBuffer

java.nio.channels.ReadableByteChannel

java.nio.channels.WritableByteChannel

java.nio.file.Path

java.util.Date

kotlin.ByteArray

kotlin.Char

kotlin.String

kotlin.Throwable

kotlin.collections.List

kotlin.collections.Set

kotlin.ranges.LongRange

kotlinx.coroutines.Job

org.slf4j.Logger

Properties

Deflate

val Deflate: Encoder

Implementation of Deflate Encoder.

GZip

val GZip: Encoder

Implementation of GZip Encoder.

GreenwichMeanTime

val GreenwichMeanTime: ZoneId

ZoneId for GMT

Functions

Attributes

expect fun Attributes(
    concurrent: Boolean = false
): Attributes

Create attributes instance suitable for the particular platform

Digest

expect fun Digest(name: String): Digest

Create Digest from specified hash name.

SilentSupervisor

fun SilentSupervisor(parent: Job? = null): <ERROR CLASS>

Supervisor with empty coroutine exception handler ignoring all exceptions.

appendAll

Append all values from the specified builder

appendFiltered

fun StringValuesBuilder.appendFiltered(
    source: StringValues,
    keepEmpty: Boolean = false,
    predicate: (String, String) -> Boolean
): Unit

Append values from source filtering values by the specified predicate

asStream

fun Input.asStream(): InputStream

Convert io.ktor.utils.io Input to java InputStream

build

suspend fun Digest.build(bytes: ByteArray): ByteArray

Calculate digest from current state and specified bytes.

suspend fun Digest.build(
    string: String,
    charset: Charset = Charsets.UTF_8
): ByteArray

Calculate digest from current state and specified string.

caseInsensitiveMap

fun <Value> caseInsensitiveMap(): MutableMap<String, Value>

Create an instance of case insensitive mutable map. For internal use only.

copyToBoth

fun ByteReadChannel.copyToBoth(
    first: ByteWriteChannel,
    second: ByteWriteChannel
): Unit

Copy source channel to both output channels chunk by chunk.

createLRUCache

fun <K, V> createLRUCache(
    supplier: (K) -> V,
    close: (V) -> Unit,
    maxSize: Int
): Map<K, V>

Create a new instance of thread safe LRUCache and return it.

decodeBase64

fun ByteReadPacket.decodeBase64(): String

decodeBase64Bytes

fun ByteReadPacket.decodeBase64Bytes(): Input

Decode ByteReadPacket from base64 format

deflated

fun ByteReadChannel.deflated(
    gzip: Boolean = true,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool,
    coroutineContext: <ERROR CLASS> = Dispatchers.Unconfined
): ByteReadChannel
fun ByteWriteChannel.deflated(
    gzip: Boolean = true,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool,
    coroutineContext: <ERROR CLASS> = Dispatchers.Unconfined
): ByteWriteChannel

Launch a coroutine on coroutineContext that does deflate compression optionally doing CRC and writing GZIP header and trailer if gzip = true

encodeBase64

fun ByteReadPacket.encodeBase64(): String

Encode ByteReadPacket in base64 format

filter

fun StringValues.filter(
    keepEmpty: Boolean = false,
    predicate: (String, String) -> Boolean
): StringValues

Create a new instance of StringValues filtered by the specified predicate

flattenEntries

fun StringValues.flattenEntries(): List<Pair<String, String>>

Copy values to a list of pairs

flattenForEach

fun StringValues.flattenForEach(
    block: (String, String) -> Unit
): Unit

Invoke block function for every value pair

generateNonce

expect fun generateNonce(): String

Generates a nonce string. Could block if the system’s entropy source is empty

fun generateNonce(size: Int): ByteArray

Generates a nonce bytes of size. Could block if the system’s entropy source is empty

getDigestFunction

fun getDigestFunction(
    algorithm: String,
    salt: String
): (String) -> ByteArray

Create a digest function with the specified algorithm and salt

fun getDigestFunction(
    algorithm: String,
    salt: (value: String) -> String
): (String) -> ByteArray

Create a digest function with the specified algorithm and salt provider.

getOrFail

fun Parameters.getOrFail(name: String): String

Get parameters value associated with this name or fail with MissingRequestParameterException

getValue

operator fun <R : Any> Parameters.getValue(
    thisRef: Any?,
    property: KProperty<*>
): R

Operator function that allows to delegate variables by call parameters. It does conversion to type R using DefaultConversionService

hex

fun hex(bytes: ByteArray): String

Encode bytes as a HEX string with no spaces, newlines and 0x prefixes.

fun hex(s: String): ByteArray

Decode bytes from HEX string. It should be no spaces and 0x prefixes.

sha1

expect fun sha1(bytes: ByteArray): ByteArray

Compute SHA-1 hash for the specified bytes

split

fun ByteReadChannel.split(
    coroutineScope: CoroutineScope
): Pair<ByteReadChannel, ByteReadChannel>

Split source ByteReadChannel into 2 new one. Cancel of one channel in split(input or both outputs) cancels other channels.

toByteArray

suspend fun ByteReadChannel.toByteArray(): ByteArray

Read channel to byte array.

toMap

fun StringValues.toMap(): Map<String, List<String>>

Copy values to a new independent map

url

fun url(block: URLBuilder.() -> Unit): String

Construct a URL

fun ApplicationCall.url(
    block: URLBuilder.() -> Unit = {}
): String

Creates an url using current call’s schema, path and parameters as initial and then invokes block function on the url builder so amend parameters

valuesOf

fun valuesOf(
    vararg pairs: Pair<String, List<String>>,
    caseInsensitiveKey: Boolean = false
): StringValues

Build an instance of StringValues from a vararg list of pairs

fun valuesOf(
    name: String,
    value: String,
    caseInsensitiveKey: Boolean = false
): StringValues

Build an instance of StringValues from a single pair

fun valuesOf(
    name: String,
    values: List<String>,
    caseInsensitiveKey: Boolean = false
): StringValues

Build an instance of StringValues with a single name and multiple values

fun valuesOf(): StringValues

Build an empty StringValues instance.

fun valuesOf(
    map: Map<String, Iterable<String>>,
    caseInsensitiveKey: Boolean = false
): StringValues

Build an instance of StringValues from the specified map

withLock

fun <R> Lock.withLock(block: () -> R): R

Companion Object Functions

createFromCall

fun URLBuilder.Companion.createFromCall(
    call: ApplicationCall
): URLBuilder

Creates an url using current call’s schema, path and parameters as initial