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
| Platform | Native Type | Notes |
|---|---|---|
| JVM | java.nio.ByteBuffer | Use Direct buffers for zero-copy I/O |
| Android | ByteBuffer + SharedMemory | IPC via Parcelable |
| iOS/macOS | NSData / NSMutableData | Foundation integration |
| JavaScript | Uint8Array | SharedArrayBuffer support |
| WASM | LinearBuffer (native memory) / ByteArrayBuffer | Zero-copy JS interop |
| Linux | ByteArray | Native 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
- Getting Started - Installation and basic usage
- Core Concepts - Understanding buffers, positions, and limits
- Recipes - Common patterns and examples
- Performance - Optimization tips