class Thrift::CompactProtocol
Constants
- EVERYTHING_ELSE_MASK
- PROTOCOL_ID
- SEVEN_BIT_MASK
- TSTOP
- TYPE_MASK
- TYPE_SHIFT_AMOUNT
- VERSION
- VERSION_MASK
Public Class Methods
new(transport)
click to toggle source
Calls superclass method
Thrift::BaseProtocol.new
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 96 def initialize(transport) super(transport) @last_field = [0] @boolean_value = nil end
Public Instance Methods
read_bool()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 294 def read_bool unless @bool_value.nil? bv = @bool_value @bool_value = nil bv else read_byte() == CompactTypes::BOOLEAN_TRUE end end
read_byte()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 304 def read_byte dat = trans.read_all(1) val = dat[0] if (val > 0x7f) val = 0 - ((val - 1) ^ 0xff) end val end
read_double()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 325 def read_double dat = trans.read_all(8) val = dat.reverse.unpack('G').first val end
read_field_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 242 def read_field_begin type = read_byte() # if it's a stop, then we can return immediately, as the struct is over. if (type & 0x0f) == Types::STOP TSTOP else field_id = nil # mask off the 4 MSB of the type header. it could contain a field id delta. modifier = (type & 0xf0) >> 4 if modifier == 0 # not a delta. look ahead for the zigzag varint field id. @last_field.pop field_id = read_i16() else # has a delta. add the delta to the last read field id. field_id = @last_field.pop + modifier end # if this happens to be a boolean field, the value is encoded in the type if CompactTypes.is_bool_type?(type) # save the boolean value in a special instance variable. @bool_value = (type & 0x0f) == CompactTypes::BOOLEAN_TRUE end # push the new field onto the field stack so we can keep the deltas going. @last_field.push(field_id) ["", CompactTypes.get_ttype(type & 0x0f), field_id] end end
read_i16()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 313 def read_i16 zig_zag_to_int(read_varint32()) end
read_i32()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 317 def read_i32 zig_zag_to_int(read_varint32()) end
read_i64()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 321 def read_i64 zig_zag_to_long(read_varint64()) end
read_list_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 280 def read_list_begin size_and_type = read_byte() size = (size_and_type >> 4) & 0x0f if size == 15 size = read_varint32() end type = CompactTypes.get_ttype(size_and_type) [type, size] end
read_map_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 274 def read_map_begin size = read_varint32() key_and_value_type = size == 0 ? 0 : read_byte() [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size] end
read_message_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 214 def read_message_begin protocol_id = read_byte() if protocol_id != PROTOCOL_ID raise ProtocolException.new("Expected protocol id #{PROTOCOL_ID} but got #{protocol_id}") end version_and_type = read_byte() version = version_and_type & VERSION_MASK if (version != VERSION) raise ProtocolException.new("Expected version #{VERSION} but got #{version}"); end type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03 seqid = read_varint32() messageName = read_string() [messageName, type, seqid] end
read_set_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 290 def read_set_begin read_list_begin end
read_string()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 331 def read_string size = read_varint32() trans.read_all(size) end
read_struct_begin()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 232 def read_struct_begin @last_field.push(0) "" end
read_struct_end()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 237 def read_struct_end @last_field.pop() nil end
write_bool(bool)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 177 def write_bool(bool) type = bool ? CompactTypes::BOOLEAN_TRUE : CompactTypes::BOOLEAN_FALSE unless @boolean_field.nil? # we haven't written the field header yet write_field_begin_internal(@boolean_field.first, @boolean_field.last, type) @boolean_field = nil else # we're not part of a field, so just write the value. write_byte(type) end end
write_byte(byte)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 189 def write_byte(byte) @trans.write([byte].pack('c')) end
write_double(dub)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 205 def write_double(dub) @trans.write([dub].pack("G").reverse) end
write_field_begin(name, type, id)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 121 def write_field_begin(name, type, id) if type == Types::BOOL # we want to possibly include the value, so we'll wait. @boolean_field = [type, id] else write_field_begin_internal(type, id) end nil end
write_field_begin_internal(type, id, type_override=nil)
click to toggle source
The workhorse of writeFieldBegin. It has the option of doing a 'type override' of the type header. This is used specifically in the boolean field case.
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 136 def write_field_begin_internal(type, id, type_override=nil) last_id = @last_field.pop # if there's a type override, use that. typeToWrite = type_override || CompactTypes.get_compact_type(type) # check if we can use delta encoding for the field id if id > last_id && id - last_id <= 15 # write them together write_byte((id - last_id) << 4 | typeToWrite) else # write them separate write_byte(typeToWrite) write_i16(id) end @last_field.push(id) nil end
write_field_stop()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 156 def write_field_stop write_byte(Types::STOP) end
write_i16(i16)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 193 def write_i16(i16) write_varint32(int_to_zig_zag(i16)) end
write_i32(i32)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 197 def write_i32(i32) write_varint32(int_to_zig_zag(i32)) end
write_i64(i64)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 201 def write_i64(i64) write_varint64(long_to_zig_zag(i64)) end
write_list_begin(etype, size)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 169 def write_list_begin(etype, size) write_collection_begin(etype, size) end
write_map_begin(ktype, vtype, size)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 160 def write_map_begin(ktype, vtype, size) if (size == 0) write_byte(0) else write_varint32(size) write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype)) end end
write_message_begin(name, type, seqid)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 103 def write_message_begin(name, type, seqid) write_byte(PROTOCOL_ID) write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK)) write_varint32(seqid) write_string(name) nil end
write_set_begin(etype, size)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 173 def write_set_begin(etype, size) write_collection_begin(etype, size); end
write_string(str)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 209 def write_string(str) write_varint32(str.length) @trans.write(str) end
write_struct_begin(name)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 111 def write_struct_begin(name) @last_field.push(0) nil end
write_struct_end()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 116 def write_struct_end @last_field.pop nil end
Private Instance Methods
int_to_zig_zag(n)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 400 def int_to_zig_zag(n) (n << 1) ^ (n >> 31) end
long_to_zig_zag(l)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 404 def long_to_zig_zag(l) # puts "zz encoded #{l} to #{(l << 1) ^ (l >> 63)}" (l << 1) ^ (l >> 63) end
read_varint32()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 384 def read_varint32() read_varint64() end
read_varint64()
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 388 def read_varint64() shift = 0 result = 0 while true b = read_byte() result |= (b & 0x7f) << shift break if (b & 0x80) != 0x80 shift += 7 end result end
write_collection_begin(elem_type, size)
click to toggle source
Abstract method for writing the start of lists and sets. List and sets on the wire differ only by the type indicator.
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 343 def write_collection_begin(elem_type, size) if size <= 14 write_byte(size << 4 | CompactTypes.get_compact_type(elem_type)) else write_byte(0xf0 | CompactTypes.get_compact_type(elem_type)) write_varint32(size) end end
write_varint32(n)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 352 def write_varint32(n) # int idx = 0; while true if (n & ~0x7F) == 0 # i32buf[idx++] = (byte)n; write_byte(n) break # return; else # i32buf[idx++] = (byte)((n & 0x7F) | 0x80); write_byte((n & 0x7F) | 0x80) n = n >> 7 end end # trans_.write(i32buf, 0, idx); end
write_varint64(n)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 372 def write_varint64(n) while true if (n & EVERYTHING_ELSE_MASK) == 0 #TODO need to find a way to make this into a long... write_byte(n) break else write_byte((n & SEVEN_BIT_MASK) | 0x80) n >>= 7 end end end
zig_zag_to_int(n)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 409 def zig_zag_to_int(n) (n >> 1) ^ -(n & 1) end
zig_zag_to_long(n)
click to toggle source
# File build/evernote-mode-FpyAOB/evernote-mode-0.41/ruby/thrift/protocol/compact_protocol.rb, line 413 def zig_zag_to_long(n) (n >> 1) ^ -(n & 1) end