Skip to main content

Buffer

Kotlin Multiplatform library for platform-agnostic byte buffer management

Buffer provides a unified API for managing byte buffers across all Kotlin platforms, delegating to native implementations to avoid memory copies.

Why Buffer?

  • Zero-copy performance: Direct delegation to platform-native buffers (ByteBuffer, NSData, Uint8Array)
  • Kotlin Multiplatform: Single API works across JVM, Android, iOS, JS, WASM, and Native
  • Buffer Pooling: High-performance buffer reuse for network I/O and protocol parsing
  • Stream Processing: Handle fragmented data across chunk boundaries
  • Optimized Operations: Bulk comparison, search (indexOf), and fill operations using SIMD-like techniques

Platform Implementations

PlatformNative TypeNotes
JVMjava.nio.ByteBufferUse Direct buffers for zero-copy I/O
AndroidByteBuffer + SharedMemoryIPC via Parcelable
iOS/macOSNSData / NSMutableDataFoundation integration
JavaScriptUint8ArraySharedArrayBuffer support
WASMLinearBuffer (native memory) / ByteArrayBufferZero-copy JS interop
LinuxByteArrayNative target

Quick Example

import com.ditchoom.buffer.PlatformBuffer
import com.ditchoom.buffer.AllocationZone
import com.ditchoom.buffer.ByteOrder

// Allocate a buffer
val buffer = PlatformBuffer.allocate(
size = 1024,
zone = AllocationZone.Direct,
byteOrder = ByteOrder.BIG_ENDIAN
)

// Write data
buffer.writeInt(42)
buffer.writeString("Hello, Buffer!")

// Prepare for reading
buffer.resetForRead()

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

Installation

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

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

Next Steps