Class: Protobuf::Decoder
- Inherits:
-
Object
- Object
- Protobuf::Decoder
- Defined in:
- lib/protobuf/decoder.rb
Class Method Summary collapse
-
.decode_each_field(stream, &block) ⇒ Object
Read bytes from +stream+ and pass to +message+ object.
- .read_field(stream) ⇒ Object
-
.read_fixed32(stream) ⇒ Object
Read 32-bit string value from +stream+.
-
.read_fixed64(stream) ⇒ Object
Read 64-bit string value from +stream+.
-
.read_key(stream) ⇒ Object
Read key pair (tag and wire-type) from +stream+.
-
.read_length_delimited(stream) ⇒ Object
Read length-delimited string value from +stream+.
-
.read_varint(stream) ⇒ Object
Read varint integer value from +stream+.
Class Method Details
.decode_each_field(stream, &block) ⇒ Object
Read bytes from +stream+ and pass to +message+ object.
8 9 10 11 12 13 |
# File 'lib/protobuf/decoder.rb', line 8 def self.decode_each_field(stream, &block) until stream.eof? tag, bytes = read_field(stream) block.call(tag, bytes) end end |
.read_field(stream) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/protobuf/decoder.rb', line 15 def self.read_field(stream) tag, wire_type = read_key(stream) bytes = case wire_type when ::Protobuf::WireType::VARINT then read_varint(stream) when ::Protobuf::WireType::FIXED64 then read_fixed64(stream) when ::Protobuf::WireType::LENGTH_DELIMITED then read_length_delimited(stream) when ::Protobuf::WireType::FIXED32 then read_fixed32(stream) when ::Protobuf::WireType::START_GROUP then raise NotImplementedError, 'Group is deprecated.' when ::Protobuf::WireType::END_GROUP then raise NotImplementedError, 'Group is deprecated.' else raise InvalidWireType, wire_type end return tag, bytes end |
.read_fixed32(stream) ⇒ Object
Read 32-bit string value from +stream+.
38 39 40 |
# File 'lib/protobuf/decoder.rb', line 38 def self.read_fixed32(stream) stream.read(4) end |
.read_fixed64(stream) ⇒ Object
Read 64-bit string value from +stream+.
43 44 45 |
# File 'lib/protobuf/decoder.rb', line 43 def self.read_fixed64(stream) stream.read(8) end |
.read_key(stream) ⇒ Object
Read key pair (tag and wire-type) from +stream+.
48 49 50 51 52 53 |
# File 'lib/protobuf/decoder.rb', line 48 def self.read_key(stream) bits = read_varint(stream) wire_type = bits & 0x07 tag = bits >> 3 [tag, wire_type] end |
.read_length_delimited(stream) ⇒ Object
Read length-delimited string value from +stream+.
56 57 58 59 |
# File 'lib/protobuf/decoder.rb', line 56 def self.read_length_delimited(stream) value_length = read_varint(stream) stream.read(value_length) end |
.read_varint(stream) ⇒ Object
Read varint integer value from +stream+.
62 63 64 65 66 67 68 69 70 |
# File 'lib/protobuf/decoder.rb', line 62 def self.read_varint(stream) value = index = 0 begin byte = stream.readbyte value |= (byte & 0x7f) << (7 * index) index += 1 end while (byte & 0x80).nonzero? value end |