Skip to content

Kotlin Integration#

Requirements#

  • JDK 17 or later
  • Kotlin 2.0 or later

Installation#

<dependency>
  <groupId>org.odenix.mxpack</groupId>
  <artifactId>mxpack-kotlin</artifactId>
  <version>${mxpackVersion}</version>
</dependency>
dependencies {
  implementation("org.odenix.mxpack:mxpack-kotlin:${mxpackVersion}")
}
dependencies {
  implementation "org.odenix.mxpack:mxpack-kotlin:${mxpackVersion}"
}

Features#

Factory methods#

The Kotlin API's factory methods mirror the Java API's factory methods but use named arguments with default values instead of option builders.

For example, BufferAllocators.ofPooled() (note the plural form) mirrors BufferAllocator.ofPooled().

var allocator = BufferAllocator.ofPooled(options -> options
    .maxByteBufferCapacity(1024 * 1024)
    .maxByteBufferPoolCapacity(1024 * 1024 * 32)
);
val allocator = BufferAllocators.ofPooled(
  maxByteBufferCapacity = 1024 * 1024,
  maxByteBufferPoolCapacity = 1024 * 1024 * 32
)

Extension methods#

Read unsigned integers#

fun read(reader: MessageReader) {
  val num1: UByte = reader.readUByteKotlin()
  val num2: UShort = reader.readUShortKotlin()
  var num3: UInt = reader.readUIntKotlin()
  var num4: ULong = reader.readULongKotlin()
}

Write unsigned integers#

fun write(writer: MessageWriter) {
  writer.write(UByte.MAX_VALUE)
  writer.write(UShort.MAX_VALUE)
  writer.write(UInt.MAX_VALUE)
  writer.write(ULong.MAX_VALUE)
}