class BinData::Buffer
A Buffer
is conceptually a substream within a data stream. It has a defined size and it will always read or write the exact number of bytes to fill the buffer. Short reads will skip over unused bytes and short writes will pad the substream with “0” bytes.
require 'bindata' obj = BinData::Buffer.new(length: 5, type: [:string, {value: "abc"}]) obj.to_binary_s #=> "abc\000\000" class MyBuffer < BinData::Buffer default_parameter length: 8 endian :little uint16 :num1 uint16 :num2 # padding occurs here end obj = MyBuffer.read("\001\000\002\000\000\000\000\000") obj.num1 #=> 1 obj.num1 #=> 2 obj.raw_num_bytes #=> 4 obj.num_bytes #=> 8 class StringTable < BinData::Record endian :little uint16 :table_size_in_bytes buffer :strings, length: :table_size_in_bytes do array read_until: :eof do uint8 :len string :str, length: :len end end end
Parameters¶ ↑
Parameters may be provided at initialisation to control the behaviour of an object. These params are:
:length
-
The number of bytes in the buffer.
:type
-
The single type inside the buffer. Use a struct if multiple fields are required.
Public Instance Methods
assign(val)
click to toggle source
# File lib/bindata/buffer.rb, line 75 def assign(val) @type.assign(val) end
clear?()
click to toggle source
# File lib/bindata/buffer.rb, line 71 def clear? @type.clear? end
initialize_instance()
click to toggle source
# File lib/bindata/buffer.rb, line 62 def initialize_instance @type = get_parameter(:type).instantiate(nil, self) end
raw_num_bytes()
click to toggle source
The number of bytes used, ignoring the padding imposed by the buffer.
# File lib/bindata/buffer.rb, line 67 def raw_num_bytes @type.num_bytes end
snapshot()
click to toggle source
# File lib/bindata/buffer.rb, line 79 def snapshot @type.snapshot end