ArrayReadWriteBuffer

class ArrayReadWriteBuffer(buffer: ByteArray, offset: Int = 0, val writeLimit: Int = -1, var writePosition: Int = offset) : ArrayReadBuffer, ReadWriteBuffer

Implements [ReadWriteBuffer] using ByteArray as backing buffer. Using array of bytes are usually faster than ByteBuffer.

This class is not thread-safe, meaning that it must operate on a single thread. Operating from multiple thread leads into an undefined behavior

All operations assume Little Endian byte order.

Constructors

Link copied to clipboard
constructor(initialCapacity: Int = 10)
constructor(buffer: ByteArray, offset: Int = 0, writeLimit: Int = -1, writePosition: Int = offset)

Properties

Link copied to clipboard
open override val capacity: Int

Maximum size in bytes that the backed buffer supports.

Link copied to clipboard
open override val limit: Int

Defines the size of the message in the buffer. It also determines last position that buffer can be read. Last byte to be accessed is in position limit() -1.

Link copied to clipboard
open override val writeLimit: Int

Defines last relative position of the backed buffer that can be written. Any addition to the buffer that goes beyond will throw an exception instead of regrow the buffer (default behavior).

Link copied to clipboard
open override var writePosition: Int

Current position of the buffer to be written. It will be automatically updated on put operations.

Functions

Link copied to clipboard
open override fun clear()

Clears (resets) the buffer so that it can be reused. Write position will be set to the start.

Link copied to clipboard
open override fun data(): ByteArray

Expose ReadBuffer as an array of bytes. This method is meant to be as efficient as possible, so for an array-backed ReadBuffer, it should return its own internal data. In case access to internal data is not possible, a copy of the data into an array of bytes might occur.

Link copied to clipboard
open override fun fill(value: Byte, start: Int, end: Int)
Link copied to clipboard
open override fun findFirst(value: Byte, start: Int, end: Int): Int

Scan through the buffer for first byte matching value.

Link copied to clipboard
open operator override fun get(index: Int): Byte

Read a Byte from the buffer.

Link copied to clipboard
open override fun getBoolean(index: Int): Boolean

Read boolean from the buffer. Booleans as stored as a single byte

Link copied to clipboard
open override fun getBytes(array: ByteArray, start: Int, length: Int)

Read a ByteArray from the buffer.

Link copied to clipboard
open override fun getDouble(index: Int): Double

Read a 64-bit float from the buffer.

Link copied to clipboard
open override fun getFloat(index: Int): Float

Read a 32-bit float from the buffer.

Link copied to clipboard
open override fun getInt(index: Int): Int

Read a Int from the buffer.

Link copied to clipboard
open override fun getLong(index: Int): Long

Read a Long from the buffer.

Link copied to clipboard
open override fun getShort(index: Int): Short

Read a Short from the buffer.

Link copied to clipboard
open override fun getString(start: Int, size: Int): String

Read a UTF-8 string from the buffer.

Link copied to clipboard
open override fun getUByte(index: Int): UByte

Read a UByte from the buffer.

Link copied to clipboard
open override fun getUInt(index: Int): UInt

Read a UInt from the buffer.

Link copied to clipboard
open override fun getULong(index: Int): ULong

Read a ULong from the buffer.

Link copied to clipboard
open override fun getUShort(index: Int): UShort

Read a UShort from the buffer.

Link copied to clipboard
open override fun moveWrittenDataToEnd(capacity: Int): Int

Special operation where we increase the backed buffer size to capacity and shift all already written data to the end of the buffer.

Link copied to clipboard
open override fun put(value: Boolean)

Put a Boolean into the buffer at writePosition . Booleans as stored as single byte. Write position will be incremented.

open override fun put(value: Byte)

Write a Byte into the buffer at writePosition. Write position will be incremented.

open override fun put(value: Double)

Write a 64-bit Double into the buffer at writePosition. Write position will be incremented.

open override fun put(value: Float)

Write a 32-bit Float into the buffer at writePosition. Write position will be incremented.

open override fun put(value: Int)

Write a Int in the buffer at writePosition. Write position will be incremented.

open override fun put(value: Long)

Write a Long into in the buffer at writePosition. Write position will be incremented.

open override fun put(value: Short)

Write a Short into in the buffer at writePosition. Write position will be incremented.

open override fun put(value: UByte)

Write a UByte into the buffer at writePosition. Write position will be incremented.

open override fun put(value: UInt)

Write a UInt into in the buffer at writePosition. Write position will be incremented.

open override fun put(value: ULong)

Write a ULong into in the buffer at writePosition. Write position will be incremented.

open override fun put(value: UShort)

Write a UShort into in the buffer at writePosition. Write position will be incremented.

open override fun put(value: CharSequence, encodedLength: Int): Int

Write a String encoded as UTF-8 into the buffer at writePosition. Write position will be incremented.

open override fun put(value: ReadBuffer, start: Int, length: Int)
open override fun put(value: ByteArray, start: Int, length: Int)

Put an array of bytes into the buffer at writePosition. Write position will be incremented.

Link copied to clipboard
open fun requestAdditionalCapacity(additional: Int, copyAtEnd: Boolean = false): Int

Request capacity of the buffer relative to writePosition. In case buffer is already larger than the requested, this method will just return true. Otherwise, It might try to resize the buffer. In case of being unable to allocate enough memory, an exception will be thrown.

Link copied to clipboard
open override fun requestCapacity(capacity: Int, copyAtEnd: Boolean): Int

Request capacity of the buffer. In case buffer is already larger than the requested, it is a no-op. Otherwise, It might try to resize the buffer. In case of being unable to allocate enough memory, an exception will be thrown.

Link copied to clipboard
open operator override fun set(index: Int, value: Boolean)

Write Boolean into a given position index on the buffer. Booleans as stored as single byte.

open operator override fun set(index: Int, value: Byte)

Write Byte into a given position index on the buffer.

open operator override fun set(index: Int, value: Double)

Write Double into a given position index on the buffer.

open operator override fun set(index: Int, value: Float)

Write Float into a given position index on the buffer.

open operator override fun set(index: Int, value: Int)

Write Int into a given position index on the buffer.

open operator override fun set(index: Int, value: Long)

Write Long into a given position index on the buffer.

open operator override fun set(index: Int, value: Short)

Short

open operator override fun set(index: Int, value: UByte)

Write UByte into a given position index on the buffer.

open operator override fun set(index: Int, value: UInt)

Write UInt into a given position index on the buffer.

open operator override fun set(index: Int, value: ULong)

Write ULong into a given position index on the buffer.

open operator override fun set(index: Int, value: UShort)

Write UShort into a given position index on the buffer.

open operator override fun set(dstIndex: Int, src: ReadBuffer, srcStart: Int, srcLength: Int)
open override fun set(dstIndex: Int, src: ByteArray, srcStart: Int, srcLength: Int)

Write an array of bytes into the buffer.

Link copied to clipboard
open override fun slice(start: Int, size: Int): ReadBuffer

Creates a new ReadBuffer point to a region of the current buffer, starting at start with size size.

Link copied to clipboard
open override fun writeSlice(offset: Int, size: Int): ReadWriteBuffer

Creates a new ReadWriteBuffer point to a region of the current buffer, starting at offset with size size.