@ditchoom/buffer-kt
v1.0.98
Published
[![Contributors][contributors-shield]][contributors-url] [![Forks][forks-shield]][forks-url] [![Stargazers][stars-shield]][stars-url] [![Issues][issues-shield]][issues-url] [![MIT License][license-shield]][license-url] [![LinkedIn][linkedin-shield]][linke
Downloads
6
Readme
About The Project
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.
Runtime Dependencies
- None
Supported Platforms
| Platform | 🛠Builds🛠 + 🔬Tests🔬 | Deployed Artifact | Non Kotlin Sample |
| :---: | :---: |:----------------------------------:|:-----------------:|
| JVM
1.8 |🚀| maven central 🔮 | WIP |
| Node.js
|🚀| npm 🔮 | WIP |
| Browser
(Chrome) |🚀| npm 🔮 | WIP |
| Android
|🚀| maven central 🔮 | WIP |
| iOS
|🚀| WIP cocoapods 🔮 | WIP |
| WatchOS
|🚀| WIP cocoapods 🔮 | WIP |
| TvOS
|🚀| WIP cocoapods 🔮 | WIP |
| MacOS
|🚀| WIP cocoapods 🔮 | WIP |
| Linux X64
|🚀| WIP apt/yum 🔮 | WIP |
| Windows X64
|🚀| WIP chocolatey 🔮 | WIP |
Installation
Gradle
NPM
Usage
Allocate a new platform agnostic buffer
val buffer = PlatformBuffer.allocate(byteSize, zone = AllocationZone.Direct, byteOrder = ByteOrder.BIG_ENDIAN)
Wrap an existing byte array into a platform agnostic buffer
val byteArray = byteArrayOf(1, 2, 3, 4, 5)
val buffer = PlatformBuffer.wrap(byteArray, byteOrder = ByteOrder.BIG_ENDIAN)
Allocation Zones
Allocation zones allow you to change where the buffer is allocated.
AllocationZone.Custom
-> Allows you to override the underlying buffer. This can be helpful for memory mapped structures.AllocationZone.Heap
-> On JVM platforms, allocates a HeapByteBuffer, otherwise a native byte arrayAllocationZone.Direct
-> On JVM platforms, allocates a DirectByteBuffer, otherwise a native byte arrayAllocationZone.AndroidSharedMemory
-> On API 27+ it allocates a Shared Memory instance, otherwise defaulting toAllocationZone.Direct
.
Android: All
JvmBuffer
s areParcelable
. To avoid extra memory copies, useAllocationZone.AndroidSharedMemory
Byte order
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
Write data into platform agnostic buffer
val buffer: WriteBuffer
// write signed byte
buffer.write(5.toByte())
// write unsigned byte
buffer.write(5.toUByte())
// write short
buffer.write(5.toShort())
// write unsigned short
buffer.write(5.toUShort())
// write int
buffer.write(5)
// write unsigned int
buffer.write(5.toUInt())
// write long
buffer.write(5L)
// write unsigned long
buffer.write(5uL)
// 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)
Read data into platform agnostic buffer
val buffer: ReadBuffer
// read signed byte
val b :Byte = buffer.readByte()
// read unsigned byte
val uByte :UByte = buffer.readUnsignedByte()
// read short
val short :Short = buffer.readShort()
// read unsigned short
val uShort :UShort = buffer.readUnsignedShort()
// read int
val intValue = buffer.readInt()
// read unsigned int
val uIntValue :Int = buffer.readUnsignedInt()
// read long
val longValue :Long = buffer.readLong()
// read unsigned long
val uLongValue :ULong = buffer.readUnsignedLong()
// read float
val float :Float = buffer.readFloat()
// read double
val double: :Double = buffer.readDouble()
// read text
val string :String = buffer.readUtf8(numOfBytesToRead)
// read byte array
val byteArray :ByteArray = buffer.readByteArray(numOfBytesToRead)
Building Locally
git clone [email protected]:DitchOoM/buffer.git
- Open cloned directory with Intellij IDEA.
- Be sure to open with gradle
Roadmap
See the open issues for a list of proposed features (and known issues).
Contributing
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
License
Distributed under the Apache 2.0 License. See LICENSE
for more information.