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:
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:
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"
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.
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.
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.
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
^^
Write methods
write
Writes a byte to this buffer and moves pointer by 1.