DataBlock
A
Properties
Property | Type | Description |
---|---|---|
The size of the block (in bytes) | ||
The CRC of the data |
Constructors
Creates a new, empty 0
).
Creates a new 0
.
Parameters
size (integer ): The size (in bytes) of the block
Functions
Overview
General
Reading
Writing
Attention
Note that these functions overwrite data, they do not insert
AsString
Gets the data as a lua
Returns
string : The data as a string
SetData
Sets the data contained within this
Parameters
data (string ): The data to import
Example
local data = DataBlock.new(10)
App.LogMessage('Size is ' .. data.size) -- 10
local str = 'This is a string that is 43 characters long'
data:SetData(str)
App.LogMessage('Size is ' .. data.size) -- 43
Clear
Clears all data and sets 0
.
Resize
Resizes the block to
Parameters
newSize (integer ): The new size of the block. If0
the resize will fail (useClear instead)preserveData (boolean ): Iftrue
, existing byte values in the block will be preserved, otherwise all bytes will be set to0
Returns
boolean :true
if the resize was successful
Copy
Copies data from another
Parameters
other (DataBlock ): TheDataBlock to copy data from
Returns
boolean :true
on success
CopyTo
Copies data to another
Parameters
other (DataBlock ): TheDataBlock to copy data to[offset] (integer ): Only copy bytes starting from this offset. Default is0
[length] (integer ): Copy this number of bytes. Default is0
, which means all bytes to the end of the block will be copied
Returns
boolean :true
on success
ImportFile
Imports data from a file on disk at
Parameters
path (string ): The path to the file on disk[offset] (integer ): Only import data starting from this offset in the file. Default is0
[length] (integer ): Import this number of bytes. Default is0
, which means all bytes to the end of the file will be imported
Returns
boolean :true
on success
ExportFile
Exports data to a file on disk at
Parameters
path (string ): The path to the file on disk. The file will be created if it doesn't already exist[offset] (integer ): Only export data starting from this offset. Default is0
[length] (integer ): Export this number of bytes. Default is0
, which means all bytes to the end of the block will be exported
Returns
boolean :true
on success
FillData
Sets all bytes in the block to
Parameters
value (integer ): The value to set all bytes to (0
-255
)
Returns
boolean :false
if the block is empty
ReadInt8
Reads the byte at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadUInt8
Reads the byte at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadInt16
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadUInt16
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadInt32
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadUInt32
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadInt64
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadUInt64
Reads the bytes beginning at
Parameters
offset (integer ): The offset to the start of the data to read
Returns
integer : The value read, ornil
if theoffset was invalid
ReadString
Reads
Parameters
offset (integer ): The offset to the start of the data to readlength (integer ): The number of bytes to read[nullTerminated] (boolean ): Iftrue
, the string will end at the first0
afteroffset , orlength bytes after offset, whichever comes first. Default isfalse
Returns
string : The string that was read, or an empty string if theoffset was invalid
Example
local data = DataBlock.new()
data:SetData('one two\0three')
local one = data:ReadString(0, 3)
local three = data:ReadString(8, 5)
App.LogMessage(one .. ' (' .. #one .. ')') -- one (3)
App.LogMessage(three .. ' (' .. #three .. ')') -- three (5)
local twoThree = data:ReadString(4, 9, false)
local twoThreeNT = data:ReadString(4, 9, true)
App.LogMessage('twoThree: ' .. #twoThree) -- twoThree: 9
App.LogMessage('twoThreeNT: ' .. #twoThreeNT) -- twoThreeNT: 3
WriteInt8
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteUInt8
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteInt16
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteUInt16
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteInt32
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteUInt32
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteInt64
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteUInt64
Writes
Parameters
offset (integer ): The offset to write data tovalue (integer ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully
WriteString
Writes the given string
Parameters
offset (integer ): The offset to write data tovalue (string ): The value to writeallowExpand (boolean ): Iftrue
, the data will be expanded to accomodate the written value if it goes past the end of the data, otherwise nothing will be written ifoffset is invalid
Returns
boolean :true
if the value was written successfully