class Thrift::CompactProtocol
Constants
- BYTE_MAX
- BYTE_MIN
- EVERYTHING_ELSE_MASK
- I16_MAX
- I16_MIN
- I32_MAX
- I32_MIN
- I64_MAX
- I64_MIN
- MAX_VARINT32_BYTES
- MAX_VARINT32_LAST_BYTE
- MAX_VARINT_BYTES
- PROTOCOL_ID
- SEVEN_BIT_MASK
- TSTOP
- TYPE_BITS
- TYPE_MASK
- TYPE_SHIFT_AMOUNT
- VARINT32_PREFIX_BYTES
- VERSION
- VERSION_MASK
Attributes
Public Class Methods
Source
# File lib/thrift/protocol/compact_protocol.rb 122 def initialize(transport, max_string_size: DEFAULT_MAX_STRING_SIZE) 123 BaseProtocol.validate_max_string_size(max_string_size) 124 super(transport) 125 @max_string_size = max_string_size 126 @reset_message_size = transport.message_boundaries? 127 128 @last_field = [0] 129 @boolean_value = nil 130 131 # Pre-allocated read buffer for read_double(). 132 @rbuf = Bytes.empty_byte_buffer(8) 133 end
Calls superclass method
Thrift::BaseProtocol::new
Public Instance Methods
Source
# File lib/thrift/protocol/compact_protocol.rb 426 def read_binary 427 size = read_varint32 428 if size > I32_MAX 429 raise ProtocolException.new(ProtocolException::SIZE_LIMIT, "Binary size limit exceeded") 430 end 431 check_string_size(size) 432 trans.read_all(size) 433 end
Source
# File lib/thrift/protocol/compact_protocol.rb 385 def read_bool 386 unless @bool_value.nil? 387 bv = @bool_value 388 @bool_value = nil 389 bv 390 else 391 read_byte == CompactTypes::BOOLEAN_TRUE 392 end 393 end
Source
# File lib/thrift/protocol/compact_protocol.rb 395 def read_byte 396 val = trans.read_byte 397 if (val > 0x7f) 398 val = 0 - ((val - 1) ^ 0xff) 399 end 400 val 401 end
Source
# File lib/thrift/protocol/compact_protocol.rb 416 def read_double 417 trans.read_into_buffer(@rbuf, 8) 418 @rbuf.reverse.unpack1("G") 419 end
Source
# File lib/thrift/protocol/compact_protocol.rb 331 def read_field_begin 332 type = read_byte 333 334 # if it's a stop, then we can return immediately, as the struct is over. 335 if (type & 0x0f) == Types::STOP 336 TSTOP 337 else 338 field_id = nil 339 340 # mask off the 4 MSB of the type header. it could contain a field id delta. 341 modifier = (type & 0xf0) >> 4 342 if modifier == 0 343 # not a delta. look ahead for the zigzag varint field id. 344 @last_field.pop 345 field_id = read_i16 346 else 347 # has a delta. add the delta to the last read field id. 348 field_id = @last_field.pop + modifier 349 end 350 351 # if this happens to be a boolean field, the value is encoded in the type 352 if CompactTypes.is_bool_type?(type) 353 # save the boolean value in a special instance variable. 354 @bool_value = (type & 0x0f) == CompactTypes::BOOLEAN_TRUE 355 end 356 357 # push the new field onto the field stack so we can keep the deltas going. 358 @last_field.push(field_id) 359 [nil, CompactTypes.get_ttype(type & 0x0f), field_id] 360 end 361 end
Source
# File lib/thrift/protocol/compact_protocol.rb 403 def read_i16 404 value = zig_zag_to_int(read_varint32) & 0xffff 405 value > I16_MAX ? value - 0x10000 : value 406 end
Source
# File lib/thrift/protocol/compact_protocol.rb 408 def read_i32 409 zig_zag_to_int(read_varint32) 410 end
Source
# File lib/thrift/protocol/compact_protocol.rb 412 def read_i64 413 zig_zag_to_long(read_varint64 & 0xffffffffffffffff) 414 end
Source
# File lib/thrift/protocol/compact_protocol.rb 370 def read_list_begin 371 size_and_type = read_byte 372 size = (size_and_type >> 4) & 0x0f 373 if size == 15 374 size = read_varint32 375 end 376 validate_container_size(size) 377 type = CompactTypes.get_ttype(size_and_type) 378 [type, size] 379 end
Source
# File lib/thrift/protocol/compact_protocol.rb 363 def read_map_begin 364 size = read_varint32 365 validate_container_size(size) 366 key_and_value_type = size == 0 ? 0 : read_byte 367 [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size] 368 end
Source
# File lib/thrift/protocol/compact_protocol.rb 295 def read_message_begin 296 trans.reset_message_size if @reset_message_size 297 298 protocol_id = read_byte 299 if protocol_id != PROTOCOL_ID 300 raise ProtocolException.new( 301 ProtocolException::BAD_VERSION, 302 "Expected protocol id #{PROTOCOL_ID} but got #{protocol_id}", 303 ) 304 end 305 306 version_and_type = read_byte 307 version = version_and_type & VERSION_MASK 308 if (version != VERSION) 309 raise ProtocolException.new( 310 ProtocolException::BAD_VERSION, 311 "Expected version #{VERSION} but got #{version}", 312 ) 313 end 314 315 type = (version_and_type >> TYPE_SHIFT_AMOUNT) & TYPE_BITS 316 seqid = message_seqid_from_varint32(read_varint32) 317 messageName = read_string 318 [messageName, type, seqid] 319 end
Source
# File lib/thrift/protocol/compact_protocol.rb 381 def read_set_begin 382 read_list_begin 383 end
Source
# File lib/thrift/protocol/compact_protocol.rb 421 def read_string 422 buffer = read_binary 423 Bytes.convert_to_string(buffer) 424 end
Source
# File lib/thrift/protocol/compact_protocol.rb 321 def read_struct_begin 322 @last_field.push(0) 323 "" 324 end
Source
# File lib/thrift/protocol/compact_protocol.rb 326 def read_struct_end 327 @last_field.pop 328 nil 329 end
Source
# File lib/thrift/protocol/compact_protocol.rb 439 def read_uuid 440 UUID.uuid_from_bytes(trans.read_all(16)) 441 end
Source
# File lib/thrift/protocol/compact_protocol.rb 435 def skip_string 436 read_binary 437 end
Source
# File lib/thrift/protocol/compact_protocol.rb 443 def to_s 444 "compact(#{super})" 445 end
Source
# File lib/thrift/protocol/compact_protocol.rb 284 def write_binary(buf) 285 size = validate_size(buf.bytesize) 286 write_varint32(size) 287 @trans.write(buf) 288 end
Source
# File lib/thrift/protocol/compact_protocol.rb 222 def write_bool(bool) 223 # Preserve Ruby truthiness: only false and nil encode as false. 224 type = bool ? CompactTypes::BOOLEAN_TRUE : CompactTypes::BOOLEAN_FALSE 225 unless @boolean_field.nil? 226 # we haven't written the field header yet 227 write_field_begin_internal(@boolean_field.first, @boolean_field.last, type) 228 @boolean_field = nil 229 else 230 # we're not part of a field, so just write the value. 231 write_byte_direct(type) 232 end 233 end
Source
# File lib/thrift/protocol/compact_protocol.rb 235 def write_byte(byte) 236 unless byte.is_a?(Integer) 237 raise "nil argument not allowed!" if byte.nil? 238 raise ::TypeError, "integer argument expected" 239 end 240 raise RangeError if byte < BYTE_MIN || byte > BYTE_MAX 241 242 write_byte_direct(byte) 243 end
Source
# File lib/thrift/protocol/compact_protocol.rb 275 def write_double(dub) 276 @trans.write([dub].pack("G").reverse) 277 end
Source
# File lib/thrift/protocol/compact_protocol.rb 159 def write_field_begin(name, type, id) 160 unless id.is_a?(Integer) 161 raise "nil argument not allowed!" if id.nil? 162 raise ::TypeError, "integer argument expected" 163 end 164 raise RangeError if id < I16_MIN || id > I16_MAX 165 166 if type == Types::BOOL 167 # we want to possibly include the value, so we'll wait. 168 @boolean_field = [type, id] 169 else 170 write_field_begin_internal(type, id) 171 end 172 nil 173 end
Source
# File lib/thrift/protocol/compact_protocol.rb 180 def write_field_begin_internal(type, id, type_override = nil) 181 last_id = @last_field.pop 182 183 # if there's a type override, use that. 184 type_to_write = type_override || CompactTypes.get_compact_type(type) 185 186 # check if we can use delta encoding for the field id 187 if id > last_id && id - last_id <= 15 188 # write them together 189 write_byte_direct(((id - last_id) << 4) | type_to_write) 190 else 191 # write them separate 192 write_byte_direct(type_to_write) 193 write_varint32(int_to_zig_zag(id)) 194 end 195 196 @last_field.push(id) 197 nil 198 end
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.
Source
# File lib/thrift/protocol/compact_protocol.rb 200 def write_field_stop 201 write_byte_direct(Types::STOP) 202 end
Source
# File lib/thrift/protocol/compact_protocol.rb 245 def write_i16(i16) 246 unless i16.is_a?(Integer) 247 raise "nil argument not allowed!" if i16.nil? 248 raise ::TypeError, "integer argument expected" 249 end 250 raise RangeError if i16 < I16_MIN || i16 > I16_MAX 251 252 write_varint32(int_to_zig_zag(i16)) 253 end
Source
# File lib/thrift/protocol/compact_protocol.rb 255 def write_i32(i32) 256 unless i32.is_a?(Integer) 257 raise "nil argument not allowed!" if i32.nil? 258 raise ::TypeError, "integer argument expected" 259 end 260 raise RangeError if i32 < I32_MIN || i32 > I32_MAX 261 262 write_varint32(int_to_zig_zag(i32)) 263 end
Source
# File lib/thrift/protocol/compact_protocol.rb 265 def write_i64(i64) 266 unless i64.is_a?(Integer) 267 raise "nil argument not allowed!" if i64.nil? 268 raise ::TypeError, "integer argument expected" 269 end 270 raise RangeError if i64 < I64_MIN || i64 > I64_MAX 271 272 write_varint64(long_to_zig_zag(i64)) 273 end
Source
# File lib/thrift/protocol/compact_protocol.rb 214 def write_list_begin(etype, size) 215 write_collection_begin(etype, size) 216 end
Source
# File lib/thrift/protocol/compact_protocol.rb 204 def write_map_begin(ktype, vtype, size) 205 size = validate_size(size) 206 if (size == 0) 207 write_byte_direct(0) 208 else 209 write_varint32(size) 210 write_byte_direct((CompactTypes.get_compact_type(ktype) << 4) | CompactTypes.get_compact_type(vtype)) 211 end 212 end
Source
# File lib/thrift/protocol/compact_protocol.rb 135 def write_message_begin(name, type, seqid) 136 unless seqid.is_a?(Integer) 137 raise "nil argument not allowed!" if seqid.nil? 138 raise ::TypeError, "integer argument expected" 139 end 140 raise RangeError if seqid < I32_MIN || seqid > I32_MAX 141 142 write_byte_direct(PROTOCOL_ID) 143 write_byte_direct((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK)) 144 write_varint32(message_seqid_to_varint32(seqid)) 145 write_string(name) 146 nil 147 end
Source
# File lib/thrift/protocol/compact_protocol.rb 218 def write_set_begin(etype, size) 219 write_collection_begin(etype, size); 220 end
Source
# File lib/thrift/protocol/compact_protocol.rb 279 def write_string(str) 280 buf = Bytes.convert_to_utf8_byte_buffer(str) 281 write_binary(buf) 282 end
Source
# File lib/thrift/protocol/compact_protocol.rb 149 def write_struct_begin(name) 150 @last_field.push(0) 151 nil 152 end
Source
# File lib/thrift/protocol/compact_protocol.rb 154 def write_struct_end 155 @last_field.pop 156 nil 157 end
Source
# File lib/thrift/protocol/compact_protocol.rb 290 def write_uuid(uuid) 291 UUID.validate_uuid!(uuid) 292 trans.write(UUID.uuid_bytes(uuid)) 293 end
Private Instance Methods
Source
# File lib/thrift/protocol/compact_protocol.rb 534 def int_to_zig_zag(n) 535 (n << 1) ^ (n >> 31) 536 end
Source
# File lib/thrift/protocol/compact_protocol.rb 538 def long_to_zig_zag(l) 539 # puts "zz encoded #{l} to #{(l << 1) ^ (l >> 63)}" 540 (l << 1) ^ (l >> 63) 541 end
Source
# File lib/thrift/protocol/compact_protocol.rb 565 def message_seqid_from_varint32(seqid) 566 seqid > 0x7fffffff ? seqid - (2**32) : seqid 567 end
Source
# File lib/thrift/protocol/compact_protocol.rb 551 def message_seqid_to_varint32(seqid) 552 seqid < 0 ? seqid + (2**32) : seqid 553 end
Source
# File lib/thrift/protocol/compact_protocol.rb 501 def read_varint32 502 shift = 0 503 result = 0 504 VARINT32_PREFIX_BYTES.times do 505 b = read_byte 506 result |= (b & 0x7f) << shift 507 return result if (b & 0x80) != 0x80 508 shift += 7 509 end 510 511 b = read_byte 512 if (b & 0x80) != 0 513 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Variable-length int over 5 bytes.") 514 end 515 if (b & ~MAX_VARINT32_LAST_BYTE) != 0 516 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Variable-length int overflows uint32.") 517 end 518 519 result | (b << shift) 520 end
Source
# File lib/thrift/protocol/compact_protocol.rb 522 def read_varint64 523 shift = 0 524 result = 0 525 MAX_VARINT_BYTES.times do 526 b = read_byte 527 result |= (b & 0x7f) << shift 528 return result if (b & 0x80) != 0x80 529 shift += 7 530 end 531 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.") 532 end
Source
# File lib/thrift/protocol/compact_protocol.rb 555 def validate_size(size) 556 unless size.is_a?(Integer) 557 raise "nil argument not allowed!" if size.nil? 558 raise ::TypeError, "integer argument expected" 559 end 560 raise RangeError if size < 0 || size > I32_MAX 561 562 size 563 end
Source
# File lib/thrift/protocol/compact_protocol.rb 464 def write_byte_direct(byte) 465 @trans.write([byte].pack("C")) 466 end
Source
# File lib/thrift/protocol/compact_protocol.rb 453 def write_collection_begin(elem_type, size) 454 size = validate_size(size) 455 compact_type = CompactTypes.get_compact_type(elem_type) 456 if size <= 14 457 write_byte_direct((size << 4) | compact_type) 458 else 459 write_byte_direct(0xf0 | compact_type) 460 write_varint32(size) 461 end 462 end
Abstract method for writing the start of lists and sets. List and sets on the wire differ only by the type indicator.
Source
# File lib/thrift/protocol/compact_protocol.rb 468 def write_varint32(n) 469 if (n & ~0x7F) == 0 470 write_byte_direct(n) 471 return 472 end 473 474 buffer = String.new(capacity: MAX_VARINT32_BYTES, encoding: Encoding::BINARY) 475 while (n & ~0x7F) != 0 476 buffer << ((n & 0x7F) | 0x80) 477 n >>= 7 478 end 479 buffer << n 480 @trans.write(buffer) 481 end
Source
# File lib/thrift/protocol/compact_protocol.rb 486 def write_varint64(n) 487 if (n & EVERYTHING_ELSE_MASK) == 0 488 write_byte_direct(n) 489 return 490 end 491 492 buffer = String.new(capacity: MAX_VARINT_BYTES, encoding: Encoding::BINARY) 493 while (n & EVERYTHING_ELSE_MASK) != 0 494 buffer << ((n & SEVEN_BIT_MASK) | 0x80) 495 n >>= 7 496 end 497 buffer << n 498 @trans.write(buffer) 499 end
Source
# File lib/thrift/protocol/compact_protocol.rb 543 def zig_zag_to_int(n) 544 (n >> 1) ^ -(n & 1) 545 end
Source
# File lib/thrift/protocol/compact_protocol.rb 547 def zig_zag_to_long(n) 548 (n >> 1) ^ -(n & 1) 549 end