decode

inline fun <T : Message> ByteArray.decode(): T

Decodes ByteArray receiver to a Message.

To use CoAP UDP (RFC 7252) decoding:

import com.juul.koap.Message.Udp

val data = byteArrayOf(...)
val message = data.decode<Udp>()

To use CoAP TCP (RFC 8323) decoding:

import com.juul.koap.Message.Tcp

val data = byteArrayOf(...)
val message = data.decode<Tcp>()

See also


fun ByteArray.decode(header: Header.Udp, offset: Int = header.size): Message.Udp

Assembles a Message.Udp by decoding CoAP message content (Options + Payload) from the ByteArray receiver and combining with provided header.

Example usage:

val header = encoded.decodeUdpHeader()
val message = encoded.decode(header)

Decoding of ByteArray will begin at offset (default is Header.size, which expects the presence of header data in the ByteArray receiver). If ByteArray does not contain header (only contains Options + Payload) then specify an offset of 0, for example:

val header = encoded.decodeUdpHeader()
val content = encoded.copyRange(header.size, encoded.size)
val message = content.decode(header, offset = 0)

fun ByteArray.decode(header: Header.Tcp, offset: Int = header.size): Message.Tcp

Assembles a Message.Tcp by decoding CoAP message content (Options + Payload) from the ByteArray receiver and combining with provided header.

Example usage:

val header = encoded.decodeTcpHeader()
val message = encoded.decode(header)

Decoding of ByteArray will begin at offset (default is Header.size, which expects the presence of header data in the ByteArray receiver). If ByteArray does not contain header (only contains Options + Payload) then specify an offset of 0, for example:

val header = encoded.decodeTcpHeader()
val content = encoded.copyRange(header.size, encoded.size)
val message = content.decode(header, offset = 0)