Class: Protobuf::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/decoder.rb

Class Method Summary collapse

Class Method Details

.decode_each_field(stream) ⇒ Object

Read bytes from +stream+ and pass to +message+ object.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protobuf/decoder.rb', line 5

def self.decode_each_field(stream)
  until stream.eof?
    bits = Varint.decode(stream)
    wire_type = bits & 0x07
    tag = bits >> 3

    bytes = if wire_type == ::Protobuf::WireType::VARINT
              Varint.decode(stream)
            elsif wire_type == ::Protobuf::WireType::LENGTH_DELIMITED
              value_length = Varint.decode(stream)
              stream.read(value_length)
            elsif wire_type == ::Protobuf::WireType::FIXED64
              stream.read(8)
            elsif wire_type == ::Protobuf::WireType::FIXED32
              stream.read(4)
            else
              fail InvalidWireType, wire_type
            end

    yield(tag, bytes)
  end
end