sok / Sok.Buffer / MultiplatformBuffer
abstract class MultiplatformBuffer
(source)
Platform and version requirements: Common
A MultiplatformBuffer
is the primitive type of Sok, you will use it to receive, send and manipulate data
MultiplatformBuffer
is a class each platform must implement to abstract native buffer types (ByteBuffer on JVM, Buffer on Node.js
and ByteArray on Native). In order to have the most consistent behaviour and avoid platform-specific exception leak the below class
implement all the behavioural code (cursor management, exception throwing) and adds abstract protected methods for platform specific code.
THIS CLASS IS NOT THREAD SAFE but you should obviously not be working with the same buffer on two different threads at the same time
<init> | MultiplatformBuffer(capacity: Int ) create an empty buffer with a cursor set to 0 and a limit set to the capacity |
capacity | val capacity: Int capacity of the buffer |
cursor | var cursor: Int cursor keeping track of the position inside the buffer. The property must respect the property 0 <= cursor <= limit <= capacity |
destroyed | var destroyed: Boolean is the buffer still usable, useful on Native platforms to know when the memory is freed and avoid any use-after-free operation. |
limit | var limit: Int limit set for the buffer. The property must respect the property 0 <= cursor <= limit <= capacity |
clone | abstract fun clone(): MultiplatformBuffer Deep copy the buffer. All the data from the start to the capacity will be copied, the cursor and limit will be reset |
destroy | abstract fun destroy(): Unit Destroy the ByteBuffer, you cannot call any method on the buffer after calling destroy. This method MUST be called on native platforms in order to free/unpin memory but you can skip it on any other platform |
get | operator fun get(index: Int ): Byte Get a byte with its index in the buffer, the cursor will not be modified |
getByte | fun getByte(index: Int ? = null): Byte Get the byte at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getByteImpl | abstract fun getByteImpl(index: Int ? = null): Byte Get the byte at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getBytes | fun getBytes(length: Int , index: Int ? = null): ByteArray Get an array of bytes of a given length starting at the current buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified fun getBytes(array: ByteArray , index: Int ? = null, destinationOffset: Int = 0, length: Int = array.size-destinationOffset): Unit Copy bytes into the array starting from the current cursor position or given index. You can start the copy with an offset in the destination array and specify the number of byte you want to be copied. |
getBytesImpl | abstract fun getBytesImpl(length: Int , index: Int ? = null): ByteArray Get an array of bytes of a given length starting at the current buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified abstract fun getBytesImpl(array: ByteArray , index: Int ? = null, destinationOffset: Int , length: Int = array.size): Unit Copy bytes into the array starting from the current cursor position or given index. You can start the copy with an offset in the destination array and specify the number of byte you want to be copied. |
getInt | fun getInt(index: Int ? = null): Int Get the integer at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getIntImpl | abstract fun getIntImpl(index: Int ? = null): Int Get the integer at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getLong | fun getLong(index: Int ? = null): Long Get the long at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getLongImpl | abstract fun getLongImpl(index: Int ? = null): Long Get the long at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getShort | fun getShort(index: Int ? = null): Short Get the short at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getShortImpl | abstract fun getShortImpl(index: Int ? = null): Short Get the short at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUByte | fun getUByte(index: Int ? = null): <ERROR CLASS> Get the unsigned byte at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUByteImpl | abstract fun getUByteImpl(index: Int ? = null): <ERROR CLASS> Get the unsigned byte at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUInt | fun getUInt(index: Int ? = null): <ERROR CLASS> Get the unsigned integer at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUIntImpl | abstract fun getUIntImpl(index: Int ? = null): <ERROR CLASS> Get the unsigned integer at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getULong | fun getULong(index: Int ? = null): <ERROR CLASS> Get the unsigned long at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getULongImpl | abstract fun getULongImpl(index: Int ? = null): <ERROR CLASS> Get the unsigned long at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUShort | fun getUShort(index: Int ? = null): <ERROR CLASS> Get the unsigned short at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
getUShortImpl | abstract fun getUShortImpl(index: Int ? = null): <ERROR CLASS> Get the unsigned short at the current cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
hasRemaining | fun hasRemaining(): Boolean Return true if there is space between the cursor and the limit |
putByte | fun putByte(value: Byte , index: Int ? = null): Unit Put the given byte inside the buffer at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putByteImpl | abstract fun putByteImpl(value: Byte , index: Int ? = null): Unit Put the given byte inside the buffer at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putBytes | fun putBytes(array: ByteArray , index: Int ? = null): Unit Put the given byte array inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putBytesImpl | abstract fun putBytesImpl(array: ByteArray , index: Int ? = null): Unit Put the given byte array inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putInt | fun putInt(value: Int , index: Int ? = null): Unit Put the given integer inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putIntImpl | abstract fun putIntImpl(value: Int , index: Int ? = null): Unit Put the given integer inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putLong | fun putLong(value: Long , index: Int ? = null): Unit Put the given long inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putLongImpl | abstract fun putLongImpl(value: Long , index: Int ? = null): Unit Put the given long inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putShort | fun putShort(value: Short , index: Int ? = null): Unit Put the given short inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
putShortImpl | abstract fun putShortImpl(value: Short , index: Int ? = null): Unit Put the given short inside the buffer starting at the buffer cursor position. If the index parameter is provided, the cursor will be ignored and not modified |
remaining | fun remaining(): Int Get the space available between the cursor and the limit of the buffer |
reset | fun reset(): Unit Reset the buffer cursor and limit |
setCursorImpl | abstract fun setCursorImpl(index: Int ): Unit Used only by the JVM to synchronize the MultiplatformBuffer state with the ByteBuffer state |
setLimitImpl | abstract fun setLimitImpl(index: Int ): Unit Used only by the JVM to synchronize the MultiplatformBuffer state with the ByteBuffer state |
toArray | abstract fun toArray(): ByteArray Get all the data between the start of the buffer and its limit, the data is copied and is not linked to the content of the buffer. WARNING this behaviour is different from the ByteBuffer array() method, please read the documentation carefully |