Class: RuneRb::IO::Buffer
- Inherits:
-
Object
- Object
- RuneRb::IO::Buffer
- Includes:
- Utils::Logging
- Defined in:
- lib/rrb/io/buffer.rb
Overview
A Buffer object encapsulates a string of binary data and dynamically includes functions to interact with that data depending on the modestring.
Constant Summary
Constants included from Utils::Logging
Instance Attribute Summary collapse
-
#bit_access ⇒ Boolean
readonly
Is #bit_access enabled?.
-
#data ⇒ String
readonly
The data.
-
#mode ⇒ String
readonly
The access mode.
Instance Method Summary collapse
-
#hex ⇒ String
Hex representation of the buffer’s data.
-
#initialize(modestring, data: '') ⇒ Buffer
constructor
Construct a new instance of Buffer.
- #inspect ⇒ Object
-
#length ⇒ Integer
(also: #size)
The length of the underlying data.
-
#peek ⇒ String
(also: #snapshot)
Fetches a snapshot of the message payload content.
-
#push(data = '') ⇒ Object
(also: #<<)
Push data directly to the #data object.
Methods included from Utils::Logging
#class_name, #err, #err!, #log, #log!
Constructor Details
#initialize(modestring, data: '') ⇒ Buffer
Construct a new instance of RuneRb::IO::Buffer.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rrb/io/buffer.rb', line 21 def initialize(modestring, data: '') raise "Invalid mode-string for Buffer! Expecting: r, rn, w, rw Got: #{modestring}" unless /(?i)r|n|w/.match?(modestring) @data = data @mode = modestring enable_readable if /(?i)r/.match?(modestring) enable_writeable if /(?i)w/.match?(modestring) enable_native_readable if /(?i)n/.match?(modestring) self end |
Instance Attribute Details
#bit_access ⇒ Boolean (readonly)
Returns is #bit_access enabled?.
9 10 11 |
# File 'lib/rrb/io/buffer.rb', line 9 def bit_access @bit_access end |
#data ⇒ String (readonly)
Returns the data.
15 16 17 |
# File 'lib/rrb/io/buffer.rb', line 15 def data @data end |
#mode ⇒ String (readonly)
Returns the access mode.
12 13 14 |
# File 'lib/rrb/io/buffer.rb', line 12 def mode @mode end |
Instance Method Details
#hex ⇒ String
Returns hex representation of the buffer’s data.
67 68 69 70 71 |
# File 'lib/rrb/io/buffer.rb', line 67 def hex res = '' peek.each_byte { res << "#{_1 < 16 ? '0' : ''}#{_1.to_s(16)} " } res.strip end |
#inspect ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/rrb/io/buffer.rb', line 58 def inspect if @mode.include?('w') "[BufferMode:] #{@mode} || [BodyLength:] #{@data.length} || [BitAccess:] #{@bit_access} || [Payload:] #{hex}" else "[BufferMode:] #{@mode} || [BodyLength:] #{@data.length} || [Payload:] #{hex}" end end |
#length ⇒ Integer Also known as: size
The length of the underlying data.
36 37 38 |
# File 'lib/rrb/io/buffer.rb', line 36 def length @data.length end |
#peek ⇒ String Also known as: snapshot
Fetches a snapshot of the message payload content.
44 45 46 |
# File 'lib/rrb/io/buffer.rb', line 44 def peek @data.force_encoding(Encoding::BINARY) end |
#push(data = '') ⇒ Object Also known as: <<
Push data directly to the #data object.
52 53 54 |
# File 'lib/rrb/io/buffer.rb', line 52 def push(data = '') @data.concat(data) unless data.empty? end |