getUuid

JVM
2.0
@ExperimentalUuidApi fun ByteBuffer.getUuid(): Uuid
(source)

Reads a Uuid value at this buffer's current position.

This function reads the next 16 bytes at this buffer's current position and assembles a uuid from them. As a result, the buffer's position is incremented by 16.

Note that this function ignores the buffer's byte order. The 16 bytes are read sequentially, with each byte representing the next 8 bits of the uuid, starting from the first byte representing the most significant 8 bits to the last byte representing the least significant 8 bits.

The returned uuid is equivalent to:

val bytes = ByteArray(16)
byteBuffer.get(bytes)
return Uuid.fromByteArray(bytes)
import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val uuidBytes = byteArrayOf(
    0x55, 0x0e, 0x84.toByte(), 0x00, 0xe2.toByte(), 0x9b.toByte(), 0x41, 0xd4.toByte(),
    0xa7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
)
val buffer = java.nio.ByteBuffer.wrap(uuidBytes)
val uuid = buffer.getUuid()

// The uuid has exactly the same 16 bytes
println(uuid.toByteArray().contentEquals(uuidBytes)) // true
println(uuid) // 550e8400-e29b-41d4-a716-446655440000
//sampleEnd
}

Exceptions

BufferUnderflowException - If there are fewer than 16 bytes remaining in this buffer.

Return The uuid value read at this buffer's current position.

See Also

Uuid.toByteArray

JVM
2.0
@ExperimentalUuidApi fun ByteBuffer.getUuid(index: Int): Uuid
(source)

Reads a Uuid value at the specified index.

This function reads the next 16 bytes from this buffer at the specified index and assembles a uuid from them. The buffer's position, however, is not updated.

Note that this function ignores the buffer's byte order. The 16 bytes are read sequentially, with each byte representing the next 8 bits of the uuid, starting from the first byte representing the most significant 8 bits to the last byte representing the least significant 8 bits.

The returned uuid is equivalent to:

val bytes = ByteArray(16) { i ->
    byteBuffer.get(index + i)
}
return Uuid.fromByteArray(bytes)

except that this function first checks that there are sufficient bytes in the buffer.

import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val bytes = byteArrayOf(
    0x00, 0x00, 0x55, 0x0e, 0x84.toByte(), 0x00, 0xe2.toByte(), 0x9b.toByte(),
    0x41, 0xd4.toByte(), 0xa7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
)
val buffer = java.nio.ByteBuffer.wrap(bytes)
val uuid = buffer.getUuid(index = 2)

// The uuid has exactly the same 16 bytes
val uuidBytes = bytes.sliceArray(2..<18)
println(uuid.toByteArray().contentEquals(uuidBytes)) // true
println(uuid) // 550e8400-e29b-41d4-a716-446655440000
//sampleEnd
}

Parameters

index - The index to read a uuid at.

Exceptions

IndexOutOfBoundsException - If index is negative or index + 15 is not smaller than this buffer's limit.

Return The uuid value read at the specified index.

See Also

Uuid.fromByteArray