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)
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 23: Line 23:
| 4
| 4
| A signed 32 bit length integer. Can contain values in range [-2147483648; 2147483647]
| A signed 32 bit length integer. Can contain values in range [-2147483648; 2147483647]
|-
| <code>uint</code>
| 4
| An unsigned 32 bit length integer. Can contain values in range [0; 4294967295]
|-
|-
| <code>long</code>
| <code>long</code>
| 8
| 8
| A signed 64 bit length integer. Can contain values in range [-9223372036854775808; 9223372036854775807]
| A signed 64 bit length integer. Can contain values in range [-9223372036854775808; 9223372036854775807]
|-
| <code>ulong</code>
| 8
| An usigned 64 bit length integer. Can contain values in range [0; 18446744073709551615]
|-
|-
| <code>float</code>
| <code>float</code>
Line 43: Line 35:
| 8
| 8
| A double-precision float-pointing number.
| A double-precision float-pointing number.
|-
| <code>str</code>
| 2+Length
| A string primitive that can be stored in buffers. Starts with 1 unsigned short defining the length of the string, and followed with string bytes.
|}
|}


Line 69: 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 85: 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 100: 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 111: 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 123: 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 131: 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 143: 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 153: 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> ||  ||  ||  
|}
|}


Buffer variable will be named <code>buf</code>
Buffer variable will be named <code>buf</code>.
 
In case if buffer state changes after method invocation, new state of example buffer will be displayed below the code snippet.


=== General ===
=== General ===
Line 210: 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 294: Line 284:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
buf:close()
buf:close()
print(buf:isClosed())
print(buf:isClosed()) --> true
</syntaxhighlight>
</syntaxhighlight>


'''@see''' [[#close|close]]
'''@see''' [[#close|close]]
=== Read methods ===
Methods of buffer related to reading data.
==== read ====
----
Reads a [[#Supported_primitives|<code>byte</code>]] from this buffer and moves pointer by 1.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>read()</code> || [[#Supported_primitives|<code>byte</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:read()) --> 170
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| || <code>^^</code> ||  ||
|}
==== readShort ====
----
Reads a [[#Supported_primitives|<code>short</code>]] from this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readShort()</code> || [[#Supported_primitives|<code>short</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readShort()) --> -21829
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| ||  || <code>^^</code> ||
|}
==== readUShort ====
----
Reads a [[#Supported_primitives|<code>ushort</code>]] from this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readUShort()</code> || [[#Supported_primitives|<code>ushort</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readUShort()) --> 43707
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| ||  || <code>^^</code> ||
|}
==== readInt ====
----
Reads an [[#Supported_primitives|<code>int</code>]] from this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readInt()</code> || [[#Supported_primitives|<code>int</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readInt()) --> -1430532899
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readLong ====
----
Reads a [[#Supported_primitives|<code>long</code>]] from this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readLong()</code> || [[#Supported_primitives|<code>long</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readLong()) --> -6144092017057071104
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readFloat ====
----
Reads a [[#Supported_primitives|<code>float</code>]] from this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readFloat()</code> || [[#Supported_primitives|<code>float</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readFloat()) --> -3.336003e-13
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readDouble ====
----
Reads a [[#Supported_primitives|<code>double</code>]] from this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readDouble()</code> || [[#Supported_primitives|<code>double</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readDouble()) --> -7.757649e-103
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readShortLE ====
----
Reads a [[#Supported_primitives|<code>short</code>]] in little-endian format from this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readShortLE()</code> || [[#Supported_primitives|<code>short</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readShortLE()) --> -17494
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| ||  || <code>^^</code> ||
|}
==== readUShortLE ====
----
Reads a [[#Supported_primitives|<code>ushort</code>]]  in little-endian format from this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readUShortLE()</code> || [[#Supported_primitives|<code>ushort</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readUShortLE()) --> 48042
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| ||  || <code>^^</code> ||
|}
==== readIntLE ====
----
Reads an [[#Supported_primitives|<code>int</code>]] in little-endian format from this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readIntLE()</code> || [[#Supported_primitives|<code>int</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readIntLE()) --> -573785174
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readLongLE ====
----
Reads a [[#Supported_primitives|<code>long</code>]] in little-endian format from this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readLongLE()</code> || [[#Supported_primitives|<code>long</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readLongLE()) --> 3721182122
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readFloatLE ====
----
Reads a [[#Supported_primitives|<code>float</code>]] in little-endian format from this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readFloatLE()</code> || [[#Supported_primitives|<code>float</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readFloatLE()) --> -1.844071e+18
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readDoubleLE ====
----
Reads a [[#Supported_primitives|<code>double</code>]] in little-endian format from this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>readDoubleLE()</code> || [[#Supported_primitives|<code>double</code>]]
|}
<syntaxhighlight lang="lua">
print(buf:readDoubleLE()) --> 1.838508e-314
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readString ====
----
Reads a string from this buffer.
{| class="wikitable"
! Arguments !! Return Type !! Description
|-
| <code>readString()</code> || {{type|string}} || Reads all bytes from current buffer position to end of the buffer and returns them in form of UTF-8 string.
|-
| <code>readString(length {{type|integer}})</code> || {{type|string}} || Reads specified amount of bytes from current buffer position and returns them in form of UTF-8 string. If <code>length</code> is more than available bytes amount, length will be clamped.
|-
| <code>readString(length {{type|integer}}, encoding {{type|BufferEncoding}})</code> || {{type|string}} || Reads specified amount of bytes from current buffer position and returns them in form of string with specified encoding. If <code>length</code> is more than available bytes amount, length will be clamped.
|}
<syntaxhighlight lang="lua">
print(buf:readString()) --> "ª»ÌÝ"
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readBase64 ====
----
Reads a Base64 string from this buffer.
{| class="wikitable"
! Arguments !! Return Type !! Description
|-
| <code>readBase64()</code> || {{type|string}} || Reads all bytes from current buffer position to end of the buffer and returns them in form of Base64 encoded byte array.
|-
| <code>readBase64(length {{type|integer}})</code> || {{type|string}} || Reads specified amount of bytes from current buffer position and returns them in form of Base64 encoded byte array. If <code>length</code> is more than available bytes amount, length will be clamped.
|}
<syntaxhighlight lang="lua">
print(buf:readBase64()) --> "qrvM7g=="
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
==== readByteArray ====
----
Reads a byte array from this buffer.
{| class="wikitable"
! Arguments !! Return Type !! Description
|-
| <code>readByteArray()</code> || {{type|string}} || Reads all bytes from current buffer position to end of the buffer and returns them in form of Lua string byte array.
|-
| <code>readByteArray(length {{type|integer}})</code> || {{type|string}} || Reads specified amount of bytes from current buffer position and returns them in form of Lua string byte array. If <code>length</code> is more than available bytes amount, length will be clamped.
|}
<syntaxhighlight lang="lua">
print(buf:readByteArray()) --> [raw data]
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| ||  ||  ||  || <code>^^</code>
|}
=== Write methods ===
==== write ====
----
Writes a [[#Supported_primitives|<code>byte</code>]] to this buffer and moves pointer by 1.
{| class="wikitable"
! Arguments
|-
| <code>write(value [[#Supported_primitives|<code>byte</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:write(255)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>FF</code> || <code>BB</code> || <code>CC</code> || <code>DD</code>
|-
| || <code>^^</code> ||  ||
|}
==== writeShort ====
----
Writes a [[#Supported_primitives|<code>short</code>]] to this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments
|-
| <code>writeShort(value [[#Supported_primitives|<code>short</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeShort(32767)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>7F</code> || <code>FF</code> || <code>CC</code> || <code>DD</code>
|-
| || || <code>^^</code> ||
|}
==== writeUShort ====
----
Writes a [[#Supported_primitives|<code>ushort</code>]] to this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments
|-
| <code>writeUShort(value [[#Supported_primitives|<code>ushort</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeUShort(32767)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>7F</code> || <code>FF</code> || <code>CC</code> || <code>DD</code>
|-
| || || <code>^^</code> ||
|}
==== writeInt ====
----
Writes an [[#Supported_primitives|<code>int</code>]] to this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments
|-
| <code>writeInt(value [[#Supported_primitives|<code>int</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeInt(2147483647)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>7F</code> || <code>FF</code> || <code>FF</code> || <code>FF</code>
|-
| || || || || <code>^^</code>
|}
==== writeLong ====
----
Writes a [[#Supported_primitives|<code>long</code>]] to this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments
|-
| <code>writeLong(value [[#Supported_primitives|<code>long</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeLong(9223372036854775807)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>7F</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code>
|-
| || || || || || || || || <code>^^</code>
|}
==== writeFloat ====
----
Writes a [[#Supported_primitives|<code>float</code>]] to this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments
|-
| <code>writeFloat(value [[#Supported_primitives|<code>float</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeFloat(50.0)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>42</code> || <code>48</code> || <code>00</code> || <code>00</code>
|-
| || || || || <code>^^</code>
|}
==== writeDouble ====
----
Writes a [[#Supported_primitives|<code>double</code>]] to this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments
|-
| <code>writeDouble(value [[#Supported_primitives|<code>double</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeDouble(50.0)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>40</code> || <code>49</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code>
|-
| || || || || || || || || <code>^^</code>
|}
==== writeShortLE ====
----
Writes a [[#Supported_primitives|<code>short</code>]] in little-endian format to this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments
|-
| <code>writeShortLE(value [[#Supported_primitives|<code>short</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeShortLE(32767)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>FF</code> || <code>7F</code> || <code>CC</code> || <code>DD</code>
|-
| || || <code>^^</code> ||
|}
==== writeUShortLE ====
----
Writes a [[#Supported_primitives|<code>ushort</code>]] in little-endian format to this buffer and moves pointer by 2.
{| class="wikitable"
! Arguments
|-
| <code>writeUShortLE(value [[#Supported_primitives|<code>ushort</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeUShortLE(32767)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>FF</code> || <code>7F</code> || <code>CC</code> || <code>DD</code>
|-
| || || <code>^^</code> ||
|}
==== writeIntLE ====
----
Writes an [[#Supported_primitives|<code>int</code>]] in little-endian format to this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments
|-
| <code>writeIntLE(value [[#Supported_primitives|<code>int</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeIntLE(2147483647)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>7F</code>
|-
| || || || || <code>^^</code>
|}
==== writeLongLE ====
----
Writes a [[#Supported_primitives|<code>long</code>]] in little-endian format to this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments
|-
| <code>writeLongLE(value [[#Supported_primitives|<code>long</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeLongLE(9223372036854775807)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>FF</code> || <code>7F</code>
|-
| || || || || || || || || <code>^^</code>
|}
==== writeFloatLE ====
----
Writes a [[#Supported_primitives|<code>float</code>]] in little-endian format to this buffer and moves pointer by 4.
{| class="wikitable"
! Arguments
|-
| <code>writeFloatLE(value [[#Supported_primitives|<code>float</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeFloatLE(50.0)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>00</code> || <code>00</code> || <code>48</code> || <code>42</code>
|-
| || || || || <code>^^</code>
|}
==== writeDoubleLE ====
----
Writes a [[#Supported_primitives|<code>double</code>]] in little-endian format to this buffer and moves pointer by 8.
{| class="wikitable"
! Arguments
|-
| <code>writeDoubleLE(value [[#Supported_primitives|<code>double</code>]])</code>
|}
<syntaxhighlight lang="lua">
buf:writeDoubleLE(50.0)
</syntaxhighlight>
{| class="wikitable"
|-
| <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>00</code> || <code>49</code> || <code>40</code>
|-
| || || || || || || || || <code>^^</code>
|}
==== writeString ====
----
Writes a string in buffer, and returns it's length in bytes.
{| class="wikitable"
! Arguments !! Return type !! Description
|-
| <code>writeString(value {{type|string}})</code> || {{type|integer}} || Writes a string in UTF-8 encoding.
|-
| <code>writeString(value {{type|string}}, encoding {{type|BufferEncoding}})</code> || {{type|integer}} || Writes a string in specified encoding.
|}
<syntaxhighlight lang="lua">
print(buf:writeString("Hello, world!")) --> 13
</syntaxhighlight>
{| class="wikitable"
|-
| <code>48</code> || <code>65</code> || <code>6C</code> || <code>6C</code> || <code>6F</code> || <code>2C</code> || <code>20</code> || <code>77</code> || <code>6F</code> || <code>72</code> || <code>6C</code> || <code>64</code> || <code>21</code> ||
|-
| || || || || || || || || || || || || || <code>^^</code>
|}
==== writeBase64 ====
----
Decodes Base64 byte array and writes in to buffer. Returns amount of written bytes.
{| class="wikitable"
! Arguments !! Return type
|-
| <code>writeBase64(value {{type|string}})</code> || {{type|integer}}
|}
<syntaxhighlight lang="lua">
print(buf:writeBase64("TWVvdw==")) --> 4
</syntaxhighlight>
{| class="wikitable"
|-
| <code>4D</code> || <code>65</code> || <code>6F</code> || <code>77</code> ||
|-
| || || || || <code>^^</code>
|}
==== writeByteArray ====
----
Write a lua string byte array to buffer. Returns amount of written bytes.
{| class="wikitable"
! Arguments !! Return type
|-
| <code>writeByteArray(value {{type|string}})</code> || {{type|integer}}
|}
<syntaxhighlight lang="lua">
print(buf:writeByteArray("\xAA\xBB\xCC\xDD")) --> 4
</syntaxhighlight>
{| class="wikitable"
|-
| <code>AA</code> || <code>BB</code> || <code>CC</code> || <code>DD</code> ||
|-
| || || || || <code>^^</code>
|}
=== Streams ===
Methods for working with Input and Output streams
==== readFromStream ====
----
Reads provided stream, and writes it's data to this buffer. Returns amount of bytes written to buffer.
{| class="wikitable"
! Arguments !! Return type !! Description
|-
| <code>readFromStream(stream {{type|InputStream}})</code> || {{type|integer}} || Reads provided stream to either max capacity of buffer, or end of stream.
|-
| <code>readFromStream(stream {{type|InputStream}}, amount {{type|integer}})</code> || {{type|integer}} || Trying to read specified amount of bytes from provided stream. Resulting amount of read bytes might be less than specified.
|}
<syntaxhighlight lang="lua">
-- Opening resource from avatar resources
local is = resources:get("buf_read_test.txt")
-- Reading content of resource, printing it's length
print(buf:readFromStream(is)) --> 13
-- Resetting buffer position so it can read string from start
buf:setPosition(0)
-- Reading string
print(buf:readString()) --> "Hello, world!"
-- Closing input stream
is:close()
</syntaxhighlight>
'''@see''' [[ResourcesAPI#get|resources:get]], [[#setPosition|setPosition]], [[#readString|readString]]
{| class="wikitable"
|-
| <code>48</code> || <code>65</code> || <code>6C</code> || <code>6C</code> || <code>6F</code> || <code>2C</code> || <code>20</code> || <code>77</code> || <code>6F</code> || <code>72</code> || <code>6C</code> || <code>64</code> || <code>21</code> ||
|-
| || || || || || || || || || || || || || <code>^^</code>
|}
==== writeToStream ====
----
Writes bytes from this buffer to provided output stream. Returns amount of bytes written to stream.
{| class="wikitable"
! Arguments !! Return type !! Description
|-
| <code>writeToStream(stream {{type|OutputStream}})</code> || {{type|integer}} || Writes all available bytes to provided stream.
|-
| <code>writeToStream(stream {{type|OutputStream}}, amount {{type|integer}})</code> || {{type|integer}} || Writes specified amount of bytes from current position to provided stream.
|}
<syntaxhighlight lang="lua">
-- Opening output stream with FileAPI
local os = file:openWriteStream("buf_write_test.txt")
-- Preparing future file content
buf:writeString("Hello, world!")
-- Resetting buffer position, so buffer can write data to stream from start
buf:setPosition(0)
-- Writing file data
buf:writeToStream(os)
-- Closing output stream
os:close()
</syntaxhighlight>
'''@see''' [[FileAPI#openWriteStream|file:openWriteStream]], [[#writeString|writeString]], [[#writeString|setPosition]], [[OutputStream#close|OutputStream:close]]
{| class="wikitable"
|-
| <code>48</code> || <code>65</code> || <code>6C</code> || <code>6C</code> || <code>6F</code> || <code>2C</code> || <code>20</code> || <code>77</code> || <code>6F</code> || <code>72</code> || <code>6C</code> || <code>64</code> || <code>21</code> ||
|-
| || || || || || || || || || || || || || <code>^^</code>
|}

Latest revision as of 10:57, 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
^^

Write methods

write


Writes a byte to this buffer and moves pointer by 1.

Arguments
write(value byte)
buf:write(255)
FF BB CC DD
^^

writeShort


Writes a short to this buffer and moves pointer by 2.

Arguments
writeShort(value short)
buf:writeShort(32767)
7F FF CC DD
^^

writeUShort


Writes a ushort to this buffer and moves pointer by 2.

Arguments
writeUShort(value ushort)
buf:writeUShort(32767)
7F FF CC DD
^^

writeInt


Writes an int to this buffer and moves pointer by 4.

Arguments
writeInt(value int)
buf:writeInt(2147483647)
7F FF FF FF
^^

writeLong


Writes a long to this buffer and moves pointer by 8.

Arguments
writeLong(value long)
buf:writeLong(9223372036854775807)
7F FF FF FF FF FF FF FF
^^

writeFloat


Writes a float to this buffer and moves pointer by 4.

Arguments
writeFloat(value float)
buf:writeFloat(50.0)
42 48 00 00
^^

writeDouble


Writes a double to this buffer and moves pointer by 8.

Arguments
writeDouble(value double)
buf:writeDouble(50.0)
40 49 00 00 00 00 00 00
^^

writeShortLE


Writes a short in little-endian format to this buffer and moves pointer by 2.

Arguments
writeShortLE(value short)
buf:writeShortLE(32767)
FF 7F CC DD
^^

writeUShortLE


Writes a ushort in little-endian format to this buffer and moves pointer by 2.

Arguments
writeUShortLE(value ushort)
buf:writeUShortLE(32767)
FF 7F CC DD
^^

writeIntLE


Writes an int in little-endian format to this buffer and moves pointer by 4.

Arguments
writeIntLE(value int)
buf:writeIntLE(2147483647)
FF FF FF 7F
^^

writeLongLE


Writes a long in little-endian format to this buffer and moves pointer by 8.

Arguments
writeLongLE(value long)
buf:writeLongLE(9223372036854775807)
FF FF FF FF FF FF FF 7F
^^

writeFloatLE


Writes a float in little-endian format to this buffer and moves pointer by 4.

Arguments
writeFloatLE(value float)
buf:writeFloatLE(50.0)
00 00 48 42
^^

writeDoubleLE


Writes a double in little-endian format to this buffer and moves pointer by 8.

Arguments
writeDoubleLE(value double)
buf:writeDoubleLE(50.0)
00 00 00 00 00 00 49 40
^^

writeString


Writes a string in buffer, and returns it's length in bytes.

Arguments Return type Description
writeString(value string) integer Writes a string in UTF-8 encoding.
writeString(value string, encoding BufferEncoding) integer Writes a string in specified encoding.
print(buf:writeString("Hello, world!")) --> 13
48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21
^^

writeBase64


Decodes Base64 byte array and writes in to buffer. Returns amount of written bytes.

Arguments Return type
writeBase64(value string) integer
print(buf:writeBase64("TWVvdw==")) --> 4
4D 65 6F 77
^^


writeByteArray


Write a lua string byte array to buffer. Returns amount of written bytes.

Arguments Return type
writeByteArray(value string) integer
print(buf:writeByteArray("\xAA\xBB\xCC\xDD")) --> 4
AA BB CC DD
^^

Streams

Methods for working with Input and Output streams

readFromStream


Reads provided stream, and writes it's data to this buffer. Returns amount of bytes written to buffer.

Arguments Return type Description
readFromStream(stream InputStream) integer Reads provided stream to either max capacity of buffer, or end of stream.
readFromStream(stream InputStream, amount integer) integer Trying to read specified amount of bytes from provided stream. Resulting amount of read bytes might be less than specified.
-- Opening resource from avatar resources
local is = resources:get("buf_read_test.txt")
-- Reading content of resource, printing it's length
print(buf:readFromStream(is)) --> 13
-- Resetting buffer position so it can read string from start
buf:setPosition(0)
-- Reading string
print(buf:readString()) --> "Hello, world!"
-- Closing input stream
is:close()

@see resources:get, setPosition, readString

48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21
^^

writeToStream


Writes bytes from this buffer to provided output stream. Returns amount of bytes written to stream.

Arguments Return type Description
writeToStream(stream OutputStream) integer Writes all available bytes to provided stream.
writeToStream(stream OutputStream, amount integer) integer Writes specified amount of bytes from current position to provided stream.
-- Opening output stream with FileAPI
local os = file:openWriteStream("buf_write_test.txt")
-- Preparing future file content
buf:writeString("Hello, world!")
-- Resetting buffer position, so buffer can write data to stream from start
buf:setPosition(0)
-- Writing file data
buf:writeToStream(os)
-- Closing output stream
os:close()

@see file:openWriteStream, writeString, setPosition, OutputStream:close

48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21
^^