Class: AMQ::Protocol::Table
- Inherits:
-
Object
- Object
- AMQ::Protocol::Table
- Includes:
- TypeConstants
- Defined in:
- lib/amq/protocol/table.rb
Defined Under Namespace
Classes: InvalidTableError
Constant Summary
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
- .decode(data) ⇒ Object
- .decode_table_key(data, offset) ⇒ Object
- .encode(table) ⇒ Object
- .hash_size(value) ⇒ Object
- .length(data) ⇒ Object
Class Method Details
.decode(data) ⇒ Object
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/amq/protocol/table.rb', line 54 def self.decode(data) table = Hash.new table_length = data.unpack(PACK_UINT32).first return table if table_length.zero? offset = 4 while offset <= table_length key, offset = decode_table_key(data, offset) type, offset = TableValueDecoder.decode_value_type(data, offset) table[key] = case type when TYPE_STRING v, offset = TableValueDecoder.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 = TableValueDecoder.decode_string(data, offset) v when TYPE_INTEGER v, offset = TableValueDecoder.decode_integer(data, offset) v when TYPE_DECIMAL v, offset = TableValueDecoder.decode_big_decimal(data, offset) v when TYPE_TIME v, offset = TableValueDecoder.decode_time(data, offset) v when TYPE_HASH v, offset = TableValueDecoder.decode_hash(data, offset) v when TYPE_BOOLEAN v, offset = TableValueDecoder.decode_boolean(data, offset) v when TYPE_BYTE then v, offset = TableValueDecoder.decode_byte(data, offset) v when TYPE_SIGNED_16BIT then v, offset = TableValueDecoder.decode_short(data, offset) v when TYPE_SIGNED_64BIT then v, offset = TableValueDecoder.decode_long(data, offset) v when TYPE_32BIT_FLOAT then v, offset = TableValueDecoder.decode_32bit_float(data, offset) v when TYPE_64BIT_FLOAT then v, offset = TableValueDecoder.decode_64bit_float(data, offset) v when TYPE_VOID nil when TYPE_ARRAY v, offset = TableValueDecoder.decode_array(data, offset) v else raise ArgumentError, "Not a valid type: #{type.inspect}\nData: #{data.inspect}\nUnprocessed data: #{data[offset..-1].inspect}\nOffset: #{offset}\nTotal size: #{table_length}\nProcessed data: #{table.inspect}" end end table end |
.decode_table_key(data, offset) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/amq/protocol/table.rb', line 135 def self.decode_table_key(data, offset) key_length = data.slice(offset, 1).unpack(PACK_CHAR).first offset += 1 key = data.slice(offset, key_length) offset += key_length [key, offset] end |
.encode(table) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/amq/protocol/table.rb', line 30 def self.encode(table) buffer = String.new table ||= {} table.each do |key, value| key = key.to_s # it can be a symbol as well buffer << key.bytesize.chr + key case value when Hash then buffer << TYPE_HASH buffer << self.encode(value) else buffer << TableValueEncoder.encode(value).force_encoding(buffer.encoding) end end [buffer.bytesize].pack(PACK_UINT32).force_encoding(buffer.encoding) + buffer end |
.hash_size(value) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/amq/protocol/table.rb', line 124 def self.hash_size(value) acc = 0 value.each do |k, v| acc += (1 + k.to_s.bytesize) acc += TableValueEncoder.field_value_size(v) end acc end |
.length(data) ⇒ Object
119 120 121 |
# File 'lib/amq/protocol/table.rb', line 119 def self.length(data) data.unpack(PACK_UINT32).first end |