Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Buffer: Difference between revisions

From FiguraMC
Lexize (talk | contribs)
Lexize (talk | contribs)
Replaced the last byte in example buffer with DD (i am so stupid)
Line 57: Line 57:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| <code>^^</code> ||  ||  ||  
| <code>^^</code> ||  ||  ||  
Line 73: Line 73:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || <code>^^</code> ||  ||  
| || <code>^^</code> ||  ||  
Line 88: Line 88:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || || <code>^^</code> ||  
| || || <code>^^</code> ||  
Line 99: Line 99:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || || || || <code>^^</code>  
| || || || || <code>^^</code>  
Line 111: Line 111:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || || || || <code>^^</code>  
| || || || || <code>^^</code>  
Line 119: Line 119:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || || || || <code>^^</code>  
| || || || || <code>^^</code>  
Line 131: Line 131:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>FF</code> || <code>CC</code> || <code>EE</code> || <code>FF</code> || <code>FF</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> || <code>FF</code> || <code>FF</code>
|-
|-
| || || || || || || <code>^^</code>  
| || || || || || || <code>^^</code>  
Line 141: Line 141:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| <code>^^</code> ||  ||  ||  
| <code>^^</code> ||  ||  ||  
Line 200: Line 200:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| ||  || <code>^^</code>  ||  
| ||  || <code>^^</code>  ||  
Line 309: Line 309:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| || <code>^^</code> ||  ||  
| || <code>^^</code> ||  ||  
Line 330: Line 330:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| ||  || <code>^^</code> ||  
| ||  || <code>^^</code> ||  
Line 351: Line 351:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| ||  || <code>^^</code> ||  
| ||  || <code>^^</code> ||  
Line 372: Line 372:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 393: Line 393:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 414: Line 414:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 435: Line 435:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 456: Line 456:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| ||  || <code>^^</code> ||  
| ||  || <code>^^</code> ||  
Line 477: Line 477:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code>
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
|-
| ||  || <code>^^</code> ||  
| ||  || <code>^^</code> ||  
Line 498: Line 498:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 519: Line 519:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 540: Line 540:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 561: Line 561:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 586: Line 586:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 609: Line 609:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
Line 632: Line 632:
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>EE</code> ||
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
|-
| ||  ||  ||  || <code>^^</code>
| ||  ||  ||  || <code>^^</code>
|}
|}

Revision as of 07:16, 29 September 2024

Buffer is an utility object that can be created by using data:createBuffer(). It is used to get more control of what you are writing and reading, by providing bunch of methods for reading and writing bytes for specific primitive data types.

Basics

Supported primitives

ⓘ Note

On this page methods, instead of standard type names, will often use primitive names from this table for specifying arguments and return types, keep this in mind while reading this page.

Type name Size in bytes Description
byte 1 An unsigned 8 bit length integer. Can contain values in range [0; 255]
short 2 A signed 16 bit length integer. Can contain values in range [-32768; 32767]
ushort 2 An unsigned 16 bit length integer. Can contain values in range [0; 65535]
int 4 A signed 32 bit length integer. Can contain values in range [-2147483648; 2147483647]
long 8 A signed 64 bit length integer. Can contain values in range [-9223372036854775808; 9223372036854775807]
float 4 A single-precision float-pointing number.
double 8 A double-precision float-pointing number.

Buffer instantiation and capacity

There are two possible ways to instantiate a buffer:

Arguments Returns Description
data:createBuffer() Buffer Creates a buffer with initial capacity of 512 bytes.
data:createBuffer(capacity integer) Buffer Creates a buffer with specified initial capacity.

@see data:createBuffer

Buffer capacity is amount of bytes until buffer will need to reallocate more space. Once it reaches the cap, it reallocates buffer with additional 512 bytes. Buffer can not exceed the amount of allocated memory specified in permissions settings.

How buffer works

Buffers have a pointer to current byte in a buffer, and it is moved by read and write methods. Buffers has same pointer both for write and read operations.

For example:

AA BB CC DD
^^

Right now buffer is pointing to first byte (index 0), so when we do the following:

print(buf:read()) --> 170

@see read

Buffer will be pointing to the next byte:

AA BB CC DD
^^

Now when we will write a byte in a buffer:

buf:write(255)

@see write

It will rewrite the pointed byte, and buffer will be pointing to the next byte.

AA FF CC DD
^^

Once buffer reaches it's current end, read methods will act different

  • read will return -1.
  • Integer and float pointing read methods will read until end, and will fill remaining bytes with nulls.
  • readString, readByteArray, and readBase64 will read until end and stop. If pointer is already at end, it will return empty result.
AA BB CC DD
^^
print(buf:read()) --> -1
print(buf:readString()) --> ""

@see read, readString

AA BB CC DD
^^

Write methods will add new bytes if buffer is pointing it's current end, and will move the pointer further:

AA BB CC DD
^^
buf:writeUShort(65535)

@see writeUShort

This is how our buffer will look like now:

AA BB CC DD FF FF
^^

Buffer methods

This buffer example is going to be used in all code snippets below:

AA BB CC DD
^^

Buffer variable will be named buf.

In case if buffer state changes after method invocation, new state of example buffer will be displayed below the code snippet.

General

General methods of a buffer.

getLength


Returns length of this buffer.

Arguments Return Type
Buffer:getLength() integer
print(buf:getLength()) --> 4


getPosition


Returns current pointer position.

Arguments Return Type
getPosition() integer
print(buf:getPosition()) --> 0

setPosition


Sets current pointer position. Position will be clamped between 0 and current buffer length.

@see getLength

Arguments
setPosition(position integer)
buf:setPosition(2)
AA BB CC DD
^^

clear


Clears this buffer and sets it's pointer position to 0.

Arguments
clear()
buf:clear()
This buffer is empty, no data to display.

available


Returns amount of bytes available to read.

Arguments Return Type
available() integer
print(buf:available()) --> 4


getMaxCapacity


Returns max buffer capacity.

Arguments Return Type
getMaxCapacity() integer
print(buf:getMaxCapacity()) --> 1024000
ⓘ Note

This is an output for avatars in unmodified Default permissions group


close


Closes this buffer, tells garbage collector to free memory used by it. After closing buffer is unable to use, and any usage attempt will cause an error.

Arguments
close()
buf:close()
buf:read() --! Throws an error with message "This byte buffer is closed and cant be used anymore"

@see read

isClosed


Checks if this buffer is closed.

Arguments Return Type
isClosed() boolean
buf:close()
print(buf:isClosed()) --> true

@see close

Read methods

Methods of buffer related to reading data.

read


Reads a byte from this buffer and moves pointer by 1.

Arguments Return Type
read() byte
print(buf:read()) --> 170
AA BB CC DD
^^

readShort


Reads a short from this buffer and moves pointer by 2.

Arguments Return Type
readShort() short
print(buf:readShort()) --> -21829
AA BB CC DD
^^

readUShort


Reads a ushort from this buffer and moves pointer by 2.

Arguments Return Type
readUShort() ushort
print(buf:readUShort()) --> 43707
AA BB CC DD
^^

readInt


Reads an int from this buffer and moves pointer by 4.

Arguments Return Type
readInt() int
print(buf:readInt()) --> -1430532899
AA BB CC DD
^^

readLong


Reads a long from this buffer and moves pointer by 8.

Arguments Return Type
readLong() long
print(buf:readLong()) --> -6144092017057071104
AA BB CC DD
^^

readFloat


Reads a float from this buffer and moves pointer by 4.

Arguments Return Type
readFloat() float
print(buf:readFloat()) --> -3.336003e-13
AA BB CC DD
^^

readDouble


Reads a double from this buffer and moves pointer by 8.

Arguments Return Type
readDouble() double
print(buf:readDouble()) --> -7.757649e-103
AA BB CC DD
^^

readShortLE


Reads a short in little-endian format from this buffer and moves pointer by 2.

Arguments Return Type
readShortLE() short
print(buf:readShortLE()) --> -17494
AA BB CC DD
^^

readUShortLE


Reads a ushort in little-endian format from this buffer and moves pointer by 2.

Arguments Return Type
readUShortLE() ushort
print(buf:readUShortLE()) --> 48042
AA BB CC DD
^^

readIntLE


Reads an int in little-endian format from this buffer and moves pointer by 4.

Arguments Return Type
readIntLE() int
print(buf:readIntLE()) --> -573785174
AA BB CC DD
^^

readLongLE


Reads a long in little-endian format from this buffer and moves pointer by 8.

Arguments Return Type
readLongLE() long
print(buf:readLongLE()) --> 3721182122
AA BB CC DD
^^

readFloatLE


Reads a float in little-endian format from this buffer and moves pointer by 4.

Arguments Return Type
readFloatLE() float
print(buf:readFloatLE()) --> -1.844071e+18
AA BB CC DD
^^

readDoubleLE


Reads a double in little-endian format from this buffer and moves pointer by 8.

Arguments Return Type
readDoubleLE() double
print(buf:readDoubleLE()) --> 1.838508e-314
AA BB CC DD
^^

readString


Reads a string from this buffer.

Arguments Return Type Description
readString() string Reads all bytes from current buffer position to end of the buffer and returns them in form of UTF-8 string.
readString(length integer) string Reads specified amount of bytes from current buffer position and returns them in form of UTF-8 string. If length is more than available bytes amount, length will be clamped.
readString(length integer, encoding BufferEncoding) string Reads specified amount of bytes from current buffer position and returns them in form of string with specified encoding. If length is more than available bytes amount, length will be clamped.
print(buf:readString()) --> "ª»ÌÝ"
AA BB CC DD
^^

readBase64


Reads a Base64 string from this buffer.

Arguments Return Type Description
readBase64() string Reads all bytes from current buffer position to end of the buffer and returns them in form of Base64 encoded byte array.
readBase64(length integer) string Reads specified amount of bytes from current buffer position and returns them in form of Base64 encoded byte array. If length is more than available bytes amount, length will be clamped.
print(buf:readBase64()) --> "qrvM7g=="
AA BB CC DD
^^

readByteArray


Reads a byte array from this buffer.

Arguments Return Type Description
readByteArray() string Reads all bytes from current buffer position to end of the buffer and returns them in form of Lua string byte array.
readByteArray(length integer) string Reads specified amount of bytes from current buffer position and returns them in form of Lua string byte array. If length is more than available bytes amount, length will be clamped.
print(buf:readByteArray()) --> [raw data]
AA BB CC DD
^^