[![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
npm install @ditchoom/buffer-kt[![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]][linkedin-url]
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
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][byte-buffer-api] 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][maven-central] ๐ฎ | WIP |
| Node.js |๐| [npm][npm] ๐ฎ | WIP |
| Browser (Chrome) |๐| [npm][npm] ๐ฎ | WIP |
| Android |๐| [maven central][maven-central] ๐ฎ | WIP |
| iOS |๐| WIP [cocoapods][cocoapods] ๐ฎ | WIP |
| WatchOS |๐| WIP [cocoapods][cocoapods] ๐ฎ | WIP |
| TvOS |๐| WIP [cocoapods][cocoapods] ๐ฎ | WIP |
| MacOS |๐| WIP [cocoapods][cocoapods] ๐ฎ | WIP |
| Linux X64 |๐| WIP [apt][apt]/[yum][yum] ๐ฎ | WIP |
| Windows X64 |๐| WIP [chocolatey][chocolately] ๐ฎ | WIP |
implementation("com.ditchoom:buffer:$version") to your build.gradle dependenciesnpm i @ditchoom/buffer-kt``kotlin`
val buffer = PlatformBuffer.allocate(byteSize, zone = AllocationZone.Direct, byteOrder = ByteOrder.BIG_ENDIAN)
`kotlin`
val byteArray = byteArrayOf(1, 2, 3, 4, 5)
val buffer = PlatformBuffer.wrap(byteArray, byteOrder = ByteOrder.BIG_ENDIAN)
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 mappedAllocationZone.Heap
structures.
- -> 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 allocatesAllocationZone.Direct
a Shared Memory instance, otherwise defaulting
to .
> Android: All JvmBuffers are Parcelable. To avoid extra memory copies, use AllocationZone.AndroidSharedMemory
Byte order defaults to big endian but can be specified when creating the buffer with ByteOrder.BIG_ENDIANByteOrder.LITTLE_ENDIAN
or
The byte order of a buffer can be checked with buffer.byteOrder
`kotlin`
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)
`kotlin`
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)
- git clone git@github.com: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.
1. Fork the Project
2. Create your Feature Branch (git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature'
3. Commit your Changes ()git push origin feature/AmazingFeature
4. Push to the Branch ()
5. Open a Pull Request
Distributed under the Apache 2.0 License. See LICENSE` for more information.
[contributors-shield]: https://img.shields.io/github/contributors/DitchOoM/buffer.svg?style=for-the-badge
[contributors-url]: https://github.com/DitchOoM/buffer/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/DitchOoM/buffer.svg?style=for-the-badge
[forks-url]: https://github.com/DitchOoM/buffer/network/members
[stars-shield]: https://img.shields.io/github/stars/DitchOoM/buffer.svg?style=for-the-badge
[stars-url]: https://github.com/DitchOoM/buffer/stargazers
[issues-shield]: https://img.shields.io/github/issues/DitchOoM/buffer.svg?style=for-the-badge
[issues-url]: https://github.com/DitchOoM/buffer/issues
[license-shield]: https://img.shields.io/github/license/DitchOoM/buffer.svg?style=for-the-badge
[license-url]: https://github.com/DitchOoM/buffer/blob/master/LICENSE.md
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
[linkedin-url]: https://www.linkedin.com/in/thebehera
[byte-buffer-api]: https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html
[maven-central]: https://search.maven.org/search?q=com.ditchoom
[npm]: https://www.npmjs.com/search?q=ditchoom-buffer
[cocoapods]: https://cocoapods.org/pods/DitchOoM-buffer
[apt]: https://packages.ubuntu.com/search?keywords=ditchoom&searchon=names&suite=groovy§ion=all
[yum]: https://pkgs.org/search/?q=DitchOoM-buffer
[chocolately]: https://chocolatey.org/packages?q=DitchOoM-buffer