1.0.98 โ€ข Published 1 year ago

@ditchoom/buffer-kt v1.0.98

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Contributors Forks Stargazers Issues MIT License LinkedIn

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 ArtifactNon 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 array
  • AllocationZone.Direct -> On JVM platforms, allocates a DirectByteBuffer, otherwise a native byte array
  • AllocationZone.AndroidSharedMemory -> On API 27+ it allocates a Shared Memory instance, otherwise defaulting to AllocationZone.Direct.

Android: All JvmBuffers are Parcelable. To avoid extra memory copies, use AllocationZone.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

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.

1.0.98

1 year ago

1.0.96

1 year ago

1.0.95

1 year ago

1.0.94

1 year ago

1.0.87

1 year ago

1.0.80

2 years ago

1.0.84

2 years ago

1.0.73

2 years ago

1.0.83

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.0.77

2 years ago

1.0.76

2 years ago

1.0.86

2 years ago

1.0.75

2 years ago

1.0.85

2 years ago

1.0.74

2 years ago

1.0.79

2 years ago

1.0.78

2 years ago

1.0.72

2 years ago

0.0.5

2 years ago