Class: Protobuf::Decoder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream = nil, message = nil) ⇒ Decoder

Returns a new instance of Decoder.



13
14
15
# File 'lib/protobuf/message/decoder.rb', line 13

def initialize(stream=nil, message=nil)
  @stream, @message = stream, message
end

Class Method Details

.decode(stream, message) ⇒ Object



8
9
10
# File 'lib/protobuf/message/decoder.rb', line 8

def decode(stream, message)
  self.new(stream, message).decode
end

Instance Method Details

#decode(stream = @stream, message = @message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/protobuf/message/decoder.rb', line 17

def decode(stream=@stream, message=@message)
  until stream.eof?
    tag, wire_type = read_key stream
    bytes =
      case wire_type
      when WireType::VARINT
        read_varint stream
      when WireType::FIXED64
        read_fixed64 stream
      when WireType::LENGTH_DELIMITED
        read_length_delimited stream
      when WireType::START_GROUP
        read_start_group stream
      when WireType::END_GROUP
        read_end_group stream
      when WireType::FIXED32
        read_fixed32 stream
      else
        raise InvalidWireType.new(wire_type)
      end
    message.set_field tag, bytes
  end
  message
end