Class: AMQ::Protocol::TableValueDecoder

Inherits:
Object
  • Object
show all
Includes:
TypeConstants
Defined in:
lib/amq/protocol/table_value_decoder.rb

Constant Summary collapse

PACK_UINT32_BE =

Pack format strings use explicit endianness (available as of Ruby 1.9.3)

'N'.freeze
PACK_INT64_BE =
'q>'.freeze
PACK_INT16_BE =
's>'.freeze
PACK_UINT64_BE =
'Q>'.freeze
PACK_FLOAT32 =

single precision float (native endian, matches encoder)

'f'.freeze
PACK_FLOAT64 =

big-endian double precision float

'G'.freeze
PACK_UCHAR_UINT32 =
'CN'.freeze

Constants included from TypeConstants

AMQ::Protocol::TypeConstants::BOOLEAN_FALSE, AMQ::Protocol::TypeConstants::BOOLEAN_TRUE, AMQ::Protocol::TypeConstants::TEN, AMQ::Protocol::TypeConstants::TYPE_32BIT_FLOAT, AMQ::Protocol::TypeConstants::TYPE_64BIT_FLOAT, AMQ::Protocol::TypeConstants::TYPE_ARRAY, AMQ::Protocol::TypeConstants::TYPE_BOOLEAN, AMQ::Protocol::TypeConstants::TYPE_BYTE, AMQ::Protocol::TypeConstants::TYPE_BYTE_ARRAY, AMQ::Protocol::TypeConstants::TYPE_DECIMAL, AMQ::Protocol::TypeConstants::TYPE_HASH, AMQ::Protocol::TypeConstants::TYPE_INTEGER, AMQ::Protocol::TypeConstants::TYPE_SIGNED_16BIT, AMQ::Protocol::TypeConstants::TYPE_SIGNED_64BIT, AMQ::Protocol::TypeConstants::TYPE_STRING, AMQ::Protocol::TypeConstants::TYPE_TIME, AMQ::Protocol::TypeConstants::TYPE_VOID

Class Method Summary collapse

Class Method Details

.decode_32bit_float(data, offset) ⇒ Object



147
148
149
150
151
152
# File 'lib/amq/protocol/table_value_decoder.rb', line 147

def self.decode_32bit_float(data, offset)
  v = data.byteslice(offset, 4).unpack1(PACK_FLOAT32)
  offset += 4

  [v, offset]
end

.decode_64bit_float(data, offset) ⇒ Object



155
156
157
158
159
160
# File 'lib/amq/protocol/table_value_decoder.rb', line 155

def self.decode_64bit_float(data, offset)
  v = data.byteslice(offset, 8).unpack1(PACK_FLOAT64)
  offset += 8

  [v, offset]
end

.decode_array(data, initial_offset) ⇒ Object

API



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/amq/protocol/table_value_decoder.rb', line 31

def self.decode_array(data, initial_offset)
  array_length = data.byteslice(initial_offset, 4).unpack1(PACK_UINT32_BE)

  ary    = []
  offset = initial_offset + 4

  while offset <= (initial_offset + array_length)
    type, offset = decode_value_type(data, offset)

    i = case type
        when TYPE_STRING
          v, offset = decode_string(data, offset)
          v
        when TYPE_BYTE_ARRAY
          # Ruby doesn't have a direct counterpart to
          # ByteBuffer or byte[], so using a string feels
          # more appropriate than an array of fixnums
          v, offset = decode_string(data, offset)
          v
        when TYPE_INTEGER
          v, offset = decode_integer(data, offset)
          v
        when TYPE_DECIMAL
          v, offset = decode_big_decimal(data, offset)
          v
        when TYPE_TIME
          v, offset = decode_time(data, offset)
          v
        when TYPE_HASH
          v, offset = decode_hash(data, offset)
          v
        when TYPE_BOOLEAN
          v, offset = decode_boolean(data, offset)
          v
        when TYPE_BYTE
          v, offset = decode_byte(data, offset)
          v
        when TYPE_SIGNED_16BIT
          v, offset = decode_short(data, offset)
          v
        when TYPE_SIGNED_64BIT
          v, offset = decode_long(data, offset)
          v
        when TYPE_32BIT_FLOAT
          v, offset = decode_32bit_float(data, offset)
          v
        when TYPE_64BIT_FLOAT
          v, offset = decode_64bit_float(data, offset)
          v
        when TYPE_VOID
          nil
        when TYPE_ARRAY
          v, offset = decode_array(data, offset)
          v
        else
          raise ArgumentError.new("unsupported type in a table value: #{type.inspect}, do not know how to decode!")
        end

    ary << i
  end


  [ary, initial_offset + array_length + 4]
end

.decode_big_decimal(data, offset) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/amq/protocol/table_value_decoder.rb', line 122

def self.decode_big_decimal(data, offset)
  decimals, raw = data.byteslice(offset, 5).unpack(PACK_UCHAR_UINT32)
  offset += 5
  v = BigDecimal(raw.to_s) * (BigDecimal(TEN) ** -decimals)

  [v, offset]
end

.decode_boolean(data, offset) ⇒ Object



140
141
142
143
144
# File 'lib/amq/protocol/table_value_decoder.rb', line 140

def self.decode_boolean(data, offset)
  byte = data.getbyte(offset)
  offset += 1
  [(byte == 1), offset]
end

.decode_byte(data, offset) ⇒ Array

Decodes/Converts a byte value from the data at the provided offset.

Parameters:

    • Binary data string

    • The offset from which to read the byte

Returns:

    • The Integer value and new offset pair



182
183
184
# File 'lib/amq/protocol/table_value_decoder.rb', line 182

def self.decode_byte(data, offset)
  [data.getbyte(offset), offset + 1]
end

.decode_hash(data, offset) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/amq/protocol/table_value_decoder.rb', line 168

def self.decode_hash(data, offset)
  length = data.byteslice(offset, 4).unpack1(PACK_UINT32_BE)
  v = Table.decode(data.byteslice(offset, length + 4))
  offset += 4 + length

  [v, offset]
end

.decode_integer(data, offset) ⇒ Object



107
108
109
110
111
112
# File 'lib/amq/protocol/table_value_decoder.rb', line 107

def self.decode_integer(data, offset)
  v = data.byteslice(offset, 4).unpack1(PACK_UINT32_BE)
  offset += 4

  [v, offset]
end

.decode_long(data, offset) ⇒ Object



115
116
117
118
119
# File 'lib/amq/protocol/table_value_decoder.rb', line 115

def self.decode_long(data, offset)
  v = data.byteslice(offset, 8).unpack1(PACK_INT64_BE)
  offset += 8
  [v, offset]
end

.decode_short(data, offset) ⇒ Object



187
188
189
190
191
# File 'lib/amq/protocol/table_value_decoder.rb', line 187

def self.decode_short(data, offset)
  v = data.byteslice(offset, 2).unpack1(PACK_INT16_BE)
  offset += 2
  [v, offset]
end

.decode_string(data, offset) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/amq/protocol/table_value_decoder.rb', line 97

def self.decode_string(data, offset)
  length = data.byteslice(offset, 4).unpack1(PACK_UINT32_BE)
  offset += 4
  v = data.byteslice(offset, length)
  offset += length

  [v, offset]
end

.decode_time(data, offset) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/amq/protocol/table_value_decoder.rb', line 131

def self.decode_time(data, offset)
  timestamp = data.byteslice(offset, 8).unpack1(PACK_UINT64_BE)
  v = Time.at(timestamp)
  offset += 8

  [v, offset]
end

.decode_value_type(data, offset) ⇒ Object



163
164
165
# File 'lib/amq/protocol/table_value_decoder.rb', line 163

def self.decode_value_type(data, offset)
  [data.byteslice(offset, 1), offset + 1]
end