Skip to content
forked from DitchOoM/buffer

Kotlin Multiplatform bytebuffer/byte[] wrapper

License

Notifications You must be signed in to change notification settings

litclimbing/buffer

 
 

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


ByteBuffer

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
  1. About The Project
  2. Installation
  3. Usage
  4. Building Locally
  5. Getting Started
  6. Roadmap
  7. Contributing
  8. License

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
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 🔮 🔮

Installation

  • TODO: Add explanation after artifacts are deployed

Usage

Allocate a new platform agnostic buffer

val buffer = PlatformBuffer.allocate(byteSize, 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)

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 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)

Read data into platform agnostic buffer

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)

Building Locally

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

About

Kotlin Multiplatform bytebuffer/byte[] wrapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%