Class: Majoron::AntHill::BinaryEncoder

Inherits:
EncoderBase show all
Defined in:
lib/binary_encoder.rb

Instance Method Summary collapse

Constructor Details

#initializeBinaryEncoder

Constructor.



23
24
25
26
27
28
# File 'lib/binary_encoder.rb', line 23

def initialize()
  super()
  @offset = 0
  @buffer = ""

end

Instance Method Details

#append_buffer(buf) ⇒ Object



121
122
123
# File 'lib/binary_encoder.rb', line 121

def append_buffer(buf)
  @buffer << buf
end

#encode_binary(value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/binary_encoder.rb', line 96

def encode_binary(value)
    # Encoding::CompatibilityError: incompatible character encodings: US-ASCII and ASCII-8BIT
    # ASCII-8BIT is a binary data, I am not able to find how to set encoding in constructor
    if RUBY_VERSION >= "1.9.0"
      @buffer.force_encoding("ASCII-8BIT")
    end

  sizeof = value.size()
  @buffer << value
  @offset += sizeof
end

#encode_char(value) ⇒ Object

Encode functions



44
45
46
47
48
# File 'lib/binary_encoder.rb', line 44

def encode_char(value)
  sizeof = 1
  @buffer << value
  @offset += sizeof
end

#encode_cstring(value, limit = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/binary_encoder.rb', line 84

def encode_cstring(value, limit = nil)
    # Encoding::CompatibilityError: incompatible character encodings: US-ASCII and ASCII-8BIT
    # ASCII-8BIT is a binary data, I am not able to find how to set encoding in constructor
    if RUBY_VERSION >= "1.9.0"
      @buffer.force_encoding("ASCII-8BIT")
    end

  sizeof = value.size() + 1      # 1 = NULL terminator
  @buffer << value << 0        # 0 = NULL terminator
  @offset += sizeof
end

#encode_integer16(value) ⇒ Object



66
67
68
69
# File 'lib/binary_encoder.rb', line 66

def encode_integer16(value)
  # TODO:  implement
  raise "encode_integer16 is not implemented"
end

#encode_integer24(value) ⇒ Object



61
62
63
64
# File 'lib/binary_encoder.rb', line 61

def encode_integer24(value)
  # TODO:  implement
  raise "encode_integer24 is not implemented"
end

#encode_integer32(value) ⇒ Object



56
57
58
59
# File 'lib/binary_encoder.rb', line 56

def encode_integer32(value)
  # TODO:  implement
  raise "encode_integer32 is not implemented"
end

#encode_uchar(value) ⇒ Object



50
51
52
53
54
# File 'lib/binary_encoder.rb', line 50

def encode_uchar(value)
  sizeof = 1
  @buffer << value
  @offset += sizeof
end

#encode_unsigned16(value) ⇒ Object



78
79
80
81
82
# File 'lib/binary_encoder.rb', line 78

def encode_unsigned16(value)
  sizeof = 2
  @buffer << sprintf("%c%c", value >> 8, value & 0xff)
  @offset += sizeof
end

#encode_unsigned32(value) ⇒ Object



71
72
73
74
75
76
# File 'lib/binary_encoder.rb', line 71

def encode_unsigned32(value)
  sizeof = 4
  # @buffer << sprintf("%c%c%c%c", value >> 24, value >> 16, value >> 8, value & 0xff)
    @buffer << sprintf("%c%c%c%c", value >> 24, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff)
  @offset += sizeof
end

#get_bufferObject



116
117
118
# File 'lib/binary_encoder.rb', line 116

def get_buffer()
  @buffer
end

#padding_n_bytes(bytes) ⇒ Object

Padding function to align on n bytes



38
39
40
41
# File 'lib/binary_encoder.rb', line 38

def padding_n_bytes(bytes)
  # TODO:  implement
  raise "padding_n_bytes is not implemented"
end

#reset_bufferObject



110
111
112
113
# File 'lib/binary_encoder.rb', line 110

def reset_buffer()
  @offset = 0
  @buffer = ""
end

#skip_n_bytes(bytes) ⇒ Object

Method to skip n bytes



31
32
33
34
# File 'lib/binary_encoder.rb', line 31

def skip_n_bytes(bytes)
  # TODO:  implement
  raise "skip_n_bytes is not implemented"
end