Class: RuneRb::IO::Buffer

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.0.1

Constant Summary

Constants included from Utils::Logging

Utils::Logging::LOG_FILE_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • modestring (String)

    the mode for the buffer. [‘r’, ‘w’]

  • data (String) (defaults to: '')

    the data.

Since:

  • 0.0.1



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_accessBoolean (readonly)

Returns is #bit_access enabled?.

Returns:

Since:

  • 0.0.1



9
10
11
# File 'lib/rrb/io/buffer.rb', line 9

def bit_access
  @bit_access
end

#dataString (readonly)

Returns the data.

Returns:

  • (String)

    the data.

Since:

  • 0.0.1



15
16
17
# File 'lib/rrb/io/buffer.rb', line 15

def data
  @data
end

#modeString (readonly)

Returns the access mode.

Returns:

  • (String)

    the access mode.

Since:

  • 0.0.1



12
13
14
# File 'lib/rrb/io/buffer.rb', line 12

def mode
  @mode
end

Instance Method Details

#hexString

Returns hex representation of the buffer’s data.

Returns:

  • (String)

    hex representation of the buffer’s data.

Since:

  • 0.0.1



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

#inspectObject

Since:

  • 0.0.1



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

#lengthInteger Also known as: size

The length of the underlying data.

Returns:

  • (Integer)

Since:

  • 0.0.1



36
37
38
# File 'lib/rrb/io/buffer.rb', line 36

def length
  @data.length
end

#peekString Also known as: snapshot

Fetches a snapshot of the message payload content.

Returns:

  • (String)

    a snapshot of the payload

Since:

  • 0.0.1



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.

Parameters:

  • data (String) (defaults to: '')

    the data to write.

Since:

  • 0.0.1



52
53
54
# File 'lib/rrb/io/buffer.rb', line 52

def push(data = '')
  @data.concat(data) unless data.empty?
end