or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

buffer.mdbytestring.mdfilesystem.mdhashing.mdindex.mdjvm-extensions.mdsources-sinks.md
tile.json

tessl/maven-com-squareup-okio--okio-jvm

A modern I/O library that complements java.io and java.nio to make it much easier to access, store, and process your data.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.squareup.okio/okio-jvm@3.12.x

To install, run

npx @tessl/cli install tessl/maven-com-squareup-okio--okio-jvm@3.12.0

index.mddocs/

Okio

Okio 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.

Package Information

  • Package Name: okio-jvm
  • Package Type: maven
  • Language: Kotlin (with Java interop)
  • Installation: implementation 'com.squareup.okio:okio-jvm:3.12.0'

Core Imports

Kotlin:

import okio.*

Or import specific classes:

import okio.Buffer
import okio.ByteString
import okio.FileSystem
import okio.Path
import okio.Source
import okio.Sink

Java:

import okio.Buffer;
import okio.ByteString;
import okio.FileSystem;
import okio.Okio;
import okio.Source;
import okio.Sink;

Basic Usage

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()

Architecture

Okio is built around several key components:

  • ByteString: Immutable byte sequences with encoding utilities (UTF-8, Base64, Hex)
  • Buffer: Mutable byte collections with efficient segment-based storage
  • Source/Sink: Streaming I/O abstractions similar to InputStream/OutputStream but simpler
  • BufferedSource/BufferedSink: Buffered variants with convenient methods for reading/writing data types
  • FileSystem: Cross-platform file system access with Path abstraction
  • Timeout: Timeout policies for I/O operations

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.

Capabilities

ByteString Operations

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): ByteString

ByteString Operations

Buffer Operations

Mutable 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
}

Buffer Operations

Source and Sink Streaming

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
}

Source and Sink Streaming

File System Operations

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): Path

File System Operations

Hash and Cryptographic Operations

Built-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

Platform-Specific Extensions

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
}

JVM Extensions

Types

Core Types

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?
)

Exception Types

class EOFException(message: String? = null) : IOException(message)