Package io.ktor.util

Types

Link copied to clipboard
object AlwaysFailNonceManager : NonceManager

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

Link copied to clipboard
class AttributeKey<T>(name: String)

Specifies a key for an attribute in Attributes

Link copied to clipboard
interface Attributes

Map of attributes accessible by AttributeKey in a typed manner

Link copied to clipboard
class CaseInsensitiveMap<Value> : MutableMap<String, Value>

A map with case insensitive String keys

Link copied to clipboard
class CaseInsensitiveSet : MutableSet<String>
Link copied to clipboard
interface Digest

Stateful digest class specified to calculate digest.

Link copied to clipboard
interface Encoder

Content encoder.

Link copied to clipboard
object GenerateOnlyNonceManager : NonceManager

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

Link copied to clipboard
object Hash
Link copied to clipboard
object Identity : Encoder

Empty Encoder that doesn't do any changes.

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard
expect class Lock
actual class Lock
actual class Lock
Link copied to clipboard
interface NonceManager

Represents a nonce manager. Its 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.

Link copied to clipboard
expect object PlatformUtils
actual object PlatformUtils
actual object PlatformUtils
Link copied to clipboard

API marked with this annotation is intended to become public in the future version. Usually it means that the API can't be public at the moment of development due to compatibility guarantees restrictions.

Link copied to clipboard
class StatelessHmacNonceManager(keySpec: SecretKeySpec, algorithm: String, timeoutMillis: Long, nonceGenerator: () -> String) : NonceManager

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

Link copied to clipboard
interface StringValues

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

Link copied to clipboard
open class StringValuesBuilder(caseInsensitiveName: Boolean, size: Int)
Link copied to clipboard
open class StringValuesImpl(caseInsensitiveName: Boolean, values: Map<String, List<String>>) : StringValues
Link copied to clipboard
open class StringValuesSingleImpl(caseInsensitiveName: Boolean, name: String, values: List<String>) : StringValues

Functions

Link copied to clipboard
fun StringValuesBuilder.appendAll(builder: StringValuesBuilder): StringValuesBuilder

Append all values from the specified builder

Link copied to clipboard
fun StringValuesBuilder.appendFiltered(source: StringValues, keepEmpty: Boolean = false, predicate: (String, String) -> Boolean)

Append values from source filtering values by the specified predicate

Link copied to clipboard
fun Input.asStream(): InputStream

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

Link copied to clipboard
expect fun Attributes(concurrent: Boolean = false): Attributes

Create attributes instance suitable for the particular platform

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

Create JVM specific attributes instance.

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

Create native specific attributes instance.

Link copied to clipboard
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.

Link copied to clipboard
fun <Value> caseInsensitiveMap(): MutableMap<String, Value>

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

Link copied to clipboard
inline fun String.chomp(separator: String, onMissingDelimiter: () -> Pair<String, String>): Pair<String, String>

Splits the given string into two parts before and after separator.

Link copied to clipboard
fun File.combineSafe(relativePath: Path): File
fun File.combineSafe(relativePath: String): File
fun Path.combineSafe(relativePath: Path): File

Append a relativePath safely that means that adding any extra .. path elements will not let access anything out of the reference directory (unless you have symbolic or hard links or multiple mount points)

Link copied to clipboard
operator fun LongRange.contains(other: LongRange): Boolean

Returns true if other range is fully contained inside this range

Link copied to clipboard
fun ByteBuffer.copy(size: Int = remaining()): ByteBuffer

Moves all bytes in this buffer to a newly created buffer with the optionally specified size

fun ByteBuffer.copy(pool: ObjectPool<ByteBuffer>, size: Int = remaining()): ByteBuffer

Moves all bytes in this buffer to a newly created buffer with the optionally specified size by allocating it from the given pool

Link copied to clipboard
fun ByteReadChannel.copyToBoth(first: ByteWriteChannel, second: ByteWriteChannel)

Copy source channel to both output channels chunk by chunk.

Link copied to clipboard
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.

Link copied to clipboard
fun ByteReadPacket.decodeBase64(): String
fun String.decodeBase64(): String
fun decodeBase64(encodedString: String): ByteArray

Decode bytes from a BASE64 string encodedString

Link copied to clipboard
fun ByteReadPacket.decodeBase64Bytes(): Input

Decode ByteReadPacket from base64 format

fun String.decodeBase64Bytes(): ByteArray

Decode String from base64 format

Link copied to clipboard
fun String.decodeBase64String(): String

Decode String from base64 format encoded in UTF-8.

Link copied to clipboard
fun ByteBuffer.decodeString(charset: Charset = Charsets.UTF_8): String

Decodes a string from this buffer with the specified charset

Link copied to clipboard
fun ByteReadChannel.deflated(gzip: Boolean = true, pool: ObjectPool<ByteBuffer> = KtorDefaultPool, coroutineContext: CoroutineContext = Dispatchers.Unconfined): ByteReadChannel
fun ByteWriteChannel.deflated(gzip: Boolean = true, pool: ObjectPool<ByteBuffer> = KtorDefaultPool, coroutineContext: CoroutineContext = Dispatchers.Unconfined): ByteWriteChannel

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

Link copied to clipboard
expect fun Digest(name: String): Digest

Create Digest from specified hash name.

actual fun Digest(name: String): Digest

Create Digest from specified hash name.

actual fun Digest(name: String): Digest

Create Digest from specified hash name.

Link copied to clipboard
fun ByteReadPacket.encodeBase64(): String

Encode ByteReadPacket in base64 format

fun ByteArray.encodeBase64(): String

Encode ByteArray in base64 format

fun String.encodeBase64(): String

Encode String in base64 format and UTF-8 character encoding.

fun encodeBase64(bytes: ByteArray): String

Encode bytes as a BASE64 string

Link copied to clipboard
fun Logger.error(exception: Throwable)

Logs an error from an exception using its message

Link copied to clipboard
fun String.escapeHTML(): String

Escapes the characters in a String using HTML entities

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

Create a new instance of StringValues filtered by the specified predicate

Link copied to clipboard
fun StringValues.flattenEntries(): List<Pair<String, String>>

Copy values to a list of pairs

Link copied to clipboard
fun StringValues.flattenForEach(block: (String, String) -> Unit)

Invoke block function for every value pair

Link copied to clipboard
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

actual fun generateNonce(): String

Generates a nonce string 16 characters long. Could block if the system's entropy source is empty

actual fun generateNonce(): String

Generates a nonce string 16 characters long. Could block if the system's entropy source is empty

Link copied to clipboard
fun getDigestFunction(algorithm: String, salt: (value: String) -> String): (String) -> ByteArray

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

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

Create a digest function with the specified algorithm and salt

Link copied to clipboard
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.

Link copied to clipboard
fun Char.isLowerCase(): Boolean

Check if Char is in lower case

Link copied to clipboard
fun ByteBuffer.moveTo(destination: ByteBuffer, limit: Int = Int.MAX_VALUE): Int

Moves bytes from this buffer to the destination buffer

Link copied to clipboard
fun ByteBuffer.moveToByteArray(): ByteArray

Moves bytes from this buffer into newly created ByteArray and returns it

Link copied to clipboard
fun File.normalizeAndRelativize(): File
fun Path.normalizeAndRelativize(): Path

Remove all redundant . and .. path elements. Leading .. are also considered redundant.

Link copied to clipboard
fun Job.printDebugTree(offset: Int = 0)

Print Job children tree.

Link copied to clipboard
fun Attributes.putAll(other: Attributes)

Adds all attributes from another collection, replacing original values if any.

Link copied to clipboard
fun ReadableByteChannel.read(buffer: IoBuffer): Int

Read from a NIO channel into the specified buffer Could return 0 if the channel is non-blocking or buffer has no free space

Link copied to clipboard
fun ByteArray.readShort(offset: Int): Short

Read Short in network order(BE) with specified offset from ByteArray.

Link copied to clipboard
expect fun sha1(bytes: ByteArray): ByteArray

Compute SHA-1 hash for the specified bytes

actual fun sha1(bytes: ByteArray): ByteArray

Compute SHA-1 hash for the specified bytes

actual fun sha1(bytes: ByteArray): ByteArray

Compute SHA-1 hash for the specified bytes

Link copied to clipboard
fun SilentSupervisor(parent: Job? = null): CoroutineContext

Supervisor with empty coroutine exception handler ignoring all exceptions.

Link copied to clipboard
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.

Link copied to clipboard
suspend fun ByteReadChannel.toByteArray(): ByteArray

Read channel to byte array.

Link copied to clipboard
fun String.toCharArray(): CharArray

Convert String to CharArray

Link copied to clipboard
fun String.toLowerCasePreservingASCIIRules(): String

Does the same as the regular toLowerCase except that locale-specific rules are not applied to ASCII characters so latin characters are converted by the original english rules.

Link copied to clipboard
fun StringValues.toMap(): Map<String, List<String>>

Copy values to a new independent map

Link copied to clipboard
fun String.toUpperCasePreservingASCIIRules(): String

Does the same as the regular toUpperCase except that locale-specific rules are not applied to ASCII characters so latin characters are converted by the original english rules.

Link copied to clipboard
expect fun <T> Set<T>.unmodifiable(): Set<T>

Freeze selected set. May do nothing on some platforms.

actual fun <T> Set<T>.unmodifiable(): Set<T>

Wraps into an unmodifiable set

actual fun <T> Set<T>.unmodifiable(): Set<T>
Link copied to clipboard
fun valuesOf(): StringValues

Build an empty StringValues instance.

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(map: Map<String, Iterable<String>>, caseInsensitiveKey: Boolean = false): StringValues

Build an instance of StringValues from the specified map

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

Link copied to clipboard
inline fun <R> Lock.withLock(crossinline block: () -> R): R
Link copied to clipboard
fun WritableByteChannel.write(buffer: IoBuffer): Int

Write bytes to a NIO channel from the specified buffer Could return 0 if the channel is non-blocking or buffer has no free space

Properties

Link copied to clipboard
val Deflate: Encoder

Implementation of Deflate Encoder.

Link copied to clipboard
val Path.extension: String

Finds an extension of the given Path

Link copied to clipboard
val GZip: Encoder

Implementation of GZip Encoder.

Link copied to clipboard
val LongRange.length: Long

Length of this LongRange

Link copied to clipboard
val Throwable.rootCause: Throwable?

Root cause of the Throwable.