A kotlin multiplatform library that allows you to allocate and modify byte[] natively using an API similar to Java's ByteBuffer API.
Report Bug
·
Request Feature
Table of Contents
Allocating and managing a chunk of memory can be slightly different based on each platform. This project aims to make it easier to manage buffers in a cross platform way using kotlin multiplatform. This was originally created as a side project for a kotlin multiplatform mqtt data sync solution.
Implementation notes:
JVM
+Android
delegate to direct ByteBuffers to avoid memory copies when possible.Native
platforms use standard byte arrays to manage memory.JS
targets use Uint8Array.
- None
Platform | 🛠Builds🛠 + 🔬Tests🔬 | Deployed Artifact | Non Kotlin Sample |
---|---|---|---|
JVM 1.8 |
🚀 | maven central 🔮 | 🔮 |
Node.js |
🚀 | npm 🔮 | 🔮 |
Browser (Chrome) |
🚀 | npm 🔮 | 🔮 |
Android |
🚀 | maven central 🔮 | 🔮 |
iOS |
🚀 | cocoapods 🔮 | 🔮 |
WatchOS |
🚀 | cocoapods 🔮 | 🔮 |
TvOS |
🚀 | cocoapods 🔮 | 🔮 |
MacOS |
🚀 | cocoapods 🔮 | 🔮 |
Linux X64 |
🚀 | apt/yum 🔮 | 🔮 |
Windows X64 |
🚀 | chocolatey 🔮 | 🔮 |
- TODO: Add explanation after artifacts are deployed
val buffer = PlatformBuffer.allocate(byteSize, byteOrder = ByteOrder.BIG_ENDIAN)
val byteArray = byteArrayOf(1,2,3,4,5)
val buffer = PlatformBuffer.wrap(byteArray, byteOrder = ByteOrder.BIG_ENDIAN)
Byte order defaults to big endian but can be specified when creating the buffer with ByteOrder.BIG_ENDIAN
or ByteOrder.LITTLE_ENDIAN
The byte order of a buffer can be checked with buffer.byteOrder
val buffer :WriteBuffer
// write signed byte
buffer.write(5.toByte())
// write unsigned byte
buffer.write(5.toUByte())
// write unsigned short
buffer.write(5.toUShort())
// write unsigned int
buffer.write(5.toUInt())
// write long
buffer.write(5L)
// write float
buffer.write(123.456f)
// write double
buffer.write(123.456)
// write text
buffer.write("5")
// copy buffer into this one
buffer.write(otherBuffer)
// write byte array
buffer.write(byteArrayOf(1,2,3,4))
// write partial byte array
buffer.write(byteArrayOf(1,2,3,4,5), offset, length)
val buffer :ReadBuffer
// read signed byte
buffer.readByte()
// read unsigned byte
buffer.readUnsignedByte()
// read unsigned short
buffer.readUnsignedShort()
// read unsigned int
buffer.readUnsignedInt()
// read long
buffer.readLong()
// read float
buffer.readFloat()
// read double
buffer.readDouble()
// read text
buffer.readUtf8(numOfBytesToRead)
// read byte array
buffer.readByteArray(numOfBytesToRead)
git clone [email protected]:DitchOoM/buffer.git
- Open cloned directory with Intellij IDEA.
- Be sure to open with gradle
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the Apache 2.0 License. See LICENSE
for more information.