https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

semoro

10/07/2016, 9:42 AM
Copy code
val ByteBuffer_addressProperty = DirectBuffer::class.declaredFunctions.find { it.name == "address" }!!
val ByteBuffer.address: Long
    get() = ByteBuffer_addressProperty.call(this) as Long

fun main(args: Array<String>) {
    //Must be called once at initialization
    ByteBuffer_addressProperty.isAccessible = true

    val buffer = ByteBuffer.allocateDirect(16)
    for (i in 1..16)
        buffer.put(i.toByte())
    buffer.flip()
    val address = buffer.address


    println(java.lang.Long.toHexString(address))

    //CHECK
    val theUnsafeField = Unsafe::class.java.declaredFields.find { it.name == "theUnsafe" }!!
    theUnsafeField.isAccessible = true
    val theUnsafe = theUnsafeField.get(null) as Unsafe
    for (i in 1..16)
        assert(theUnsafe.getByte(address + i) == i.toByte())


}