The buffer
library provides a way to easily read and write binary data.
-- Create a new buffer
local buffer = require("buffer").new();
Creates a new buffer (default UTF8 encoding and network byte order).
Supported encodings:
ascii
, latin1
, latin2
, latin9
,
utf8
, utf16
, utf32
, windows1250
, windows1251
, windows1252
.
Supported byte orders:
le
, be
, network
, native
.
local b1 = require("buffer").new();
local b2 = libs.buffer.new("utf16");
local b3 = libs.buffer.new("utf8", "le");
Returns the total length of the data in the buffer.
local b = require("buffer").new();
b:writebyte(123);
print(buffer:length()); -- 1
Returns the length of unread data in the buffer.
local b = require("buffer").new();
b:writebyte(123);
print(buffer.available()); -- 1
b:readbyte();
print(buffer.available()); -- 0
Returns the current position in the buffer.
local b = require("buffer").new();
b:writebyte(123);
print(b:position()); -- 0
b:readbyte();
print(b:position()); -- 1
Returns the byte at the specified position (zero-index based).
local b = require("buffer").new();
b:writestring("abc");
print(b:at(0)); -- 3
print(b:at(2)); -- 'b' 98
Returns a string representation of the buffer.
local b = require("buffer").new();
b:writebytes(12, 34, 56);
print(b:tostring()); -- [12,34,56]
Returns a string hex representation of the buffer.
local b = require("buffer").new();
b:writebytes(161, 178, 195);
print(b:tostring()); -- [A1,B2,C3]
Write raw data to buffer.
local b = require("buffer").new();
b:write("abc");
print(b:tostring()); -- [97,98,99]
Copies the data from another buffer.
local a = require("buffer").new();
b:write("abc");
local b = require("buffer").new();
b:writebuffer(a);
print(b:tostring()); -- [97,98,99]
Writes a string using the specified encoding (length-prefixed).
local b = libs.buffer.new("utf16");
b:writestring("abc");
print(b:tostring()); -- [6,97,0,98,0,99,0]
Writes a raw line of text using the specified encoding (not length-prefixed).
local b = require("buffer").new();
b:writeline("foobar");
print(b:readline()); -- foobar
Writes a single byte to the buffer.
local b = require("buffer").new();
b:writebyte(123);
print(b:tostring()); -- [123]
Writes multiple bytes to the buffer.
local b = require("buffer").new();
b:writebytes(12, 34);
b:writebytes({ 56, 78 });
print(b:tostring()); -- [12,34,56,78]
Writes an 8-bit integer value.
local b = require("buffer").new();
b:writeint8(123);
print(b:tostring()); -- [123]
Writes a 16-bit integer value.
local b = require("buffer").new();
b:writeint16(123);
print(b:tostring()); -- [0,123]
Writes a 32-bit integer value.
local b = require("buffer").new();
b:writeint32(123);
print(b:tostring()); -- [0,0,0,123]
Writes a 64-bit integer value.
local b = require("buffer").new();
b:writeint64(123);
print(b:tostring()); -- [0,0,0,0,0,0,0,123]
Writes a 32-bit floating point value.
local b = require("buffer").new();
b:writefloat(123.456);
print(b:tostring()); -- [67,122,0,0]
Writes a 64-bit floating point value.
local b = require("buffer").new();
b:writedouble(123.456);
print(b:tostring()); -- [64,111,64,0,0,0,0,0]
Read raw data from the buffer.
local b = require("buffer").new();
b:write("abc");
print(b:read(1)); -- a
print(b:read()); -- bc
Read data from the buffer into a new buffer.
local a = require("buffer").new();
a:write("abc");
print(a:read(1)); -- a
local b = a:readbuffer();
print(b:tostring()) -- [98,99]
Read a string using the specified encoding (length-prefixed).
local b = require("buffer").new();
b:writestring("abc");
print(b:readstring()); -- abc
Reads a raw line of text using the specified encoding (not length-prefixed). Returns nil
if no full lines available.
local b = require("buffer").new();
b:writeline("foobar");
print(b:readline()); -- foobar
b:readline(); -- nil
Reads a single byte from the buffer.
local b = require("buffer").new();
b:writebyte(123);
print(b:readbyte()); -- 123
Read multiple bytes from the buffer.
local b = require("buffer").new();
b:writestring("abc");
print(b:readbytes()); -- [3,97,98,99]
Read unsigned 8-bit integer value.
local b = require("buffer").new();
b:writeint8(250);
print(b:readuint8()); -- 250
Read signed 8-bit integer value.
local b = require("buffer").new();
b:writeint8(250);
print(b:readint8()); -- -6
Read unsigned 16-bit integer value.
local b = require("buffer").new();
b:writeint16(250);
print(b:readuint16()); -- 250
Read signed 16-bit integer value.
local b = require("buffer").new();
b:writeint16(250);
print(b:readint16()); -- -6
Read unsigned 32-bit integer value.
local b = require("buffer").new();
b:writeint32(250);
print(b:readuint32()); -- 250
Read signed 32-bit integer value.
local b = require("buffer").new();
b:writeint32(250);
print(b:readint32()); -- -6
Read unsigned 64-bit integer value.
local b = require("buffer").new();
b:writeint64(250);
print(b:readuint64()); -- 250
Read signed 64-bit integer value.
local b = require("buffer").new();
b:writeint64(250);
print(b:readint64()); -- -6
Read a 32-bit floating point value.
local b = require("buffer").new();
b:writefloat(123.456);
print(b:tostring()); -- [67,122,0,0]
Read a 64-bit floating point value.
local b = require("buffer").new();
b:writedouble(123.456);
print(b:tostring()); -- [64,111,64,0,0,0,0,0]