Skip to main content

Buffer

Kotlin Multiplatform byte buffers — same code on JVM, Android, iOS, macOS, Linux, JS, and WASM with zero-copy performance.

Buffer gives you one ReadBuffer/WriteBuffer API that delegates to platform-native types — ByteBuffer on JVM, NSData on Apple, malloc on Linux, Uint8Array on JS — so data never copies between your code and the OS.

Why Buffer?

  • Zero-copy I/O: Delegates directly to platform-native buffers — no copying between your code and the OS
  • Buffer Pooling: Allocate once, reuse for every network request — no GC pauses, predictable latency
  • Stream Processing: Parse protocols that span chunk boundaries without manual accumulator code
  • Compression: compress()/decompress() on any ReadBuffer across all platforms
  • Flow Extensions: mapBuffer(), asStringFlow(), lines() — compose streaming transforms with Kotlin Flow
  • SIMD-Accelerated Operations: Bulk comparison, search (indexOf), fill, and XOR masking
  • 7 Platforms: Same API on JVM, Android, iOS, macOS, Linux, JS, WASM

Quick Example

// Works identically on all platforms
val buffer = PlatformBuffer.allocate(1024)
buffer.writeInt(42)
buffer.writeString("Hello, Buffer!")
buffer.resetForRead()

val number = buffer.readInt() // 42
val text = buffer.readString(14) // "Hello, Buffer!"

High-Value Patterns

Buffer pooling — no allocation per request:

withPool(defaultBufferSize = 8192) { pool ->
pool.withBuffer(1024) { buffer ->
buffer.writeInt(requestId)
buffer.writeString(payload)
buffer.resetForRead()
sendToNetwork(buffer)
} // buffer returned to pool
}

Streaming pipeline — decompress, decode, split lines:

bufferFlow
.mapBuffer { decompress(it, Gzip).getOrThrow() }
.asStringFlow()
.lines()
.collect { line -> process(line) }

Platform Implementations

PlatformNative TypeNotes
JVMjava.nio.ByteBufferDirect buffers for zero-copy I/O
AndroidByteBuffer + SharedMemoryIPC via Parcelable
iOS/macOSNSData / NSMutableDataFoundation integration
JavaScriptUint8ArraySharedArrayBuffer support
WASMLinearBuffer / ByteArrayBufferZero-copy JS interop
LinuxNativeBuffer (malloc) / ByteArrayBufferZero-copy io_uring I/O

Part of the DitchOoM Stack

Buffer is the foundation for the Socket library — cross-platform TCP + TLS that streams these same ReadBuffer/WriteBuffer types.

┌─────────────────────────────┐
│ Your Protocol │
├─────────────────────────────┤
│ socket (TCP + TLS) │ ← com.ditchoom:socket
├─────────────────────────────┤
│ buffer-compression │ ← com.ditchoom:buffer-compression
├─────────────────────────────┤
│ buffer-flow │ ← com.ditchoom:buffer-flow
├─────────────────────────────┤
│ buffer │ ← com.ditchoom:buffer
└─────────────────────────────┘

Installation

Add to your build.gradle.kts (see Maven Central for the latest version):

dependencies {
// Core buffer library
implementation("com.ditchoom:buffer:<latest-version>")

// Optional: Compression (gzip, deflate)
implementation("com.ditchoom:buffer-compression:<latest-version>")

// Optional: Flow extensions (lines, mapBuffer, asStringFlow)
implementation("com.ditchoom:buffer-flow:<latest-version>")
}

Next Steps