CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-utils-jvm

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.

Pending
Overview
Eval results
Files

io.mddocs/

I/O & Networking

Network address handling, file channel operations, and stream adapters for efficient I/O. Provides comprehensive utilities for network communication, file operations, and stream processing with JVM-specific optimizations.

Capabilities

Network Address Handling

Classes and utilities for working with network addresses and endpoints.

/**
 * Represents a network address with hostname and port
 * On JVM, this is a typealias to java.net.SocketAddress
 */
typealias NetworkAddress = java.net.SocketAddress

/**
 * Create a NetworkAddress with hostname and port
 * @param hostname Host name or IP address
 * @param port Port number
 * @return NetworkAddress instance (InetSocketAddress on JVM)
 */
fun NetworkAddress(hostname: String, port: Int): NetworkAddress

/**
 * Get hostname from NetworkAddress
 */
val NetworkAddress.hostname: String

/**
 * Get IP address string from NetworkAddress
 */
val NetworkAddress.address: String

/**
 * Get port from NetworkAddress
 */
val NetworkAddress.port: Int
}

Usage Examples:

import io.ktor.util.network.*

// Create network addresses
val localhost = NetworkAddress("localhost", 8080)
val remote = NetworkAddress("api.example.com", 443)

println(localhost) // "localhost:8080"

// Use in network operations
fun connectTo(address: NetworkAddress) {
    println("Connecting to ${address.hostname}:${address.port}")
}

connectTo(localhost)

File Channel Operations

JVM-specific utilities for efficient file I/O using NIO channels.

/**
 * Create a read channel from a File
 * @return ByteReadChannel for reading file content
 */
fun File.readChannel(): ByteReadChannel

/**
 * Create a write channel to a File
 * @param append Whether to append to existing file or overwrite
 * @return ByteWriteChannel for writing file content
 */
fun File.writeChannel(append: Boolean = false): ByteWriteChannel

/**
 * Read channel from file at NIO Path
 * @return ByteReadChannel for reading
 */
fun java.nio.file.Path.readChannel(): ByteReadChannel

/**
 * Write channel to file at NIO Path
 * @param append Whether to append to existing content
 * @return ByteWriteChannel for writing
 */
fun java.nio.file.Path.writeChannel(append: Boolean = false): ByteWriteChannel

Usage Examples:

import io.ktor.util.cio.*
import java.io.File
import java.nio.file.Paths

// File-based I/O
val file = File("data.txt")

// Read from file
val readChannel = file.readChannel()
val content = readChannel.readRemaining().readText()
println(content)

// Write to file
val writeChannel = file.writeChannel()
writeChannel.writeStringUtf8("Hello, World!")
writeChannel.close()

// Append to file
val appendChannel = file.writeChannel(append = true)
appendChannel.writeStringUtf8("\nAppended content")
appendChannel.close()

// NIO Path operations
val path = Paths.get("data.txt")
val pathReadChannel = path.readChannel()
val pathWriteChannel = path.writeChannel()

Stream Adapters

Convert between Ktor channels and standard Java I/O streams.

/**
 * Convert ByteReadChannel to InputStream
 * @return InputStream that reads from the channel
 */
fun ByteReadChannel.toInputStream(): InputStream

/**
 * Convert ByteWriteChannel to OutputStream  
 * @return OutputStream that writes to the channel
 */
fun ByteWriteChannel.toOutputStream(): OutputStream

/**
 * Convert InputStream to ByteReadChannel
 * @param pool ByteBuffer pool for efficient memory usage
 * @return ByteReadChannel that reads from the stream
 */
fun InputStream.toByteReadChannel(
    pool: ObjectPool<ByteBuffer> = ByteBufferPool
): ByteReadChannel

/**
 * Convert OutputStream to ByteWriteChannel
 * @param pool ByteBuffer pool for efficient memory usage
 * @return ByteWriteChannel that writes to the stream
 */
fun OutputStream.toByteWriteChannel(
    pool: ObjectPool<ByteBuffer> = ByteBufferPool
): ByteWriteChannel

Usage Examples:

import io.ktor.util.cio.*
import java.io.*

// Convert channel to stream for legacy APIs
val readChannel: ByteReadChannel = // ... from somewhere
val inputStream = readChannel.toInputStream()

// Use with standard Java APIs
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
val line = bufferedReader.readLine()

// Convert stream to channel
val fileInputStream = FileInputStream("data.txt")
val channelFromStream = fileInputStream.toByteReadChannel()

// Write channel to stream
val writeChannel: ByteWriteChannel = // ... 
val outputStream = writeChannel.toOutputStream()
val printWriter = PrintWriter(outputStream)
printWriter.println("Hello from stream")
printWriter.flush()

NIO Channel Operations

Low-level NIO channel utilities for advanced I/O operations.

/**
 * Create ByteReadChannel from ReadableByteChannel
 * @return ByteReadChannel that reads from NIO channel
 */
fun ReadableByteChannel.readChannel(): ByteReadChannel

/**
 * Create ByteWriteChannel from WritableByteChannel
 * @return ByteWriteChannel that writes to NIO channel
 */
fun WritableByteChannel.writeChannel(): ByteWriteChannel

/**
 * Create ByteReadChannel from FileChannel
 * @param start Starting position in the file
 * @param endInclusive Ending position in the file (inclusive)
 * @return ByteReadChannel for reading file range
 */
fun FileChannel.readChannel(
    start: Long = 0,
    endInclusive: Long = -1
): ByteReadChannel

/**
 * Create ByteWriteChannel from FileChannel
 * @param start Starting position for writing
 * @return ByteWriteChannel for writing to file
 */
fun FileChannel.writeChannel(start: Long = 0): ByteWriteChannel

Usage Examples:

import io.ktor.util.*
import java.nio.channels.*
import java.io.RandomAccessFile

// Work with file channels directly
val randomAccessFile = RandomAccessFile("data.bin", "rw")
val fileChannel = randomAccessFile.channel

// Read from specific range
val rangeChannel = fileChannel.readChannel(start = 100, endInclusive = 199)
val data = rangeChannel.readRemaining()

// Write to specific position
val writeChannel = fileChannel.writeChannel(start = 50)
writeChannel.writeFully(data)

// Work with socket channels
val socketChannel = SocketChannel.open()
val readChannel = socketChannel.readChannel()
val writeChannel = socketChannel.writeChannel()

ByteBuffer Pool

Memory pool for efficient ByteBuffer reuse in I/O operations.

/**
 * Object pool for ByteBuffer instances to reduce garbage collection
 */
object ByteBufferPool {
    /** Borrow a ByteBuffer from the pool */
    fun borrow(): ByteBuffer
    
    /** Return a ByteBuffer to the pool */
    fun recycle(buffer: ByteBuffer)
    
    /** Get pool capacity */
    val capacity: Int
}

/**
 * Generic object pool interface
 */
interface ObjectPool<T> {
    /** Borrow an object from the pool */
    fun borrow(): T
    
    /** Return an object to the pool */
    fun recycle(instance: T)
    
    /** Dispose of the pool */
    fun dispose()
}

Usage Examples:

import io.ktor.util.cio.*

// Use ByteBuffer pool for efficient memory management
val buffer = ByteBufferPool.borrow()
try {
    // Use buffer for I/O operations
    buffer.clear()
    // ... read/write operations
} finally {
    ByteBufferPool.recycle(buffer)
}

// Pool is used automatically by stream adapters
val inputStream = FileInputStream("large-file.dat")
val channel = inputStream.toByteReadChannel(pool = ByteBufferPool)

Channel Utilities

Additional utilities for working with byte channels.

/**
 * Copy all data from source channel to destination channel
 * @param dst Destination channel
 * @return Number of bytes copied
 */
suspend fun ByteReadChannel.copyTo(dst: ByteWriteChannel): Long

/**
 * Copy data with progress callback
 * @param dst Destination channel
 * @param progressCallback Called periodically with bytes copied
 * @return Total bytes copied
 */
suspend fun ByteReadChannel.copyTo(
    dst: ByteWriteChannel,
    progressCallback: (Long) -> Unit
): Long

/**
 * Read all remaining data as ByteArray
 * @return All remaining bytes
 */
suspend fun ByteReadChannel.readRemaining(): ByteArray

/**
 * Read all remaining data as String with specified charset
 * @param charset Character encoding (default UTF-8)
 * @return Decoded string
 */
suspend fun ByteReadChannel.readUTF8Line(): String?

Usage Examples:

import io.ktor.util.cio.*

// Copy between channels with progress
val sourceFile = File("source.dat")
val destFile = File("destination.dat")

val source = sourceFile.readChannel()
val destination = destFile.writeChannel()

val totalBytes = source.copyTo(destination) { bytesCopied ->
    println("Copied $bytesCopied bytes")
}

println("Total copied: $totalBytes bytes")

// Read content as string
val textFile = File("readme.txt")
val textChannel = textFile.readChannel()
val content = textChannel.readRemaining().decodeToString()
println(content)

Path Utilities

JVM-specific path manipulation utilities.

/**
 * Convert string to Path
 * @return NIO Path instance
 */
fun String.toPath(): java.nio.file.Path

/**
 * Get file extension from Path
 */
val java.nio.file.Path.extension: String

/**
 * Get file name without extension
 */
val java.nio.file.Path.nameWithoutExtension: String

/**
 * Check if path represents a hidden file
 */
val java.nio.file.Path.isHidden: Boolean

Usage Examples:

import io.ktor.util.*
import java.nio.file.Paths

// Path manipulation
val pathString = "/home/user/document.pdf"
val path = pathString.toPath()

println(path.extension) // "pdf"
println(path.nameWithoutExtension) // "document"
println(path.isHidden) // false

// Work with different path types
val relativePath = "configs/app.properties".toPath()
val absolutePath = "/etc/hostname".toPath()

// Use with file operations
val configChannel = relativePath.readChannel()
val systemChannel = absolutePath.readChannel()

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-utils-jvm

docs

attributes.md

collections.md

compression.md

conversion.md

crypto.md

datetime.md

index.md

io.md

logging.md

pipeline.md

strings.md

tile.json