putUuid

JVM
2.0
@ExperimentalUuidApi fun ByteBuffer.putUuid(
    uuid: Uuid
): ByteBuffer

(source)

Writes the specified uuid value at this buffer's current position.

This function writes 16 bytes containing the given uuid value into this buffer at the current position. 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 written 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.

This function is equivalent to:

byteBuffer.put(uuid.toByteArray())
import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val uuid = Uuid.parse("550e8400-e29b-41d4-a716-446655440000")
val bytes = ByteArray(16)
val buffer = java.nio.ByteBuffer.wrap(bytes)
buffer.putUuid(uuid)

// The written 16 bytes are exactly equal to the uuid bytes
println(bytes.contentEquals(uuid.toByteArray())) // true
//sampleEnd
}

Parameters

uuid - The uuid value to write.

Exceptions

BufferOverflowException - If there is insufficient space in this buffer for 16 bytes.

ReadOnlyBufferException - If this buffer is read-only.

Return This byte buffer.

See Also

Uuid.toByteArray

JVM
2.0
@ExperimentalUuidApi fun ByteBuffer.putUuid(
    index: Int,
    uuid: Uuid
): ByteBuffer

(source)

Writes the specified uuid value at the specified index.

This function writes 16 bytes containing the given uuid value into this buffer at the specified index. The buffer's position, however, is not updated.

Note that this function ignores the buffer's byte order. The 16 bytes are written 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.

This function is equivalent to:

val bytes = uuid.toByteArray()
bytes.forEachIndexed { i, byte ->
    byteBuffer.put(index + i, byte)
}

except that this function first checks that there is sufficient space in the buffer.

import kotlin.uuid.*

fun main(args: Array<String>) {
//sampleStart
val uuid = Uuid.parse("550e8400-e29b-41d4-a716-446655440000")
val bytes = ByteArray(20)
val buffer = java.nio.ByteBuffer.wrap(bytes)
buffer.putUuid(index = 2, uuid)

// The written 16 bytes are exactly equal to the uuid bytes
val writtenBytes = bytes.sliceArray(2..<18)
println(writtenBytes.contentEquals(uuid.toByteArray())) // true
//sampleEnd
}

Parameters

index - The index to write the specified uuid value at.

uuid - The uuid value to write.

Exceptions

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

ReadOnlyBufferException - If this buffer is read-only.

Return This byte buffer.

See Also

Uuid.toByteArray