Class: TypeDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/protocol/type_deserializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/protocol/type_deserializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/protocol/type_deserializer.rb

Class Method Summary collapse

Class Method Details

.deserialize_bool(encoded_bool) ⇒ Object



36
37
38
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 36

def self.deserialize_bool(encoded_bool)
  encoded_bool[0] == 1
end

.deserialize_byte(encoded_byte) ⇒ Object



64
65
66
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 64

def self.deserialize_byte(encoded_byte)
  encoded_byte[0]
end

.deserialize_char(encoded_char) ⇒ Object



68
69
70
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 68

def self.deserialize_char(encoded_char)
  encoded_char[0].ord
end

.deserialize_command(command_byte_array) ⇒ Object



5
6
7
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 5

def self.deserialize_command(command_byte_array)
  Command.new(RuntimeName(command_byte_array[0]), CommandType(command_byte_array[1]), [])
end

.deserialize_double(encoded_double) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 83

def self.deserialize_double(encoded_double)
  # Reconstruct the 64-bit bit pattern from the byte array.
  bits = 0
  encoded_double.each_with_index do |byte, index|
    bits |= (byte & 0xFF) << (8 * index)
  end

  # IEEE 754 double precision (64-bit) decoding:
  sign     = ((bits >> 63) == 0) ? 1 : -1
  exponent = (bits >> 52) & 0x7FF
  fraction = bits & 0xFFFFFFFFFFFFF

  if exponent == 2047
    return fraction == 0 ? sign * Float::INFINITY : Float::NAN
  elsif exponent == 0
    return sign * (fraction.to_f / (1 << 52)) * (2 ** (-1022))
  else
    return sign * (1 + fraction.to_f / (1 << 52)) * (2 ** (exponent - 1023))
  end
end

.deserialize_float(encoded_float) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 40

def self.deserialize_float(encoded_float)
  # Reconstruct the 32-bit integer from the byte array.
  bits = 0
  encoded_float.each_with_index do |byte, index|
    bits |= (byte & 0xFF) << (8 * index)
  end

  # IEEE 754 single precision (32-bit) decoding:
  sign     = ((bits >> 31) == 0) ? 1 : -1
  exponent = (bits >> 23) & 0xFF
  fraction = bits & 0x7FFFFF

  if exponent == 255
    # Handle special cases for Inf and NaN.
    return fraction == 0 ? sign * Float::INFINITY : Float::NAN
  elsif exponent == 0
    # Subnormal numbers.
    return sign * (fraction.to_f / (1 << 23)) * (2 ** (-126))
  else
    # Normalized number.
    return sign * (1 + fraction.to_f / (1 << 23)) * (2 ** (exponent - 127))
  end
end

.deserialize_int(encoded_int) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 25

def self.deserialize_int(encoded_int)
  # Assuming a 32-bit little-endian integer.
  value = 0
  encoded_int.each_with_index do |byte, index|
    value |= (byte & 0xFF) << (8 * index)
  end
  # Sign the extension if the 31st bit is set.
  value -= (1 << 32) if (value & (1 << 31)) != 0
  value
end

.deserialize_longlong(encoded_long) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 72

def self.deserialize_longlong(encoded_long)
  # Assuming a 64-bit little-endian integer.
  value = 0
  encoded_long.each_with_index do |byte, index|
    value |= (byte & 0xFF) << (8 * index)
  end
  # Sign extension for 64-bit: if the 63rd bit is set.
  value -= (1 << 64) if value >= (1 << 63)
  value
end

.deserialize_nil(encoded_nil) ⇒ Object



122
123
124
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 122

def self.deserialize_nil(encoded_nil)
  nil
end

.deserialize_string(string_encoding_mode, encoded_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 9

def self.deserialize_string(string_encoding_mode, encoded_string)
  raw_str = encoded_string.map(&:chr).join
  case string_encoding_mode
  when StringEncodingMode::ASCII
    raw_str.force_encoding("US-ASCII").encode("UTF-8")
  when StringEncodingMode::UTF8
    raw_str.force_encoding("UTF-8").encode("UTF-8")
  when StringEncodingMode::UTF16
    raw_str.force_encoding("UTF-16LE").encode("UTF-8")
  when StringEncodingMode::UTF32
    raw_str.force_encoding("UTF-32").encode("UTF-8")
  else
    raise "Argument out of range in deserialize_string"
  end
end

.deserialize_uint(encoded_uint) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 113

def self.deserialize_uint(encoded_uint)
  # Assuming an unsigned 32-bit little-endian integer.
  value = 0
  encoded_uint.each_with_index do |byte, index|
    value |= (byte & 0xFF) << (8 * index)
  end
  value
end

.deserialize_ullong(encoded_ullong) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/javonet-ruby-sdk/core/protocol/type_deserializer.rb', line 104

def self.deserialize_ullong(encoded_ullong)
  # Assuming an unsigned 64-bit little-endian integer.
  value = 0
  encoded_ullong.each_with_index do |byte, index|
    value |= (byte & 0xFF) << (8 * index)
  end
  value
end