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: Intcapacity of the buffer |
| cursor | var cursor: Intcursor keeping track of the position inside the buffer. The property must respect the property 0 <= cursor <= limit <= capacity |
| destroyed | var destroyed: Booleanis 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: Intlimit set for the buffer. The property must respect the property 0 <= cursor <= limit <= capacity |
| clone | abstract fun clone(): MultiplatformBufferDeep 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(): UnitDestroy 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): ByteGet a byte with its index in the buffer, the cursor will not be modified |
| getByte | fun getByte(index: Int? = null): ByteGet 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): ByteGet 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): ByteArrayGet 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): UnitCopy 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): ByteArrayGet 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): UnitCopy 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): IntGet 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): IntGet 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): LongGet 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): LongGet 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): ShortGet 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): ShortGet 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(): BooleanReturn true if there is space between the cursor and the limit |
| putByte | fun putByte(value: Byte, index: Int? = null): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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): UnitPut 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(): IntGet the space available between the cursor and the limit of the buffer |
| reset | fun reset(): UnitReset the buffer cursor and limit |
| setCursorImpl | abstract fun setCursorImpl(index: Int): UnitUsed only by the JVM to synchronize the MultiplatformBuffer state with the ByteBuffer state |
| setLimitImpl | abstract fun setLimitImpl(index: Int): UnitUsed only by the JVM to synchronize the MultiplatformBuffer state with the ByteBuffer state |
| toArray | abstract fun toArray(): ByteArrayGet 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 |