com.soywiz.kmem.ByteArraySlice.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kmem-common Show documentation
Show all versions of kmem-common Show documentation
kmem: Fast memory for Kotlin Common (JVM and JS) using Buffers and Typed Arrays
The newest version!
package com.soywiz.kmem
class ByteArraySlice(val data: ByteArray, val position: Int, val length: Int) {
fun getPointer(): Pointer = Pointer(data, position)
override fun toString() = "ByteArraySlice(data=$data, position=$position, length=$length)"
operator fun get(n: Int): Byte = data[position + n]
operator fun set(n: Int, value: Byte): Unit = run { data[position + n] = value }
companion object {
fun create(start: Pointer, end: Pointer): ByteArraySlice {
if (start.ba != end.ba) throw RuntimeException("Pointer must reference the samea array")
return ByteArraySlice(start.ba, start.offset, end.offset - start.offset)
}
}
}
fun ByteArray.toByteArraySlice() = ByteArraySlice(this, 0, size)