A modern I/O library that complements java.io and java.nio to make it much easier to access, store, and process your data.
npx @tessl/cli install tessl/maven-com-squareup-okio--okio-jvm@3.12.0Okio is a modern I/O library that complements java.io and java.nio to make it much easier to access, store, and process your data. Originally developed as a component of OkHttp, Okio provides efficient implementations of core I/O primitives including ByteString (immutable byte sequences), Buffer (mutable byte queues), Source (input streams), and Sink (output streams) that work consistently across JVM, Android, JavaScript, and native platforms.
implementation 'com.squareup.okio:okio-jvm:3.12.0'Kotlin:
import okio.*Or import specific classes:
import okio.Buffer
import okio.ByteString
import okio.FileSystem
import okio.Path
import okio.Source
import okio.SinkJava:
import okio.Buffer;
import okio.ByteString;
import okio.FileSystem;
import okio.Okio;
import okio.Source;
import okio.Sink;import okio.*
// Working with ByteString (immutable bytes)
val bytes = "Hello, Okio!".encodeUtf8()
println(bytes.hex()) // Hexadecimal representation
println(bytes.base64()) // Base64 encoding
// Working with Buffer (mutable bytes)
val buffer = Buffer()
buffer.writeUtf8("Hello")
buffer.writeUtf8(", ")
buffer.writeUtf8("World!")
println(buffer.readUtf8()) // "Hello, World!"
// Reading and writing files
val path = "/tmp/example.txt".toPath()
FileSystem.SYSTEM.write(path) {
writeUtf8("File content")
}
val content = FileSystem.SYSTEM.read(path) {
readUtf8()
}
// Streaming I/O
val source: Source = path.source()
val bufferedSource = source.buffer()
val line = bufferedSource.readUtf8Line()
bufferedSource.close()Okio is built around several key components:
The library emphasizes memory efficiency through features like segment pooling for buffers, copy-on-write semantics for ByteString, and built-in support for various data formats.
Immutable byte sequences with encoding, hashing, and manipulation utilities. Ideal for working with binary data, API keys, and data that needs to be shared across threads.
abstract class ByteString : Comparable<ByteString> {
abstract val size: Int
abstract fun utf8(): String
abstract fun base64(): String
abstract fun hex(): String
abstract fun sha256(): ByteString
abstract fun toByteArray(): ByteArray
}
companion object {
val EMPTY: ByteString
fun of(vararg data: Byte): ByteString
}
fun String.encodeUtf8(): ByteString
fun ByteArray.toByteString(offset: Int = 0, byteCount: Int = size): ByteStringMutable byte collections with efficient operations for building and manipulating byte sequences. Perfect for parsing protocols, building requests, and streaming data processing.
class Buffer : BufferedSource, BufferedSink {
val size: Long
fun clear()
fun copy(): Buffer
fun snapshot(): ByteString
fun get(pos: Long): Byte
}Streaming I/O abstractions for reading and writing bytes with timeout support and efficient buffering.
interface Source : Closeable {
fun read(sink: Buffer, byteCount: Long): Long
fun timeout(): Timeout
}
interface Sink : Closeable {
fun write(source: Buffer, byteCount: Long)
fun flush()
fun timeout(): Timeout
}
interface BufferedSource : Source {
val buffer: Buffer
fun readUtf8(): String
fun readByte(): Byte
fun readInt(): Int
fun readLong(): Long
}
interface BufferedSink : Sink {
val buffer: Buffer
fun writeUtf8(string: String): BufferedSink
fun writeByte(b: Int): BufferedSink
fun writeInt(i: Int): BufferedSink
fun writeLong(v: Long): BufferedSink
}Cross-platform file system access with path manipulation, metadata queries, and efficient file I/O operations.
abstract class FileSystem : Closeable {
abstract fun canonicalize(path: Path): Path
abstract fun list(dir: Path): List<Path>
abstract fun source(file: Path): Source
abstract fun sink(file: Path, mustCreate: Boolean = false): Sink
abstract fun createDirectory(dir: Path, mustCreate: Boolean = false)
abstract fun delete(path: Path, mustExist: Boolean = true)
companion object {
val SYSTEM: FileSystem
val SYSTEM_TEMPORARY_DIRECTORY: Path
}
}
class Path : Comparable<Path> {
val name: String
val parent: Path?
val isAbsolute: Boolean
operator fun div(child: String): Path
}
fun String.toPath(normalize: Boolean = false): PathBuilt-in hashing and cryptographic operations for data integrity and security.
// ByteString hashing methods
fun ByteString.sha1(): ByteString
fun ByteString.sha256(): ByteString
fun ByteString.sha512(): ByteString
fun ByteString.hmacSha256(key: ByteString): ByteString
// Streaming hash computation
class HashingSink(sink: Sink, algorithm: String) : Sink {
val hash: ByteString
}
class HashingSource(source: Source, algorithm: String) : Source {
val hash: ByteString
}Hash and Cryptographic Operations
JVM-specific extensions for interoperability with Java I/O, NIO, and security APIs.
// Java I/O extensions
fun OutputStream.sink(): Sink
fun InputStream.source(): Source
fun File.source(): Source
fun File.sink(append: Boolean = false): Sink
// Security extensions
fun Sink.cipherSink(cipher: Cipher): CipherSink
fun Source.cipherSource(cipher: Cipher): CipherSource
// Async operations
class AsyncTimeout : Timeout {
fun enter()
fun exit(): Boolean
fun <T> withTimeout(block: () -> T): T
}interface Closeable {
fun close()
}
abstract class Timeout {
open fun timeoutNanos(): Long
open fun hasDeadline(): Boolean
open fun deadlineNanoTime(): Long
companion object {
val NONE: Timeout
}
}
data class FileMetadata(
val isRegularFile: Boolean,
val isDirectory: Boolean,
val symlinkTarget: Path?,
val size: Long?,
val createdAtMillis: Long?,
val lastModifiedAtMillis: Long?,
val lastAccessedAtMillis: Long?
)class EOFException(message: String? = null) : IOException(message)