Buffer
11/22/25Less than 1 minute
Buffer
silly.adt.buffer is an efficient byte buffer implementation for handling network data stream concatenation and parsing.
Module Import
local buffer = require "silly.adt.buffer"API Reference
buffer.new()
Creates a new buffer object.
- Returns:
buffer- A new buffer object
buffer:append(data [, ...])
Appends data to the buffer.
- Parameters:
data:string|lightuserdata- Data to append...:string|lightuserdata- Additional data
- Returns:
integer- Current total bytes in the buffer - Notes:
- If the parameter is
lightuserdata, it must be followed by anintegertype length parameter. - Example:
buf:append(ptr, len)
- If the parameter is
buffer:read(n)
Reads a specified number of bytes from the buffer.
- Parameters:
n:integer- Number of bytes to read
- Returns:
string|nil- The read data, ornilif buffer has insufficient datainteger- Remaining bytes in the buffer
buffer:read(delim)
Reads data from the buffer until encountering the specified delimiter (including the delimiter).
- Parameters:
delim:string- Delimiter (must be a single-character string)
- Returns:
string|nil- The read data (including delimiter), ornilif delimiter not foundinteger- Remaining bytes in the buffer
buffer:readall()
Reads all data from the buffer.
- Returns:
string- All data in the buffer
buffer:clear()
Clears the buffer.
buffer:size()
Gets the current number of bytes in the buffer.
- Returns:
integer- Number of bytes
buffer:dump()
Gets debug information about the buffer.
- Returns:
table- A table containing the buffer's internal state