
Cross-platform ByteBuffer implementation offers Java NIO API compatibility, memory efficiency, endianness control, and buffer operations. Supports primitive types and runs on multiple platforms.
A Kotlin Multiplatform library providing a cross-platform ByteBuffer implementation compatible with Java NIO semantics, supporting Android, iOS, JVM.
ByteBuffer interfaceAdd to your build.gradle.kts:
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.hehua2008:kmpbuffer:0.9.0")
}
}
}
}// Allocate a direct buffer with 1024 bytes capacity
val buffer = allocateDirectByteBuffer(1024)
buffer.putInt(42)
buffer.putDouble(3.14159)
val strBytes = "Hello".encodeToByteArray()
buffer.put(strBytes, 0, strBytes.size)
// Flip for reading
buffer.flip()
val intValue = buffer.getInt()
val doubleValue = buffer.getDouble()
val byteArray = ByteArray(buffer.remaining()) { buffer.get() }
val str = byteArray.decodeToString()
// Release native direct memory
buffer.release()// Heap-backed buffer
val heapBuffer = allocateHeapByteBuffer(512)
// Direct memory buffer (platform-specific implementation)
val directBuffer = allocateDirectByteBuffer(1024)
// Wrap existing byte array
val wrappedBuffer = byteArrayOf(1,2,3,4).wrapInHeapByteBuffer()| Platform | Characteristics |
|---|---|
| JVM | Uses java.nio.ByteBuffer internally (NOTE: Java version <= 18) |
| Android | Shares JVM implementation with heap and direct buffer support |
| iOS | Native implementation using ByteArray and CArrayPointer<ByteVar>
|
./gradlew assembleContributions welcome!
Apache License 2.0 - See LICENSE for details
A Kotlin Multiplatform library providing a cross-platform ByteBuffer implementation compatible with Java NIO semantics, supporting Android, iOS, JVM.
ByteBuffer interfaceAdd to your build.gradle.kts:
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.hehua2008:kmpbuffer:0.9.0")
}
}
}
}// Allocate a direct buffer with 1024 bytes capacity
val buffer = allocateDirectByteBuffer(1024)
buffer.putInt(42)
buffer.putDouble(3.14159)
val strBytes = "Hello".encodeToByteArray()
buffer.put(strBytes, 0, strBytes.size)
// Flip for reading
buffer.flip()
val intValue = buffer.getInt()
val doubleValue = buffer.getDouble()
val byteArray = ByteArray(buffer.remaining()) { buffer.get() }
val str = byteArray.decodeToString()
// Release native direct memory
buffer.release()// Heap-backed buffer
val heapBuffer = allocateHeapByteBuffer(512)
// Direct memory buffer (platform-specific implementation)
val directBuffer = allocateDirectByteBuffer(1024)
// Wrap existing byte array
val wrappedBuffer = byteArrayOf(1,2,3,4).wrapInHeapByteBuffer()| Platform | Characteristics |
|---|---|
| JVM | Uses java.nio.ByteBuffer internally (NOTE: Java version <= 18) |
| Android | Shares JVM implementation with heap and direct buffer support |
| iOS | Native implementation using ByteArray and CArrayPointer<ByteVar>
|
./gradlew assembleContributions welcome!
Apache License 2.0 - See LICENSE for details